代码之家  ›  专栏  ›  技术社区  ›  Ian Vaughan

不与共享库共享所有类

  •  4
  • Ian Vaughan  · 技术社区  · 15 年前

    通过使用 第二章 宏,它的优势在于明确您要导出或不导出的内容。

    将相同的代码移动到LinuxGNU/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
    {
        ...
    }
    
    2 回复  |  直到 15 年前
        1
  •  12
  •   Dan    15 年前

    这在GCC4.0及更高版本中是可能的。海湾合作委员会的人认为 能见度 . 有一个好 article 在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);
    };
    
        2
  •  0
  •   unwind    15 年前

    如果一个类不应该是可用的,那么它不应该在公共头中。共享用户不能使用的声明有什么意义?