虽然 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 人员认为这是可见性。有个好article在关于这个主题的 GCC wiki 上。这是该文章的一个片段:
#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);
};
关于c++ - 不与共享库共享所有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638073/