与win32一样丑陋Microsoft编译器使用 __ declspec 宏,它确实具有明确关于你想要导出的内容的优势。

现在将相同的代码移到Linux gnu / gcc系统上意味着导出所有类!(?)

这是真的吗?

有没有办法在gcc下的共享库中导出类?

#ifndef WIN32
#define __IMPEXP__
#else
#undef __IMPEXP__
#ifdef __BUILDING_PULSETRACKER__
#define __IMPEXP__ __declspec(dllexport)
#else
#define __IMPEXP__ __declspec(dllimport)
#endif // __BUILDING_PULSETRACKER__
#endif // _WIN32

class __IMPEXP__ MyClass
{
    ...
}
有帮助吗?

解决方案

这在GCC 4.0及更高版本中是可行的。 GCC人员会考虑可见性。关于该主题的GCC维基上有一篇很好的文章。以下是该文章的摘录:

#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #ifdef __GNUC__
      #define DLL_PUBLIC __attribute__((dllexport))
    #else
      #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #else
    #ifdef __GNUC__
      #define DLL_PUBLIC __attribute__((dllimport))
    #else
      #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #endif
    #define DLL_LOCAL
#else
  #if __GNUC__ >= 4
    #define DLL_PUBLIC __attribute__ ((visibility("default")))
    #define DLL_LOCAL  __attribute__ ((visibility("hidden")))
  #else
    #define DLL_PUBLIC
    #define DLL_LOCAL
  #endif
#endif

extern "C" DLL_PUBLIC void function(int a);
class DLL_PUBLIC SomeClass
{
   int c;
   DLL_LOCAL void privateMethod();  // Only for use within this DSO
 public:
   Person(int _c) : c(_c) { }
   static void foo(int a);
};

其他提示

如果某个类不可用,则不应该在公共标题中。分享用户无法使用的事物的声明是什么意思?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top