不与共享库共享所有类 [英] NOT sharing all classes with shared library

查看:156
本文介绍了不与共享库共享所有类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于win32 Microsoft编译器使用 __ declspec 宏,它确实具有明确显示您要导出或不导出的优点。

As ugly as win32 Microsoft compiler is by using the __declspec macro, it does have the advantage of being explicit about what you want to export or not.

将同样的代码移动到Linux的gnu / gcc系统现在意味着所有的类都被导出!

Moving the same code onto a Linux gnu/gcc system now means all classes are exported!(?)

这是真的吗?

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

Is there a way to NOT export a class within a shared library under 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人员考虑此可见性。有关此主题的GCC wiki上有一个很好的文章。以下是该文章的一段代码:

This is possible in GCC 4.0 and later. The GCC folks consider this visibility. There is a good article on the GCC wiki about the subject. Here is a snippet from that article:

#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);
};

这篇关于不与共享库共享所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆