[CMake] Proper way to export a library

Hendrik Sattler post at hendrik-sattler.de
Thu Oct 31 14:46:26 EDT 2013



Matthew Woehlke <matthew.woehlke at kitware.com> schrieb:
>On 2013-10-31 05:26, Cyrille Faucheux wrote:
>> Can you tell me a bit more about "implicit compile flags [...] for
>imported
>> tagets"?
>
>See documentation on target_compile_definitions.
>
>> On the library I'm currently working on, with Visual Studio, I have
>to
>> declare some compile definition in order to get the proper
>> "__declspec(dllimport)" or "__declspec(dllexport)" defined (or not
>defined
>> when compiling/using a static version of this library).
>
>This sounds like you're doing it wrong.

Not really as you can only cover the export import with this case but not static vs. dynamic linking to that library with the same header file.
That it's usually solved with a define when linking statically

>When building with CMake, the preferred way is to unconditionally
>define 
>an ABI export symbol via a header which exists for that purpose (e.g. 
>config.h, my_package_exports.h, etc.). The value of the definition 
>however is conditional on a symbol that is only defined when building 
>the library, to choose between import and export as appropriate.
>
>Even better, CMake will define <target>_EXPORTS for you when building a
>
>library, so you shouldn't need to manually specify any definitions at
>all.
>
>IOW you libraries headers would somewhere contain:
>
>#if defined(_WIN32)
>#  define MYPROJECT_ABI_EXPORT __declspec(dllexport)
>#  define MYPROJECT_ABI_IMPORT __declspec(dllimport)
>#elif __GNUC__ >= 4
>#  define MYPROJECT_ABI_EXPORT __attribute__ ((visibility("default")))
>#  define MYPROJECT_ABI_IMPORT __attribute__ ((visibility("default")))
>#else
>#  define MYPROJECT_ABI_EXPORT
>#  define MYPROJECT_ABI_IMPORT
>#endif
>
>...and:
>
>#ifdef mylibrary_EXPORTS
>#  define MYLIBRARY_EXPORT MYPROJECT_ABI_EXPORT
>#else
>#  define MYLIBRARY_EXPORT MYPROJECT_ABI_IMPORT
>#endif
>
>
>That said...
>
>> Does "implicit compile flags" means that those compile definition
>could be
>> automatically declared by the imported target?
>
>...this was my understanding of how target_compile_definitions(PUBLIC) 
>is supposed to work. (Note: I haven't actually used this feature myself
>
>yet.)




More information about the CMake mailing list