[CMake] Proper way to export a library
J Decker
d3ck0r at gmail.com
Thu Oct 31 15:26:59 EDT 2013
something like.... (but probably mostly in a common header)
if( IS_THIS_PROJECT )
if( UNIX or STATIC )
set( EXPORT ) # nothing, everything is exported by default; gcc/unix
else( UNIX or STATIC )
set( EXPORT __declspec(dllexport) )
endif( UNIX or STATIC )
else( IS_THIS_PROJECT )
# if something else includes this....
if( UNIX or STATIC )
set( EXPORT extern )
else( UNIX or STATIC )
set( EXPORT __declspec(dllimport) )
endif( UNIX or STATIC )
endif( IS_THIS_PROJECT )
On Thu, Oct 31, 2013 at 11:46 AM, Hendrik Sattler
<post at hendrik-sattler.de> wrote:
>
>
> 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.)
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
More information about the CMake
mailing list