簡體   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