CMake- 'target_link_options(.. -lgcov)' 和 'target_link_libraries(...gcov)' 有什么区别?
CMake- what is the difference between 'target_link_options(.. -lgcov)' and 'target_link_libraries(...gcov)'?
我正在尝试构建一个启用代码覆盖率的库,如下所示:
add_library(my_library my_lib.cpp)
target_compile_options(my_library PRIVATE --coverage)
target_link_options(my_library PRIVATE -lgcov)
然后我对图书馆进行了一些测试:
add_executable(my_test my_test.cpp)
target_link_libraries(my_test my_library)
但是我在构建测试时遇到 link 错误-
libmy_library.a(my_lib.cpp.o): In function `_GLOBAL__sub_I_00100_0__ZN7myClass10returnTrueEv':
my_lib.cpp:(.text+0x2f): undefined reference to `__gcov_init'
libmy_library.a(my_lib.cpp.o): In function `_GLOBAL__sub_D_00100_1__ZN7myClass10returnTrueEv':
my_lib.cpp:(.text+0x3a): undefined reference to `__gcov_exit'
libmy_library.a(my_lib.cpp.o):(.data.rel+0x20): undefined reference to `__gcov_merge_add'
如果不使用 target_link_options
我使用 target_link_libraries
-
target_link_libraries(my_library PRIVATE gcov)
那么我就没有得到错误。
造成行为差异的原因是什么?我认为 target_link_libraries(my_library PRIVATE gcov)
等同于 target_link_options(my_library PRIVATE -lgcov)
.
答案就在那里,在 docs:
Note This command cannot be used to add options for static library
targets, since they do not use a linker. To add archiver or MSVC
librarian flags, see the STATIC_LIBRARY_OPTIONS target property.
我正在尝试构建一个启用代码覆盖率的库,如下所示:
add_library(my_library my_lib.cpp)
target_compile_options(my_library PRIVATE --coverage)
target_link_options(my_library PRIVATE -lgcov)
然后我对图书馆进行了一些测试:
add_executable(my_test my_test.cpp)
target_link_libraries(my_test my_library)
但是我在构建测试时遇到 link 错误-
libmy_library.a(my_lib.cpp.o): In function `_GLOBAL__sub_I_00100_0__ZN7myClass10returnTrueEv':
my_lib.cpp:(.text+0x2f): undefined reference to `__gcov_init'
libmy_library.a(my_lib.cpp.o): In function `_GLOBAL__sub_D_00100_1__ZN7myClass10returnTrueEv':
my_lib.cpp:(.text+0x3a): undefined reference to `__gcov_exit'
libmy_library.a(my_lib.cpp.o):(.data.rel+0x20): undefined reference to `__gcov_merge_add'
如果不使用 target_link_options
我使用 target_link_libraries
-
target_link_libraries(my_library PRIVATE gcov)
那么我就没有得到错误。
造成行为差异的原因是什么?我认为 target_link_libraries(my_library PRIVATE gcov)
等同于 target_link_options(my_library PRIVATE -lgcov)
.
答案就在那里,在 docs:
Note This command cannot be used to add options for static library targets, since they do not use a linker. To add archiver or MSVC librarian flags, see the STATIC_LIBRARY_OPTIONS target property.