CMake add a library which is a group of other libraries

I have a main library that is sent to several other libraries. In CMakeLists.txt, this is a bit like

ADD_LIBRARY (core ...) ADD_LIBRARY (branch1 ...) ADD_LIBRARY (branch2 ...) ... TARGET_LINK_LIBRARIES (branch1 core) TARGET_LINK_LIBRARIES (branch2 core) ... 

I have some executables that may depend on any or all branches. For those that depend on all branches, instead of writing

 ADD_EXECUTABLE (foo ...) TARGET_LINK_LIBRARIES (foo branch1 branch2 ...) 

I tried

 ADD_LIBRARY (all-branches) TARGET_LINK_LIBRARIES (all-branches branch1 branch2 ...) 

then

 TARGET_LINK_LIBRARIES (foo all-branches) 

This works, but CMake issues a warning.

 You have called ADD_LIBRARY for library all-branches without any source files. This typically indicates a problem with your CMakeLists.txt file 

I understand the message, but this is not a mistake. What does CMake think is an acceptable way to create a meta library?

+6
source share
2 answers

I have added a new type of library, the INTERFACE library, to the upcoming version of CMake 3.0.0. This solution as a solution to this problem:

http://www.cmake.org/cmake/help/git-master/manual/cmake-buildsystem.7.html#interface-libraries

 add_library(all_branches INTERFACE) target_link_libraries(all_branches INTERFACE branch1 branch2) add_executable(myexe ...) target_link_libraries(myexe all_branches) 
+9
source

In CMAKE, add_executable and add_library are very similar. They simply tell CMAKE that it should create a MAKE statement for the library or executable based on the list of src files that you provide after the library / executable name and parameters (such as SHARED, etc.).

What you can do is add the names of your libraries that you want to link in the name variable you are incrementing, for example

 SET(TARGET_LIBS ${TARGET_LIBS} myFirstLib) SET(TARGET_LIBS ${TARGET_LIBS} myNextLib) 

and then just:

 target_link_libraries(myExe ${TARGET_LIBS}) 

Thus, you can easily define the library groups that may be needed for different subprojects without creating a meta-lib.

0
source

All Articles