C / cmake - 在TARGET_LINK_LIBRARIES中指定库时,如何向(未使用的)库添加链接器标志? [英] C/cmake - how to add a linker flag to an (unused) library when the library is specified in TARGET_LINK_LIBRARIES?
问题描述
在我的项目的根目录,我有一个子目录 my_lib
和另一个 my_app
。
库 my_lib
定义填充由链接器定义的段的表,这些表不直接由 my_app $ c $
要强制my_lib链接我添加了标记--whole-archive,如下所述这里。
在根目录的CMakelist.txt中有以下内容:
SET(CMAKE_EXE_LINKER_FLAGS-mmcu = cc430f6137 -Wl, - gc-sections -Wl, - 整个档案-MY_LIB -Wl, - 无整个档案)
ADD_SUBDIRECTORY(my_lib)
在 CMakelist.txt
my_lib
我有:
ADD_LIBRARY(MY_LIB
my_lib.c
)
TARGET_LINK_LIBRARIES(MY_LIB)
在 CMakelist.txt
of my_app
我有:
code> ADD_EXECUTABLE(my_app my_app.c)
TARGET_LINK_LIBRARIES(my_app MY_LIB)
我的问题是我只想使用这个标志( - 整个归档
)如果 MY_LIB
code> my_app
CMakelist.txt
中的code> TARGET_LINK_LIBRARIES 如果最后一行 TARGET_LINK_LIBRARIES(my_app MY_LIB)
不存在,我不想添加 CMAKE_EXE_LINKER_FLAGS
中创建> - Wl, - 整个文档-lMY_LIB -Wl, - 没有整个归档 >
我试图从root的CMakelist.txt中删除此标志,并将以下内容添加到 CMakelist.txt
中 my_lib
子目录:
SET_TARGET_PROPERTIES(MY_LIB PROPERTIES CMAKE_EXE_LINKER_FLAGS-Wl, - whole -archive -lMY_LIB -Wl, - no-whole-archive)
。
我如何做到这一点?
CMake命令 target_link_libraries 允许在链接给定目标时指定库和标志。而不是直接在 TARGET_LINK_LIBRARIES
调用中使用目标名 MY_LIB
,请使用包含引用 - 整个档案
和 - 无整个档案$ c $> c> flags:
ADD_LIBRARY(MY_LIB
my_lib.c
)
SET(MY_LIB_LINK_LIBRARIES -Wl, - 整个存档MY_LIB -Wl, - 无整个存档)
...
ADD_EXECUTABLE(my_app my_app.c)
TARGET_LINK_LIBRARIES(my_app $ {MY_LIB_LINK_LIBRARIES})
In the root directory of my project, I have a subdirectory for my_lib
and another for my_app
.
The library my_lib
defines tables that populates a section defined by the linker, these tables are not used directly by my_app
, so this library is not linked.
To force my_lib to be linked I added the flag --whole-archive as described here.
And it works!
In the CMakelist.txt of the root directory I have the following:
SET(CMAKE_EXE_LINKER_FLAGS "-mmcu=cc430f6137 -Wl,--gc-sections -Wl,--whole-archive -lMY_LIB -Wl,--no-whole-archive")
ADD_SUBDIRECTORY(my_lib)
In the CMakelist.txt
of my_lib
I have:
ADD_LIBRARY(MY_LIB
my_lib.c
)
TARGET_LINK_LIBRARIES(MY_LIB)
In the CMakelist.txt
of my_app
I have:
ADD_EXECUTABLE(my_app my_app.c)
TARGET_LINK_LIBRARIES(my_app MY_LIB)
My problem is I just want to use this flag (--whole-archive
) if MY_LIB
is specified in the TARGET_LINK_LIBRARIES
in CMakelist.txt
of my_app
.
If the the last line TARGET_LINK_LIBRARIES(my_app MY_LIB)
is not there, I don't want to add "-Wl,--whole-archive -lMY_LIB -Wl,--no-whole-archive"
in the CMAKE_EXE_LINKER_FLAGS
.
I tried to remove this flag from the CMakelist.txt in root and add the following into the CMakelist.txt
in my_lib
subdirectory:
SET_TARGET_PROPERTIES(MY_LIB PROPERTIES CMAKE_EXE_LINKER_FLAGS "-Wl,--whole-archive -lMY_LIB -Wl,--no-whole-archive")
But this does not work.
How can I do this?
The CMake command target_link_libraries allows for specifying both libraries and flags when linking a given target. Instead of directly using the target name MY_LIB
in the TARGET_LINK_LIBRARIES
call, use a variable that wraps the reference to MY_LIB
with the --whole-archive
and --no-whole-archive
flags:
ADD_LIBRARY(MY_LIB
my_lib.c
)
SET(MY_LIB_LINK_LIBRARIES -Wl,--whole-archive MY_LIB -Wl,--no-whole-archive)
...
ADD_EXECUTABLE(my_app my_app.c)
TARGET_LINK_LIBRARIES(my_app ${MY_LIB_LINK_LIBRARIES})
这篇关于C / cmake - 在TARGET_LINK_LIBRARIES中指定库时,如何向(未使用的)库添加链接器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!