Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake add a library which is a group of other libraries

Tags:

cmake

I have a core library which branches off into several other libraries. In CMakeLists.txt it looks a bit like this

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

I have some executables which may depend on any or all of the branches. For those that depend on all the 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 spits out 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 it's not an error. What does CMake think is an acceptable way to build a meta-library like this?

like image 780
spraff Avatar asked Feb 04 '14 19:02

spraff


People also ask

How do I create a shared library in CMake?

To build everything, just place yourself into the build directory and run: /tmp/example/library/build $ cmake .. That's it, you should find mymath. h under /usr/local/include as well as libmymath.so under /usr/local/lib .

What does target link libraries do in CMake?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

What does CMake add_library do?

Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project. The actual file name of the library built is constructed based on conventions of the native platform (such as lib<name>.


1 Answers

I added a new type of library, the INTERFACE library to the upcoming CMake 3.0.0 release. That is designed as the 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)
like image 108
steveire Avatar answered Dec 01 '22 02:12

steveire