I have a demo project and it's structure
like as below:
top_dir
CMakeLists.txt
sub_dir1
CMakeLists.txt
sub_dir2
CMakeLists.txt
top_dir/sub_dir1/CMakeLists.txt
used to build lib1
by using add_library(lib1 ...)
,
top_dir/sub_dir2/CMakeLists.txt
used to build exe1
with linking lib1
by target_link_library(exe1 lib1)
.
And the content of top_dir/CMakeLists.txt is as below:
add_subdirectory(sub_dir2)
add_subdirectory(sub_dir1)
Normally, when build target exe1
, cmake will check dependency
so lib1
will be built before building exe1
. The problem is I am transfering an existed makefile project into CMake, and there are many gcc link options
, like "whole-archive ... no-whole-archive, allow-mutiple-definition"
, if use like target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a --no-whole-archive")
(The form like this, and this may not work, it just a e.g.), cmake seem don't built lib1
any more. Is there any way i can use target_link_library
like target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a")
and cmake link dependency checking still work, or other way i can transfer these gcc link options into cmake?
The next hack permits to locally define the flags to the library to which you want to apply the flags, without modifying all exe link flags :
Arguments for
target_link_libraries
are going into the resulted command line in the same order they appears. Whenever target name is used as argument, path to target's output is used in resulted command line. So, you may use library target whenever you need path to that library in the command line:Such a way a target-level dependency between executable exe1 and library lib1 is automatically deduced by CMake, as usual.