CMake:如何在CMakeLists.txt中设置LDFLAGS ?

时间:2021-08-21 12:41:30

I set the CFLAGS in CMake by CMAKE_C_FLAGS. Is something like this to set LDFLAGS?

我用CMAKE_C_FLAGS设置CMake中的CFLAGS。像这样设置LDFLAGS吗?

5 个解决方案

#1


43  

It depends a bit on what you want:

这取决于你想要什么:

A) If you want to specify which libraries to link to, you can use find_library to find libs and then use link_directories and target_link_libraries to.

A)如果您想指定要链接到哪个库,可以使用find_library查找libs,然后使用link_directory和target_link_libraries。

Of course, it is often worth the effort to write a good find_package script, which nicely adds "imported" libraries with add_library( YourLib IMPORTED ) with correct locations, and platform/build specific pre- and suffixes. You can then simply refer to 'YourLib' and use target_link_libraries.

当然,编写一个良好的find_package脚本通常是值得的,它很好地添加了带有正确位置的add_library(YourLib导入)的“导入”库,以及平台/构建特定的前缀和后缀。然后可以简单地引用“YourLib”并使用target_link_libraries。

B) If you wish to specify particular linker-flags, e.g. '-mthreads' or '-Wl,--export-all-symbols' with MinGW-GCC, you can use CMAKE_EXE_LINKER_FLAGS. There are also two similar but undocumented flags for modules, shared or static libraries:

B)如果您希望指定特定的链接标志,例如“-mthreads”或“-Wl,- export-all-symbols”与MinGW-GCC一起,您可以使用CMAKE_EXE_LINKER_FLAGS。对于模块、共享或静态库,也有两种类似的但没有文档记录的标志:

CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

#2


19  

Look at:

看:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

#3


5  

You can specify linker flags in target_link_libraries.

您可以在target_link_libraries中指定链接器标志。

#4


3  

For linking against libraries see Andre's answer.

对于链接库,请参阅安德烈的答案。

For linker flags - the following 4 CMake variables:

对于链接器标记—以下4个CMake变量:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

can be easily manipulated for different configs (debug, release...) with the ucm_add_linker_flags macro of ucm

可以很容易地使用ucm的ucm_add_linker_flags宏来处理不同的configs (debug, release…)

#5


3  

If you want to add a flag to every link, e.g. -fsanitize=address then I would not recommend using CMAKE_*_LINKER_FLAGS. Even with them all set it still doesn't use the flag when linking a framework on OSX, and maybe in other situations. Instead use link_libraries():

如果您想为每个链接添加一个标志,例如-fsanitize=address,那么我将不推荐使用CMAKE_*_LINKER_FLAGS。即使它们都设置好了,在连接OSX框架和其他情况时仍然不使用标记。而不是使用link_libraries():

add_compile_options("-fsanitize=address")
link_libraries("-fsanitize=address")

This works for everything.

这是为我所做的一切。

#1


43  

It depends a bit on what you want:

这取决于你想要什么:

A) If you want to specify which libraries to link to, you can use find_library to find libs and then use link_directories and target_link_libraries to.

A)如果您想指定要链接到哪个库,可以使用find_library查找libs,然后使用link_directory和target_link_libraries。

Of course, it is often worth the effort to write a good find_package script, which nicely adds "imported" libraries with add_library( YourLib IMPORTED ) with correct locations, and platform/build specific pre- and suffixes. You can then simply refer to 'YourLib' and use target_link_libraries.

当然,编写一个良好的find_package脚本通常是值得的,它很好地添加了带有正确位置的add_library(YourLib导入)的“导入”库,以及平台/构建特定的前缀和后缀。然后可以简单地引用“YourLib”并使用target_link_libraries。

B) If you wish to specify particular linker-flags, e.g. '-mthreads' or '-Wl,--export-all-symbols' with MinGW-GCC, you can use CMAKE_EXE_LINKER_FLAGS. There are also two similar but undocumented flags for modules, shared or static libraries:

B)如果您希望指定特定的链接标志,例如“-mthreads”或“-Wl,- export-all-symbols”与MinGW-GCC一起,您可以使用CMAKE_EXE_LINKER_FLAGS。对于模块、共享或静态库,也有两种类似的但没有文档记录的标志:

CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

#2


19  

Look at:

看:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

#3


5  

You can specify linker flags in target_link_libraries.

您可以在target_link_libraries中指定链接器标志。

#4


3  

For linking against libraries see Andre's answer.

对于链接库,请参阅安德烈的答案。

For linker flags - the following 4 CMake variables:

对于链接器标记—以下4个CMake变量:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

can be easily manipulated for different configs (debug, release...) with the ucm_add_linker_flags macro of ucm

可以很容易地使用ucm的ucm_add_linker_flags宏来处理不同的configs (debug, release…)

#5


3  

If you want to add a flag to every link, e.g. -fsanitize=address then I would not recommend using CMAKE_*_LINKER_FLAGS. Even with them all set it still doesn't use the flag when linking a framework on OSX, and maybe in other situations. Instead use link_libraries():

如果您想为每个链接添加一个标志,例如-fsanitize=address,那么我将不推荐使用CMAKE_*_LINKER_FLAGS。即使它们都设置好了,在连接OSX框架和其他情况时仍然不使用标记。而不是使用link_libraries():

add_compile_options("-fsanitize=address")
link_libraries("-fsanitize=address")

This works for everything.

这是为我所做的一切。