[CMake] Clarification around "target_link_libraries"

Alexander Neundorf a.neundorf-work at gmx.net
Thu Sep 12 15:36:00 EDT 2013


On Thursday 12 September 2013, SF Markus Elfring wrote:
> Hello,
> 
> CMake supports also building of static and shared libraries together.
> http://cmake.org/cmake/help/v2.8.11/cmake.html#command:add_library
> 
> I get the impression from the tutorial that a needed library can be passed
> as a target name for the macro "target_link_libraries".
> http://cmake.org/cmake/help/cmake_tutorial.html#s2
> 
> But I get a different experience.
> http://cmake.org/cmake/help/v2.8.11/cmake.html#command:target_link_librarie
> s
> 
> Now I try to give a build configurator the choice with which library type
> some executable files should be linked. It seems that I need to construct
> corresponding file names instead of target names.

No, you don't have to.


Do you want to build static and dynamic lib at the same time ?
Then do something like this ?

add_library(foo STATIC ${libSrcs})
add_library(fooShared SHARED ${libSrcs})

set(myLibs foo)

if (USER_DECIDED_TO_LINK_AGAINST_SHARED_LIBS)
  set(myLibs fooShared)
else()
  set(myLibs foo)
endif()


add_executable(hello ${hellosSrcs})

target_link_libraries(hello ${myLibs} )

Alex


More information about the CMake mailing list