[CMake] import/export and DLL's

Michael Wild themiwi at gmail.com
Tue Dec 6 07:50:20 EST 2011


On 12/06/2011 10:42 AM, Totte Karlsson wrote:
> Thanks for feedback
>>
>> You misunderstand and misuse the DEFINE_SYMBOL property. All the
>> IMPORT_X_DLL symbols are wrong, you have to remove them.
> 
> But when building the B, and C dll the import symbols need to be defined
> somehow. See my export/import header below.

Well, then just use add_definitions(-DIMPORT_X_DLL). If -DEXPORT_X_DLL
is present, it will override your export/import header definitions for
the import case, and everything should be fine.

> 
> CMake will only
>> add the DEFINE_SYMBOL when *building* the specified target, and
>> otherwise just leave it away.
> Not sure I understand.
> 

When CMake invokes the compiler to *build* the DLL it adds
-DEXPORT_X_DLL. If the DLL is being *used* it doesn't do anything.

> 
>> So, in your code you do something like this:
>>
>> #ifdef EXPORT_A_DLL
>> #define A_API __declspec(dllexport)
>> #else
>> #define A_API __declspec(dllimport)
>> #endif
>>
> I'll try to see if that works. Right now I do have an exporter/importer
> header and it is more complex and looks like (for a target "COMMON"):
> #if defined(EXPORT_COMMON_DLL)
>     #define MTK_COMMON __declspec(dllexport)
> #elif defined(IMPORT_COMMON_DLL)
>     #define MTK_COMMON __declspec(dllimport)
> #elif defined(EXPORT_COMMON_PKG)
>     #define MTK_COMMON __declspec(package)
> #elif defined(IMPORT_COMMON_PKG)
>     #define MTK_COMMON __declspec(package)
> #else
>     #define MTK_COMMON
> #endif

Is this for embarcadero c++? in which case do you define
EXPORT_COMMON_DLL, and when do you use EXPORT_COMMON_PKG?

> 
> Is the problem perhaps that the EXPORT_COMMON_DLL defined symbol is not
> "undeffed" so when the B DLL need to Import from A, it can't?

If you are referring to your original code, that was completely broken.
As I explained above, if you have a structure like this


add_library(A SHARED a.cpp)
add_library(B SHARED b.cpp)
target_link_libraries(B A)

the following will happen:

- A.cpp is compiled with -DA_EXPORTS. The DEFINE_SYMBOL property for
target B is ignored.

- DLL A is created

- B.cpp is compiled with -DB_EXPORTS. The DEFINE_SYMBOL property for
target A is ignored.

- DLL B is created and linked against A


To help you more, I would need to understand __declspec(package). The
Embarcadero docs didn't really help me grasp its use...

Michael


More information about the CMake mailing list