【发布时间】:2018-03-21 18:19:34
【问题描述】:
从Catch2's example 开始,我尝试使用cmake 运行此示例,其中我的项目结构如下:
/factorial
+-- CMakeLists.txt
+-- /bin
+-- /include
| +-- catch.hpp
| +-- fact.hpp
+-- /src
| +-- CMakeLists.txt
| +-- fact.cpp
+-- /test
+-- CMakeLists.txt
+-- test_fact.cpp
fact.cpp:
unsigned int factorial( unsigned int number ) {
return number <= 1 ? number : factorial(number-1)*number;
}
fact.hpp:
#ifndef FACT_H
#define FACT_H
unsigned int factorial(unsigned int);
#endif
test_fact.cpp:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "fact.hpp"
TEST_CASE( "factorials are computed", "[factorial]" ) {
REQUIRE( factorial(1) == 1 );
REQUIRE( factorial(2) == 2 );
REQUIRE( factorial(3) == 6 );
REQUIRE( factorial(10) == 3628800 );
}
我已经尝试了几种方法来使用cmake 构建这个项目,但都失败了。有时我得到一个错误:
cpp:X:XX: fatal error: 'fact.hpp' file not found
...
有时我得到:
Undefined symbols for architecture x86_64:
"_main", referenced from:
...
当我运行make。
如果我想在factorial/bin 中保存我的执行文件,我应该在factorial/CMakeLists.txt、factorial/src/CMakeLists.txt 和factorial/test/CMakeLists.txt 中有什么?
补充: 这是我的 CMakeLists.txts(我认为它们完全错误)。
factorial/CMakeLists.txt:
project(factorial)
cmake_minimum_required(VERSION 2.8.12)
add_definitions("-std=c++11")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_subdirectory(src)
add_subdirectory(test)
factorial/src/CMakeLists.txt:
project(factorial)
cmake_minimum_required(VERSION 2.8.12)
add_executable(fact fact.cpp)
factorial/test/CMakeLists.txt:
project(factorial)
cmake_minimum_required(VERSION 2.8.12)
add_executable(test_fact test_fact.cpp)
target_include_directories(test_fact PRIVATE ${CMAKE_SOURCE_DIR}/include)
【问题讨论】:
-
你的 cmake 文件的内容是什么?
-
你查看过他们的最小示例here
-
您的 CMake 文件是否在任何地方包含
include_directories(include)? (或者至少是target_include_directories()) -
@GuillaumeRacicot 我正在写,请稍候。卡尔还没谢谢。
-
第二个问题,你可以这样做:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)。但是,我建议您不要这样做,而是将可执行文件保存在创建它们的目录中。
标签: c++ cmake catch-unit-test