[CMake] portable way of linking (external) libs statically/dynamically/for dlopen

Philippe Cerfon philcerf at gmail.com
Mon Apr 29 21:44:40 EDT 2013


On Tue, Apr 30, 2013 at 1:54 AM, J Decker <d3ck0r at gmail.com> wrote:
> lib${CMAKE_LIBRARY_SUFFIX}/${CMAKE_SHARED_LIBRARY_PREFIX}c${CMAKE_SHARED_LIBRARY_SUFFIX}
> or
> lib${CMAKE_LIBRARY_SUFFIX}/${CMAKE_STATIC_LIBRARY_PREFIX}c${CMAKE_STATIC_LIBRARY_SUFFIX}
>
> to choose lib[64]/libc.so or lib[64]/libc.a  (or it would generate
> c.dll/c.lib for windows type compilers)
>
> But, although you can generate portable projects that target lots of
> platforms, the scripts still have to have things like
>
> if( WIN32 )
> if( UNIX )
> etc...
Well exactly this is what I don't wan to do as it's not portable...
_I_ (the developer) need to take care on any possible platorm, it's
library naming conventions and possibly also on any compiler/linker
suite I want to support...
If the responsibility for this falls back to me, I can also build via
shell script (more or less).

What I'd like to have is:
1) My CMakeLists checks for all dependencies (e.g. lib foo) using the
CMakes functions for that.
These functions should tell me which versions were found (i.e. static
or shared).
2) Depending on (1) I set CMake options cache with reasonable defaults
(i.e. in most cases this will be "link lib foo dynamic") for each lib
foo.
For some I might even offer to use dlopen(), depending on whether my
sources support this.
3) The user can then chance that settings per lib in the cache as he likes.
4) When I say target_link_library... I want to set some flag (static
vs. shared) depending on what was chosen in (3) (or not link at all in
case of dlopen()),

Everything else, platform naming conventions, compiler/linker options,
should happen automatically (for all supported platforms +
compilter/linker suites)... that would be portability.


Cheers!


More information about the CMake mailing list