c++ - CMake- 'target_link_options(.. -lgcov)' 和 'target_link_libraries(...gcov)' 之间有什么区别?

标签 c++ cmake 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)

但是我在构建测试时遇到链接错误-

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.

关于c++ - CMake- 'target_link_options(.. -lgcov)' 和 'target_link_libraries(...gcov)' 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71384930/

相关文章:

C++ - 从注册表读取的值中获取空值

c++ - gdb 列表错误 "No such file or directory"

c++ - 如何将值列表传递给需要数组的函数?

c++ - CMake - 尝试构建项目时遇到 undefined reference

c++ - 使用 CLion、CMake 和 VS2017 编译器编译 C++17

c++ - 没有制定目标 'TBB_ENV_LIB_DEBUG-NOTFOUND' 的规则,'lib/libopencv_core.so.3.2.0 需要

c++ - 使用 CXX 社区插件在 Sonarqube-5.6.6(LTS) 中导入 Gcov 报告

C++:我需要目录导航方面的帮助

c++ - 是否可以在测试后检查代码覆盖率是否增加