Reputation: 2479
I have a C++ shared library. The library has files to be exported. I was using Qt, which makes it quite easy, but I can't use it anymore. So I need a pure C++ variant that covers it for Linux and Windows. So I came up with the following macro definitions.
Pure 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
Currently I'm using the Qt C++ variant. My question is if it is safe to replace the Qt variant, as seen above, with the pure C++ variant. And are they equivalent?
Any help is appreciated, thanks in advance.
Upvotes: 0
Views: 1535
Reputation: 21504
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. 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. In my mind, this was for 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.
Upvotes: 1