2015-11-11 56 views
0

我有一个C++共享库。该库有要导出的文件。我使用的是Qt,这很容易,但我不能再使用它了。所以我需要一个涵盖Linux和Windows的纯C++版本。所以我想出了以下宏定义。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变体是安全的,如上所示。他们是否等同?

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

+0

我建议您查看Q_DECLARE_ *宏的定义 –

回答

1

定义您自己的导入/导出宏是安全的。但是你发布的那个不等于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的。

正如评论由拉斐尔,最简单的可能是简单地去的Qt源(qglobal.h中)和复制/粘贴从这里Q_DECL_EXPORT/Q_DECL_IMPORT到你自己的头文件,然后包括它从您的环境。

+0

感谢您的回答,因为我没有使用它们,所以我放弃了导入。当我对Rafael做出回应时,复制Qt宏的问题是他们使用更多的宏,然后我真的需要。无论如何,我会尝试一下,看看它是如何发展的。再次感谢! – fonZ

+0

我建议你复制Qt宏,然后通过删除你不需要的东西来简化它们。 – jpo38

+0

最终我做到了。 – fonZ