简体   繁体   English

C ++共享库宏

[英]C++ Shared Library Macro

I have a C++ shared library. 我有一个C ++共享库。 The library has files to be exported. 库中有要导出的文件。 I was using Qt, which makes it quite easy, but I can't use it anymore. 我使用的是Qt,这很容易,但是我不能使用了。 So I need a pure C++ variant that covers it for Linux and Windows. 因此,我需要一个适用于Linux和Windows的纯C ++变体。 So I came up with the following macro definitions. 因此,我提出了以下宏定义。

Pure C++ 纯C ++

#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
    //  Microsoft
    #define MY_SHARED_EXPORT __declspec(dllexport)
#elif defined(__linux__) || defined(UNIX) || defined(__unix__) || defined(LINUX)
    //  GCC
    #define MY_SHARED_EXPORT __attribute__((visibility("default")))
#else
//  do nothing and hope for the best?
    #define MY_SHARED_EXPORT
    #pragma WARNING: Unknown dynamic link import/export semantics.
#endif

Qt C++ Qt C ++

#if defined(MY_LIBRARY)
#  define MY_SHARED_EXPORT Q_DECL_EXPORT
#else
#  define MY_SHARED_EXPORT Q_DECL_IMPORT
#endif

Currently I'm using the Qt C++ variant. 目前,我正在使用Qt C ++变体。 My question is if it is safe to replace the Qt variant, as seen above, with the pure C++ variant. 我的问题是,用纯C ++变体替换Qt变体是否安全(如上所述)。 And are they equivalent? 它们是否相等?

Any help is appreciated, thanks in advance. 任何帮助表示赞赏,在此先感谢。

It's safe to define your own import/export macros. 定义自己的导入/导出宏是安全的。 But the one you posted is not equivalent to Qt one because you did not handle the import. 但是您发布的那个不等于Qt一个,因为您没有处理导入。 It should be: 它应该是:

#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
    //  Microsoft
    #if defined(MY_LIBRARY)
        #define MY_SHARED_EXPORT __declspec(dllexport)
    #else
        #define MY_SHARED_IMPORT __declspec(dllimport)
    #endif
#elif defined(__linux__) || defined(UNIX) || defined(__unix__) || defined(LINUX)
    //  GCC
    #if defined(MY_LIBRARY)
        #define MY_SHARED_EXPORT __attribute__((visibility("default")))
    #else
        #define MY_SHARED_IMPORT
    #endif
#else
//  do nothing and hope for the best?
    #define MY_SHARED_EXPORT
    #pragma WARNING: Unknown dynamic link import/export semantics.
#endif

I'm not 100% sure __attribute__((visibility("default"))) applies to Linux. 我不是100%确定__attribute__((visibility("default")))适用于Linux。 In my mind, this was for iOS. 在我看来,这是针对iOS的。

As commented by Rafael, the easiest is probably to simply go to Qt sources (qglobal.h) and copy/paste Q_DECL_EXPORT / Q_DECL_IMPORT from here to your own header file and then include it from your environment. 正如Rafael所说,最简单的方法可能是简单地转到Qt源(qglobal.h), Q_DECL_IMPORT从此处将Q_DECL_EXPORT / Q_DECL_IMPORT复制/粘贴到您自己的头文件中,然后从您的环境中包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM