lookisrus.blogg.se

Cmake list
Cmake list





cmake list

In the subdirectory dependency_lib, we have the following CMakeLists.txt: project(dependency_lib) add_library(dependency_lib SHARED library.cpp) add_subdirectory(sub_dependency_lib) target_link_libraries(dependency_lib PRIVATE sub_dependency_lib)Ĭlearly, we have a hierarchy of dependencies. Let us consider, for example, the following code: project(main_project) add_executable(main_project main.cpp) add_subdirectory(dependency_lib) target_link_libraries(main_project PRIVATE dependency_lib) This is useful if you want to carry over a dependency from a child target to the parent. In modern CMake, target commands let you specify the command scope using INTERFACE, PRIVATE and PUBLIC keywords. Use Target Transitivity to Specify Dependency Hierarchies Furthermore, these commands let us optionally specify the scope to achieve the desired inheritance behavior (see next tip). On the other hand, using target_include_directories or target_link_libraries, we explicitly specify the target we want to use, avoiding any leakage problems.

cmake list

Especially with commands such as add_definitions or add_compile_options, this may cause some hard-to-find compilation errors. The same also applies to the other commands. Thus, we might end up with a scenario of using the wrong include files in dependency_lib. Unfortunately, since the directories specified in include_directories are appended to the list of all targets in the current CMakeLists.txt file, they will also be appended to dependency_lib. For instance, if you want to build an executable target that additionally depends on a library defined in a subdirectory, you could specify it like this: project(main_project) add_executable(main_project main.cpp) add_subdirectory(dependency_lib) include_directories(include_files) Since the functions without target_ do not specify a scope, they will leak to all targets specified in the directory. However, whenever possible, you should prefer using their counterparts target_compile_definitions, target_include_directories, target_sources, or target_link_libraries instead. Always Use target_*() Commandsįunctions like add_definitions, include_directories, link_libraries, and others are still around in modern CMake due to backward compatibility. While you may already be familiar with some of them, I am sure you will find some useful ones as well. In this post, I would like to introduce you to some tips that might help you write better CMake scripts. Since it was introduced 21 years ago, it came a long way and added support for many great features, gaining popularity among C++ developers. It is widely used for multi-platform development and supports generating build files for most C++ compilers. CMake has become the de facto standard tool for C++build automation, testing, and packaging.







Cmake list