c++ - 是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法?

标签 c++ c dll export linkage

在 cpp 文件中考虑这一点:

struct someStruct{
    public:
    extern "C"  __declspec(dllexport) int sumup();
} someStruct;

extern "C" __declspec(dllexport) int someStruct::sumup()
{
    return 0;
}

这不会编译:错误:字符串常量之前预期有不合格的 id 是否无法通过 C 链接导出 C++ 成员方法?

最佳答案

首先,链接规范不适用于成员函数;通过 [dcl.link]/4:

[...] A linkage-specification shall occur only in namespace scope (3.3). [...]

但是标准中甚至有一个与您的问题在某种程度上相关的示例,在同一段落中:

[...] A C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions. [Example:

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
  }
};

end example]

关于c++ - 是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441445/

相关文章:

c++ - 如何只将新文本写入文本文件

c++ - 将十进制美元金额从字符串转换为缩放整数

c++ - 将空的 c++ 文件添加到 xcode 项目会在 obj-c 头文件中创建语法错误

检查文件是否不存在或权限被拒绝 C

c++ - VS 2013 单元测试不起作用 => 设置执行上下文时出错

c++ - 获取参数包的前 N ​​个元素

c - 用一个头文件链接多个c文件

c - 解释 "About size_t and ptrdiff_t"中的这段话

java - 当尝试查找 dll 的位置时,jvm 会查看注册表吗?

java - 为什么 GetLastError() 会阻止我的方法?