如何在应该失败的CMake中实现编译测试
How to implement compilation test in CMake which should fail?
我正在研究一个长整数库作为我C++家庭作业,我们的老师提供了其接口使用的示例。这是此文件的一部分:
void test_conversions()
{
int ordinary = 42;
lint long_int(ordinary);
long_int = ordinary;
ordinary = static_cast<int>(long_int); // ok
std::string s("-15");
lint z(s);
z.to_string() == s; // this should be true
}
void failed_conversions_test()
{
int ordinary = 42;
std::string str = "-42";
lint x = 5;
ordinary = x; // must be compilation error!
x = str; // must be compilation error!
str = x; // must be compilation error!
}
我想在构建过程中测试此文件的兼容性。如您所见,应该有四个测试:一个用于编译成功(test_conversions
),三个用于failed_conversions_test
中呈现的每个失败。我可以通过添加存根int main()
并在CMakeLists.txt
中调用ADD_TEST
来轻松实现编译成功检查,但是如何告诉CMake编译其余三个测试并确保编译不成功?
我正在考虑添加类似"运行执行所有操作的自定义脚本"之类的内容,但这非常依赖于编译器和平台,看起来不是好方法。
谢谢。
try_compile
命令可能是您要查找的。
您可以使用它来尝试构建单个源文件,并将结果报告回 CMake:
try_compile(COMPILE_SUCCEEDED ${CMAKE_BINARY_DIR}/compile_tests my_test_src.cpp)
if(COMPILE_SUCCEEDED)
message("Success!")
endif()
此功能背后的动机是测试编译器功能,因此它只会在 CMake 配置时执行,而不是在构建时执行。如果您仍然想要后者,则需要求助于带有脚本的自定义构建步骤。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如果没有malloc,链表实现将失败
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用CMake创建QML插件
- 如何在c++中实现处理器调度模拟器
- cmake如何在fedora工作站中找到boost静态库包
- 使用CMake检测支持的C++标准
- 如何在c++中使用引用实现类似python的行为
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 实现无开销push_back的最佳方法是什么
- 如何使用CMake编译.proto文件来生成.grpcp.pb.cc和.grpc.pb.h文件
- cmake在我的项目中所需的所有静态库都不成功
- 使用简单类型列表实现的指数编译时间.为什么
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- 在 CMake 中实现条件强制
- 带有AUTOMOC的CMake正在删除我的实现
- 如何在应该失败的CMake中实现编译测试
- gtest 仅在该方法在 .h 中实现时检测方法,而不是在 .cpp 中实现(cMake;问题发生在"make"之后)