C++ 在ubuntu 20.04上安装Catch2#include<;catch 2/catch.hpp>;
我正在尝试在ubuntu 20.04上安装 使用来自的指令 我就是这么做的:C++ 在ubuntu 20.04上安装Catch2#include<;catch 2/catch.hpp>;,c++,cmake,catch2,C++,Cmake,Catch2,我正在尝试在ubuntu 20.04上安装 使用来自的指令 我就是这么做的: $ git clone https://github.com/catchorg/Catch2.git $ cd Catch2 $ cmake -Bbuild -H. -DBUILD_TESTING=OFF $ sudo cmake --build build/ --target install 比它告诉我一切都好 但是: 当我尝试编译示例时:// main.cpp #define CATCH_CONFIG_MAIN
$ git clone https://github.com/catchorg/Catch2.git
$ cd Catch2
$ cmake -Bbuild -H. -DBUILD_TESTING=OFF
$ sudo cmake --build build/ --target install
比它告诉我一切都好
但是:
当我尝试编译示例时://
main.cpp
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) == 1);
// some more asserts..
CHECK(Fibonacci(5) == 8);
// some more asserts..
// now let's benchmark:
BENCHMARK("Fibonacci 20") {
return Fibonacci(20);
};
BENCHMARK("Fibonacci 25") {
return Fibonacci(25);
};
BENCHMARK("Fibonacci 30") {
return Fibonacci(30);
};
BENCHMARK("Fibonacci 35") {
return Fibonacci(35);
};
}
它输出这样的错误:catch2/catch.hpp:没有这样的文件或目录
提前谢谢
问题很简单:克隆catchorg/Catch2现在默认情况下会得到一个v3分支,其工作方式不同。最重要的变化是,它不再是单个标头,而且
catch2/catch.hpp
标头不再存在
您可以在配置和安装构建之前切换到v2分支,或者根据v3中的更改调整代码
要获取默认的main,请链接到Catch2::Catch2WithMain
target。Admin帮助了我
在第三次捕获中。我需要:
cmake_minimum_required(VERSION 3.5)
project(Catch2 LANGUAGES CXX)
add_executable(${PROJECT_NAME} main.cpp )
find_package(Catch2)
target_link_libraries(${PROJECT_NAME} Catch2::Catch2WithMain)
如果您只是链接Catch2::Catch2,您将无法获得默认的main,必须编写自己的main,并且您自己的main需要调用测试。见例
我理解,使用main时,它应该是这样的:
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch_all.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) == 1);
// some more asserts..
CHECK(Fibonacci(5) == 8);
// some more asserts..
// now let's benchmark:
BENCHMARK("Fibonacci 20") {
return Fibonacci(20);
};
BENCHMARK("Fibonacci 25") {
return Fibonacci(25);
};
BENCHMARK("Fibonacci 30") {
return Fibonacci(30);
};
BENCHMARK("Fibonacci 35") {
return Fibonacci(35);
};
}
int main( int argc, char* argv[] )
{
Catch::Session session; // There must be exactly one instance
// writing to session.configData() here sets defaults
// this is the preferred way to set them
int returnCode = session.applyCommandLine( argc, argv );
if( returnCode != 0 ) // Indicates a command line error
return returnCode;
// writing to session.configData() or session.Config() here
// overrides command line args
// only do this if you know you need to
int numFailed = session.run();
// numFailed is clamped to 255 as some unices only use the lower 8 bits.
// This clamping has already been applied, so just return it here
// You can also do any post run clean-up here
return numFailed;
}
#define CATCH_CONFIG_MAIN//这告诉CATCH提供MAIN()-只在一个cpp文件中执行此操作
#定义捕获配置启用基准测试
#包括
标准:uint64\u t斐波那契(标准:uint64\u t编号){
返回数<2?1:Fibonacci(数-1)+Fibonacci(数-2);
}
测试用例(“斐波那契”){
检查(斐波那契(0)=1);
//还有一些断言。。
检查(斐波那契(5)=8);
//还有一些断言。。
//现在让我们进行基准测试:
基准(“斐波那契20”){
返回斐波那契(20);
};
基准(“斐波那契25”){
返回斐波那契(25);
};
基准(“斐波那契30”){
返回斐波那契(30);
};
基准(“斐波那契35”){
返回斐波那契(35);
};
}
int main(int argc,char*argv[])
{
Catch::Session;//必须只有一个实例
//在此处写入session.configData()将设置默认值
//这是设置它们的首选方法
int returnCode=session.applyCommandLine(argc,argv);
if(returnCode!=0)//表示命令行错误
返回代码;
//在此处写入session.configData()或session.Config()
//重写命令行参数
//只有在你知道需要的时候才这样做
int numFailed=session.run();
//numFailed被钳制为255,因为某些Unice仅使用较低的8位。
//此夹紧已应用,因此只需将其返回此处
//你也可以在这里进行赛后清理
返回numFailed;
}
我知道没有文件。。。
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch_all.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) == 1);
// some more asserts..
CHECK(Fibonacci(5) == 8);
// some more asserts..
// now let's benchmark:
BENCHMARK("Fibonacci 20") {
return Fibonacci(20);
};
BENCHMARK("Fibonacci 25") {
return Fibonacci(25);
};
BENCHMARK("Fibonacci 30") {
return Fibonacci(30);
};
BENCHMARK("Fibonacci 35") {
return Fibonacci(35);
};
}
int main( int argc, char* argv[] )
{
Catch::Session session; // There must be exactly one instance
// writing to session.configData() here sets defaults
// this is the preferred way to set them
int returnCode = session.applyCommandLine( argc, argv );
if( returnCode != 0 ) // Indicates a command line error
return returnCode;
// writing to session.configData() or session.Config() here
// overrides command line args
// only do this if you know you need to
int numFailed = session.run();
// numFailed is clamped to 255 as some unices only use the lower 8 bits.
// This clamping has already been applied, so just return it here
// You can also do any post run clean-up here
return numFailed;
}