0

我有一个非常简单的项目结构,我无法使用 CMake 进行编译。我试图阅读有关 CMake 或教程的文档,但我无法让它工作。

有一个类似的问题,但即使尝试了答案所暗示的内容,我也无法让它发挥作用。

你可以在这里看到我的完整代码

但相关的 CMakeLists 是:

根级别:

cmake_minimum_required(VERSION 3.5)

find_package(Catch2 REQUIRED)

project(majorityQueries LANGUAGES CXX VERSION 0.0.1)

include_directories(include)

add_subdirectory(src)
add_subdirectory(tests)

file(GLOB SOURCES "*.cpp")

源代码:

add_library(fact factorial.cpp)

测试:

add_executable(test test_factorial.cpp)

target_link_libraries(test Catch2::Catch2)

但基本上我有一个 test_factorial.cpp 文件,其中包含标头 factorial.hpp (在包含目录中),因此应该知道 Factorial(int) 函数的存在,但它说它未定义。

我尝试的是:

cd build/
cmake ..
make

我希望make工作,而不是我得到:

Undefined symbols for architecture x86_64:
  "Factorial(int)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test_factorial.cpp.o
  "Catch::NameAndTags::NameAndTags(Catch::StringRef const&, Catch::StringRef const&)", referenced from:
      ___cxx_global_var_init in test_factorial.cpp.o
  "Catch::StringMaker<int, void>::convert(int)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::stringify<int>(int const&) in test_factorial.cpp.o (...)
4

1 回答 1

0

正如我在评论中已经写的那样,将源文件test_main.cpp和库添加fact./tests/CMakeLists.txt.

add_executable(test test_main.cpp test_factorial.cpp)
target_link_libraries(test fact Catch2::Catch2)
于 2019-07-15T07:51:54.620 回答