繁体   English   中英

C ++共享库宏

[英]C++ Shared Library Macro

我有一个C ++共享库。 库中有要导出的文件。 我使用的是Qt,这很容易,但是我不能使用了。 因此,我需要一个适用于Linux和Windows的纯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 ++

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

目前,我正在使用Qt C ++变体。 我的问题是,用纯C ++变体替换Qt变体是否安全(如上所述)。 它们是否相等?

任何帮助表示赞赏,在此先感谢。

定义自己的导入/导出宏是安全的。 但是您发布的那个不等于Qt一个,因为您没有处理导入。 它应该是:

#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

我不是100%确定__attribute__((visibility("default")))适用于Linux。 在我看来,这是针对iOS的。

正如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