문제

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