Automatically add linking options to dependants of shared lib - cmake

Let's say I have a shared lib, A, and something else that links against it, B. They are in two separate projects.
In my current setup, for A's functionality to be able to work properly, B needs to add -rdynamic to its linker options, like this:
target_link_libraries(B -rdynamic)
The thing is that there may be many, many dependants of A, so having to explicitly include the line above for each of them is a hassle.
Is there a way for A to have all its dependants automatically use -rdynamic?
Edit: here's a more detailed explanation of my situation.
I have a shared library, mylib. It cannot be a static library. It is defined in a CMake project of the same name.
mylib
├── CMakeLists.txt
└── src
   └── ...
Its CMakeLists.txt looks like this:
# mylib/CMakeLists.txt
project(mylib)
...
add_library(mylib SHARED ${SOURCES})
...
In separate projects, there are executables, client_i, that use/link against mylib, sometimes indirectly (i.e. link against something that links against mylib).
client_0
├── CMakeLists.txt
└── src
   └── ...
client_1
├── CMakeLists.txt
└── src
   └── ...
client_2
├── CMakeLists.txt
└── src
   └── ...
...
One of those CMakeLists.txt looks like:
# client_i/CMakeLists.txt
project(client_i)
...
add_executable(client_i ${SOME_OTHER_SOURCES})
target_link_libraries(client_i mylib -rdynamic)
...
Notice the -rdynamic. Without this option, some functionality provided by mylib doesn't work. I need it.
My problem is that there may be thousands of different client_is, each defined in its own project by different people/users of mylib, which I provide (and may be used as a binary).
I'd like to avoid having to add -rdynamic to each of the client_is, since some users might not know about it (since it may be used indirectly), some might forget about it, it might lead to headaches, etc.
Ideally, the CMakeLists.txt for mylib would look like:
# mylib/CMakeLists.txt
project(mylib)
...
add_library(mylib SHARED ${SOURCES})
target_link_libraries(mylib -rdynamic)
...
And a client_i CMakeLists.txt would simply be:
# client_i/CMakeLists.txt
project(client_i)
...
add_executable(client_i ${SOME_OTHER_SOURCES})
target_link_libraries(client_i mylib)
...
And it would have -rdynamic in its linker options. But I have tried this and it does not seem to work.

This is a simple solution you can try:
# Get a list of A's direct library dependencies.
get_target_properties(LIB_DEPENDENCIES A LINK_LIBRARIES)
# Loop through each library, adding the link option to each.
foreach(LIB ${LIB_DEPENDENCIES})
target_compile_options(${LIB} PRIVATE "-rdynamic")
endforeach()
This can be expanded, similar to the answer #KamilCuk linked here, to account for static/shared/imported libraries and recursion depending on the variety and complexity of your dependencies.

How to link A against B when A and B are in different projects?
If A is a different project than B, the you have to export package A and use find_package in project B to find A. Here is a good tutorial show-casing the use-case.

Related

CMake build target without specifying target_include_directories

I have a project with the following file layout:
Project
├── CMakeLists.txt
├── app
│ ├── CMakeLists.txt
│ └── main.cpp
└── ext
├──CMakeLists.txt
└── lib
├── CMakeLists.txt
├── include
│ └── foo.h
└── foo.cpp
foo is a 3rd party library that I downloaded the source code of that I want to use in main.cpp. I can build main.cpp using a cmake file like:
add_executable(app main.cpp)
target_include_directories(app PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/extern/foo/include)
target_link_libraries(app foo)
However I feel it should be the responsibility of the foo lib to specify what files should be included when using it. Is there a way to make this work without the target_include_directories call?
Just add
target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
into extern/foo/CMakeLists.txt.
That way the include directory will be accessible when you build foo library and for anyone who links with that library.
Note, that automatic propagation of include directory and other library properties works only when you link with the library target:
# Assuming you have 'add_library(foo)' somewhere,
# PUBLIC and INTERFACE properties of the `foo` library will be propagated
# to the 'app'.
target_link_libraries(app PUBLIC foo)
This would work even if add_library(foo) is issued after target_link_libraries call.
Propagation won't work when link with the library file:
# Propagation won't work with a name of the library file:
target_link_libraries(app PUBLIC foo.a)
# Propagation won't work with a full path to the library file:
target_link_libraries(app PUBLIC /path/to/foo.a)
# If 'add_library(foo)' in inaccessible from the project,
# then linking with 'foo' means linking with a library file,
# so propagation won't work.
target_link_directories(app PUBLIC /path/to/)
target_link_libraries(app PUBLIC foo)

use set_tests_properties from test in other directory

(related to add standard libraries to RPATH for build tree executables)
I have the following directory structure as minimal working example
.
├── baz.cc
├── baz.h
├── CMakeLists.txt
└── sub
├── CMakeLists.txt
└── foo.cc
sub is in practice a git submodule and should not be edited for my specific corner case but remain generic.
sub/CMakeLists.txt defines a test that does not run in my setup because a shared library is not added to the RPATH by cmake. To reproduce this behavior, it can be thought be
enable_testing()
add_executable(foo foo.cc)
set_property(TARGET foo PROPERTY SKIP_BUILD_RPATH TRUE)
target_link_libraries(foo ${CMAKE_CURRENT_BINARY_DIR}/../libbaz.so)
include_directories(..)
add_test(NAME footest COMMAND foo)
(foo.cc can be:
#include "baz.h"
int main() {
return bazinga() - 42;
}
let's assume a shared library with the bazinga symbol will always be available, e.g. from /usr/lib)
My top level CMakeLists.txt is
add_subdirectory(sub)
add_library(baz SHARED baz.cc)
I want to get the footest running by manipulating its LD_LIBRARY_PATH as suggested here. This would be by adding
set_tests_properties(footest PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=/path_where_libbaz.so_is_on_my_target_system")
This line does the job when I add it to sub/CMakeLists.txt, but fails with the following error when I edit the top level CMakeLists.txt
CMake Error at CMakeLists.txt:3 (set_tests_properties):
set_tests_properties Can not find test to add properties to: footest
given the above reason, I want to do the fixup without editing anything in the subdirectory.
Is there a way to set properties of tests that are defined in other directories than the current one?

Single CMakeLists.txt enough for my project?

I am trying to port my old CMake to modern CMake (CMake 3.0.2 or above). In the old design I had multiple CMakelists.txt, each directory contained a CMakeLists.txt file.
My current project's directory structure looks like :
.
├── VizSim.cpp
├── algo
├── contacts
│   ├── BoundingVolumeHierarchies
│   │   └── AABBTree.h
│   └── SpatialPartitoning
├── geom
│   └── Geometry.h
├── math
│   ├── Tolerance.h
│   ├── Vector3.cpp
│   └── Vector3.h
├── mesh
│   ├── Edge.h
│   ├── Face.h
│   ├── Mesh.cpp
│   ├── Mesh.h
│   └── Node.h
├── util
| |__ Defines.h
| |__ Math.h
|
└── viz
└── Renderer.h
What I was planning to do was just use a single CMakelists.txt and place all the cpp files in SOURCE and all the headers in HEADER and use add_executable.
set (SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/src/mesh/Mesh.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/math/Vector3.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VizSim.cpp
....
)
set (HEADER
${HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/src/mesh/Mesh.h
${CMAKE_CURRENT_SOURCE_DIR}/src/math/Vector3.h
....
)
add_library(${PROJECT_NAME} SHARED ${SOURCE})
Doing this I am worried if using a single CMakeLists.txt is good practice. So does single CMakeLists.txt suffice or do I need a CMakeLists.txt for each folder?
I can only think of one good reason to have multiple CMakeLists.txt in my project and that is modularity.
Considering my project will grow eventually.
This is a bit long for a comment – so I make it an answer:
In one of my projects (a library), I have that many sources that I started to move some of them in a sub-directory util.
For this, I made separate variables:
file(GLOB headers *.h)
file(GLOB sources *.cc)
file(GLOB utilHeaders
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/util/*.h)
file(GLOB utilSources
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/util/*.cc)
To make it nice looking / more convenient in VisualStudio, I inserted source_groups which generates appropriate sub-folders in the VS project. I believe they are called "Filters".
source_group("Header Files\\Utilities" FILES ${utilHeaders})
source_group("Source Files\\Utilities" FILES ${utilSources})
Of course, I have to consider the variables utilHeaders and utilSources as well where the sources have to be provided:
add_library(libName
${sources} ${headers}
${utilSources} ${utilHeaders})
That's it.
Fred reminded in his comment that I shouldn't forget to mention that file(GLOB has a certain weakness (although I find it very valuable in our daily work). This is even mentioned in the CMake doc.:
Note: We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate. The CONFIGURE_DEPENDS flag may not work reliably on all generators, or if a new generator is added in the future that cannot support it, projects using it will be stuck. Even if CONFIGURE_DEPENDS works reliably, there is still a cost to perform the check on every rebuild.
So, using file(GLOB, you shouldn't never forget to re-run CMake once files have been added, moved, or removed. An alternative could be as well, to add, move, remove the files directly in the generated built-scripts (e.g. VS project files) and rely on the fact that the next re-run of CMake will those files cover as well. Last but not least, a git pull is something else that it's worth to consider a re-run of CMake.
I would always recommend a CMakeList.txt file per directory. My reasons:
locality: keep everything in the same folder that belongs together. This includes the relevant parts of the build system. I would hate it to navigate to the root folder to see how a library or target was invoked.
separation of build artifacts and related build code: Tests belong below test, libraries below lib, binaries below bin, documentation below doc, and utilities below utils. This may vary from project to project. When I have to make a change to the documentation, why should I wade through dozens of unrelated CMake code? Just have a look into the right CMakeLists.txt.
avoid handling of paths: In most cases relative or absolute paths including stuff like ${CMAKE_CURRENT_SOURCE_DIR} can be avoided. That leads to maintainable build code and reduces errors from wrong paths. Especially with out-of-source build, which should be used anyway.
localization of errors: If a CMake error occurs it is easier to locate the problem. Often a sub-directory can be excluded as a first workaround.

CMake adding files with ends with "*.a" [duplicate]

Clion: how add or (use) prebuilt static library in my Project?
You're probably asking about how to link your project to the pre-built static library. If so, you can do like this by calling target_link_libraries.
Assume your project called myProj and the pre-built library myLib.lib, you can do like this:
target_link_libraries(myProj myLib)
I had great difficulty making this work as I was completely new to CLion and CMake.
In my scenario I was taking a class that required us to use the course library in every project.
Assuming you have a library called libClassLibrary.a, do the following in the CMakeLists.txt at the project root:
First, find the library's location:
find_library(LIB_TO_INCLUDE ClassLibrary /path/to/your/library)
LIB_TO_INCLUDE will contain the location of the library assuming it is found. Note that hardcoding the path could be problematic if you'd like your solution to be portable to other systems. You can add additional search paths separated by a space if the library could exist in multiple locations. A typical example is to include common install locations such as /usr/bin /usr/local/bin etc.
Next, ensure that header files (if applicable) are included in the header search paths:
find_path (LIB_INCLUDES ClassLibrary.h /path/to/header/files)
Again, include multiple search paths if the headers could be loaded in multiple locations. If there is more than one header file, you'll need to include all of them.
Now, include the directories using the include_directories command:
include_directories(${LIB_INCLUDES})
The above will instruct the build system to search all directories contained in LIB_INCLUDES or whatever you decide to call it.
Finally, add the executable and use the target_link_libraries command to link the libClassLibrary.a.
add_executable(MyExecutable main.cpp)
target_link_libraries(MyExecutable ${LIB_TO_INCLUDE})
That's all. You'll notice that under "External Libraries" > "Header Search Paths" in the Project organizer window the directories containing your header files appears.
PS - The book Mastering CMake by Ken Martin and Bill Hoffmann is an invaluable resource.
I've linked my static lib test.a with the related header files as following:
Project
├── main.cpp
├── CmakeLists.txt
├── libs
│ ├── includes
│ │ ├── *.h
│ ├── lib
│ │ ├── test.a
I've added this to the ./CMakeLists.txt:
include_directories(${CMAKE_SOURCE_DIR}/libs/include)
find_library(Test_LIB test "${CMAKE_SOURCE_DIR}/libs/lib")
add_executable(TestApp main.cpp)
target_link_libraries(TestApp ${Test_LIB})
By adding message(${Test_LIB}), you can print out and check the path.
Your question is unrelated to CLion, it is pure CMake. Modify the CMakeLists.txt from your project and use add_library. The CMake documentation might be helpful.
I misunderstood the question, target_link_library is probably the answer to the question.

How to add prebuilt static library in project using CMake?

Clion: how add or (use) prebuilt static library in my Project?
You're probably asking about how to link your project to the pre-built static library. If so, you can do like this by calling target_link_libraries.
Assume your project called myProj and the pre-built library myLib.lib, you can do like this:
target_link_libraries(myProj myLib)
I had great difficulty making this work as I was completely new to CLion and CMake.
In my scenario I was taking a class that required us to use the course library in every project.
Assuming you have a library called libClassLibrary.a, do the following in the CMakeLists.txt at the project root:
First, find the library's location:
find_library(LIB_TO_INCLUDE ClassLibrary /path/to/your/library)
LIB_TO_INCLUDE will contain the location of the library assuming it is found. Note that hardcoding the path could be problematic if you'd like your solution to be portable to other systems. You can add additional search paths separated by a space if the library could exist in multiple locations. A typical example is to include common install locations such as /usr/bin /usr/local/bin etc.
Next, ensure that header files (if applicable) are included in the header search paths:
find_path (LIB_INCLUDES ClassLibrary.h /path/to/header/files)
Again, include multiple search paths if the headers could be loaded in multiple locations. If there is more than one header file, you'll need to include all of them.
Now, include the directories using the include_directories command:
include_directories(${LIB_INCLUDES})
The above will instruct the build system to search all directories contained in LIB_INCLUDES or whatever you decide to call it.
Finally, add the executable and use the target_link_libraries command to link the libClassLibrary.a.
add_executable(MyExecutable main.cpp)
target_link_libraries(MyExecutable ${LIB_TO_INCLUDE})
That's all. You'll notice that under "External Libraries" > "Header Search Paths" in the Project organizer window the directories containing your header files appears.
PS - The book Mastering CMake by Ken Martin and Bill Hoffmann is an invaluable resource.
I've linked my static lib test.a with the related header files as following:
Project
├── main.cpp
├── CmakeLists.txt
├── libs
│ ├── includes
│ │ ├── *.h
│ ├── lib
│ │ ├── test.a
I've added this to the ./CMakeLists.txt:
include_directories(${CMAKE_SOURCE_DIR}/libs/include)
find_library(Test_LIB test "${CMAKE_SOURCE_DIR}/libs/lib")
add_executable(TestApp main.cpp)
target_link_libraries(TestApp ${Test_LIB})
By adding message(${Test_LIB}), you can print out and check the path.
Your question is unrelated to CLion, it is pure CMake. Modify the CMakeLists.txt from your project and use add_library. The CMake documentation might be helpful.
I misunderstood the question, target_link_library is probably the answer to the question.