c++ - 测试链接对于使用 CMake 的 C++ 静态库是正确的
问题描述
我有一个构建静态库的 CMake 项目,它们将链接到其他几个 C++ 项目以增加代码重用。
该项目尚不包含任何测试,我想开始添加它并尽快增加测试代码覆盖率。这将涉及大量工作,因为代码不是非常模块化,并且可能需要几个月的时间才能达到我认为可以接受的水平。
我相信会改进测试的一个快速胜利是检查这些静态库是否正确链接到他们正在使用的第三方库。为此,我使用 CMake 将这些静态库链接到测试可执行文件中。
# Creating static library
# -------
add_library(StaticLib STATIC
${STATICLIB_SRC}
${STATICLIB_HEADERS}
)
target_link_libraries(StaticLib ${THIRD_PARTY_LIBRARIES})
target_include_directories(StaticLib
PUBLIC
${THIRD_PARTY_INCLUDES})
# -------
# Creating test executable, test using Boost
# -------
#Add compile target
add_executable(LibTest test.cpp)
# Add include directories
target_include_directories(LibTest
PRIVATE
${BOOST_INCLUDES}
$<TARGET_PROPERTY:StaticLib,INTERFACE_INCLUDE_DIRECTORIES>
)
# Link to dependencies
target_link_libraries(LibTest
${BOOST_LIBS}
StaticLib
)
# -------
但是,我注意到仅链接它并不会检查静态库中所有丢失的符号。它只会检查测试需要链接到的目标文件中是否缺少符号。
比如这个test.cpp如果没有正确链接第三方库就会给出链接时错误,因为Foo不能被实例化:
// This is test.cpp
// The below will cause a link time error, if third party libraries are not linked correctly
BOOST_AUTO_TEST_CASE(FooTest)
{
Foo f = Foo(); // Defined in StaticLib and uses third party libraries
BOOST_TEST_MESSAGE( "passed" );
}
但是,这不会产生错误:
// This is test.cpp
// The below will *not* cause a link time error, if third party libraries are not linked correctly
BOOST_AUTO_TEST_CASE(FooTest)
{
// No Foo being instantiated
BOOST_TEST_MESSAGE( "passed" );
}
有没有办法检查静态库中所有目标文件的符号是否存在,而不必编写覆盖它们的测试代码?
解决方案
推荐阅读
- android - 我的安卓手机内置短信应用程序怎么这么快?
- node.js - 如何使用pureimage画一条线?
- java - 创建没有 JOINS 的 Spring Data Jpa Specification 方法
- spring - Spring 的 findByColumnName 返回空列表
- sql - Firebird中的条件唯一索引
- android - 使用加速度计和磁场传感器寻找朝拜方向的逻辑
- android - 如何在 FloatingActionButton/Button Click 事件中使用协程
- django - Heroku 无法检测到我的 forms.py。“模块未找到错误在 /”
- python - Altair 中的聚合函数块图交互性
- jquery - 通过 Ajax 向 Laravel 控制器提交带有文本和图像的表单