13

I have a huge project managed with CMake and this project has hundreds of components each of them having own source files and each of them linking to a list of libraries, specified with target_link_libraries(${project} some_libraries, some_other_libraries)

Now, what I am aiming for is that:

Without actually modifying any of the CMakeLists.txt I want ALL the projects's target executable to link to some specific libraries.

Is there a way of achieving this? Since this is a one time trial, I don't want to manually hunt down all the CMakeLists.txt files and modify them (yes, this is the other alternative). Just a note, I compile the entire project from command line, using cmake (no cmake gui).

2 Answers 2

17

This is kind of a hack, but for a C++ project, you can use CMAKE_CXX_STANDARD_LIBRARIES. For a C project, I think you would use CMAKE_C_STANDARD_LIRBARIES.

Example for C++ that links to libbar and libfoo:

cmake ... -DCMAKE_CXX_STANDARD_LIBRARIES="-lbar -lfoo"

See the documentation here:

https://cmake.org/cmake/help/v3.6/variable/CMAKE_LANG_STANDARD_LIBRARIES.html

This won't be available for older versions of CMake; it was added some time after version 3.0.

1
  • 1
    There must be cleaner hack for this six years later, right? Commented Nov 4, 2023 at 16:01
3

This is a dirty, dirty hack, so please only use it for testing.

You can actually overload the add_executable command by defining a function of the same name. Do this close to the top of the top-level CMakeLists.txt:

function (add_executable name)
    message("Added executable: " ${name})
    _add_executable(${name} ${ARGN})
    target_link_libraries(${name$} your_additional_lib)
endfunction()

Note that _add_executable is an internal CMake name that may break in future CMake versions. As of now (version 3.0) it seems to work with all versions though.

You can overload add_library the same way if required.

For more fine-grained control over what is linked, instead of calling target_link_libraries you can also mess with the LINK_LIBRARIES and INTERFACE_LINK_LIBRARIES target properties directly.

3
  • indeed adds the libraries (libz pthread) as expected ... however it adds them at the beginning of the library list, so I get: -rdynamic -lz -lpthread ../../libfappcore.a -lQtXml -lQtSql -lQtNetwork -lQtCore ../../../externals/gmock/libgmock.a ../../../externals/gmock/gtest/libgtest.a unfortunately the linker fails: undefined reference to symbol 'deflate' ... if I add manually to the end of the compile command (like: -rdynamic ../../libfappcore.a -lQtXml -lQtSql -lQtNetwork -lQtCore ../../../externals/gmock/libgmock.a ../../../externals/gmock/gtest/libgtest.a -lz -lpthread) it works :( Commented Aug 11, 2014 at 13:29
  • 1
    @fritzone I see. In that case, do not call target_link_libraries directly. Instead, save all the names of your executables to a list. Then, at the very end of your top-level CMakeLists, foreach over the list and call target_link_libraries there. Commented Aug 11, 2014 at 13:34
  • This is indeed an excellent idea and I like it a lot, unfortunately does not work: CMake Error at CMakeLists.txt:87 (target_link_libraries): Attempt to add link library "pthread" to target "CoreMessageUnitTests" which is not built in this directory. Commented Aug 12, 2014 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.