c++ - Catch2 - 未定义的引用
问题描述
我正在使用 Catch2 作为库测试我的项目。我遵循了 Catch 文档中的每一步,但是当我运行测试时,我收到以下错误:
CMakeFiles/tests.dir/tests/IntegerIntervalTest.cpp.o: in function "____C_A_T_C_H____T_E_S_T____0()":
/home/davide/cpp-project/tests/IntegerIntervalTest.cpp:8: undefined reference to "domain::IntegerAbstractInterval<int, int>::IntegerAbstractInterval(int, int)"
并且对于测试“类”中的每个方法调用都会重复此错误。
CMakeLists:
PROJECT(cpp_project)
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-std=c++14 COMPILER_SUPPORTS_CXX14)
IF (COMPILER_SUPPORTS_CXX14)
ADD_COMPILE_OPTIONS("-std=c++14")
ELSE ()
MESSAGE(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} has no C++14 support.")
ENDIF ()
set(BASE_SRCS src/Bound.cpp src/Bound.hpp src/Infinity.cpp src/Infinity.hpp src/AbstractInterval.cpp src/AbstractInterval.hpp
src/UndefinedOperationException.hpp src/IntegerAbstractInterval.cpp src/IntegerAbstractInterval.hpp src/FloatingPointAbstractInterval.cpp
src/FloatingPointAbstractInterval.hpp)
ADD_COMPILE_OPTIONS("-Wall" "-Wextra" "-Wpedantic" "-Werror" )
ADD_LIBRARY(cpp-project ${BASE_SRCS})
INCLUDE_DIRECTORIES(libs/variant/include)
set(EXECUTABLE_OUTPUT_PATH bin)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/catch)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
set(TEST_SRCS tests/InfinityTest.cpp tests/IntegerIntervalTest.cpp)
add_executable(tests ${BASE_SRCS} ${TEST_SRCS})
target_link_libraries(tests Catch)
这是测试文件IntegerIntervalTest.cpp
:
#include "../src/IntegerAbstractInterval.hpp"
#include "../libs/catch2/catch.hpp"
using namespace domain;
TEST_CASE("Operations on IntegerInterval instances", "[IntegerAbstractInterval]") {
IntegerAbstractInterval<int, int> i(0,1);
IntegerAbstractInterval<Infinity, Infinity> ii(Infinity('-'), Infinity('+'));
IntegerAbstractInterval<Infinity, int> iii(Infinity('-'), 5);
IntegerAbstractInterval<int, Infinity> iv(0, Infinity('+'));
IntegerAbstractInterval<int, int> v(-1,1);
SECTION("Sum operations") {
auto res1 = i + v;
REQUIRE(res1.getLowerBound() == Bound(-1));
REQUIRE(res1.getUpperBound() == Bound(2));
}
}
解决方案
推荐阅读
- mysql - 如何在 octoberCMS 查询中组合多个 and or 条件?
- php - httpd.conf 会覆盖 php.ini 吗?
- r - R tmap:在 tmap_mode("view") 中绘制的多边形,但只有第一个观察值 ("plot")
- r - 如何对R中的“系列”向量进行向量化操作
- azure-devops - 使用微软主机代理执行构建防御时出现运行问题?
- python - TypeError:__init__() 缺少 2 个必需的位置参数:“no_of_arrows”和“email”
- python-3.x - 将 h:m:s 转换为分钟格式
- c# - 相对于文本框中的值更改按钮的颜色
- reactjs - 当 React 中的数据更改时,胜利图表不会更新
- python - 从字典列表创建键字符串