GCC(ARM)对应于__declspec(dllexport)

8

当为x86构建应用程序时,以下代码可以正常工作:

#if   defined _WIN32
#define LIB_PRE __declspec(dllexport)
#elif defined __unix__
#define LIB_PRE
#else
#define LIB_PRE __declspec(dllexport)
#endif

但在GCC(ARM)上会出错。我已经发现__declspec(dllexport)在GCC上不能正常工作。如果是这样,那么在GCC(ARM)上该使用什么?

修改如下:

它用于许多类中。例如:

class CJsonValueString : public CJsonValue
{
 private:
  jstring value;
 public:
  LIB_PRE CJsonValueString(jstring value);
  LIB_PRE CJsonValueString(const CJsonValueString * value);
  LIB_PRE jstring ToString() const;
  LIB_PRE int ToInt() const;
  LIB_PRE int64 ToInt64 () const;
  LIB_PRE float ToFloat () const;
  LIB_PRE void GetValue(jstring & str) const;
};

你能展示一下这个宏的使用例子吗? - Kerrek SB
1
你可能是指“适用于Windows/x86”,因为它无法在“Linux/x86”上运行 - Basile Starynkevitch
还有一点需要提到的是,在Windows上,__declspec(dllexport)在MinGW GCC上可以正常工作。 - Kos
2个回答

7

基本上,您可能不需要任何特殊的东西。但是如果您想要(并且如果在共享对象上工作,即*.so文件),请了解有关可见性编译指示和可见性函数属性的更多信息。

问题更具有目标操作系统的特定性,而不是目标机器的特定性。(我想象一下运行某些晦涩的Windows8 / ARM系统的ARM也需要您的__declspec; 相反,在Linux / x86上使用您的__declspec是没有意义的)。


确切地说,Windows 8 可能也会在 ARM 上使用这些属性。我不明白为什么他们不会 :) - rubenvb
1
在我非常主观的看法中,Windows DLL 的设计比 Linux 共享库要糟糕得多。 - Basile Starynkevitch
5
我个人喜欢微软所强制实施的明确可见性。这样可以获得一个清晰的界面,尽管像MYLIB_DLL这样的定义会出现在某些地方 :/ - rubenvb
在 MINGW / GCC 中,__declspec 和可见性 pragma 都可以出现,其中使用 __declspec 似乎会抑制可见性 pragma,因此如果一个第三方库需要 __declspec,则所有库都必须具备它。 - Sam Ginrich

5
这是我们在代码中使用的简化版本。
#ifdef __cplusplus
#define EXTERNC         extern "C"
#else
#define EXTERNC
#endif

#if defined(__NT__)                   // MS Windows
  #define idaapi            __stdcall
  #define ida_export        idaapi
  #if defined(__IDP__)                  // modules
    #define idaman          EXTERNC
  #else                                 // kernel
    #if defined(__X64__) || defined(__NOEXPORT__)
      #define idaman          EXTERNC
    #else
      #define idaman          EXTERNC __declspec(dllexport)
    #endif
  #endif
  #define ida_local
#elif defined(__UNIX__)                 // for unix
  #define idaapi
  #if defined(__MAC__)
    #define idaman          EXTERNC __attribute__((visibility("default")))
    #define ida_local       __attribute__((visibility("hidden")))
  #else  // Linux
    #if __GNUC__ >= 4
      #define idaman          EXTERNC __attribute__ ((visibility("default")))
      #define ida_local       __attribute__((visibility("hidden")))
    #else
      #define idaman          EXTERNC
      #define ida_local
    #endif
  #endif
#endif

在Linux/OS X系统上,我们默认使用-fvisibility=hidden -fvisibility-inlines-hidden编译所有代码,并使用idaman标识我们想要导出的内容,例如:
idaman bool ida_export set_enum_width(enum_t id, int width);

如果您正在导出C++方法,您可能需要跳过extern "C"部分。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接