是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法
Is it possible to export a C++ member method with C linkage in a DLL?
在 cpp 文件中考虑这一点:
struct someStruct{
public:
extern "C" __declspec(dllexport) int sumup();
} someStruct;
extern "C" __declspec(dllexport) int someStruct::sumup()
{
return 0;
}
这不会编译:error: expected unqualified-id before string constant
是否无法导出具有 C 链接的 C++ 成员方法?
首先,链接规范不适用于成员函数; 通过 [dcl.link]/4:
[...]链接规范应仅在命名空间范围 (3.3) 中出现。[...]
但是,在同一段中,标准中甚至有一个与您的问题相关的示例:
[...]在确定类成员名称的语言链接和类成员函数的函数类型时,将忽略 C 语言链接。[示例:
extern "C" typedef void FUNC_c(); class C { void mf1(FUNC_c*); // the name of the function mf1 and the member // function’s type have C ++ language linkage; the // parameter has type pointer to C function FUNC_c mf2; // the name of the function mf2 and the member // function’s type have C ++ language linkage static FUNC_c* q; // the name of the data member q has C ++ language // linkage and the data member’s type is pointer to // C function }; extern "C" { class X { void mf(); // the name of the function mf and the member // function’s type have C ++ language linkage void mf2(void(*)()); // the name of the function mf2 has C ++ language // linkage; the parameter has type pointer to // C function } };
— 结束示例]
No. 它不是那样工作的。 它根本不起作用。 做不到。 没办法不行。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 使用类模板的方法链接错误
- 类和类成员的链接
- 为什么调用没有正文的纯虚拟方法不会导致链接器错误?
- 指向(数据)成员的指针作为非类型模板参数,例如具有自动存储持续时间/无链接
- 在使用库时,找到要链接的必要库的快速方法是什么
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 将多个效果与 libSox 链接并读取输出数据的正确方法
- 当只有静态方法受到影响时,如何解决C++中的链接器错误?
- C++ - 使用 std::list,如何打印对象的私有成员的链接列表?
- 从方法链接中使用的临时移动
- 无法使用 Gradle 链接同一源文件中C++方法
- C++标准是否定义了结构中成员函数的函数内定义是否必须具有静态链接?
- 为什么链接器报告全局函数的乘法定义符号,而不是类静态方法
- 单独库中的类成员函数定义和链接依赖项
- 使用 CMake 的静态方法链接错误
- C++ 使用 .chain().method() 链接成员函数 vers ->chained(0->method()
- 是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法
- 如何"decrypt"链接器方法名称?
- 方法内部的简单c++成员引用(方法解析后的链接器错误)