すべてのクラスを共有ライブラリと共有しない
-
08-07-2019 - |
質問
win32 Microsoftコンパイラは __ declspec マクロを使用するのと同じくらいAsいので、エクスポートするかどうかを明示的に指定できるという利点があります。
同じコードを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 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);
};
他のヒント
クラスを使用できないようにする場合、パブリックヘッダーに含めることはできません。ユーザーが使用できないものの宣言を共有するポイントは何ですか?
所属していません StackOverflow