我需要使用 boost unit_test_framework 并且我正在使用 Cmake 命令来找到它:
cmake_minimum_required(VERSION 3.15)
project(My_String)
set(CMAKE_CXX_STANDARD 17)
set(Boost_USE_STATIC_LIBS OFF)
set(SOURCE_FILES MyStringTest.cpp)
set(BOOST_ROOT "C:\\Program Files\\boost\\boost_1_71_0")
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories(${Boost_INCLUDE_DIR})
include_directories(../src)
add_executable (Boost_Tests_run ${SOURCE_FILES})
target_link_libraries (Boost_Tests_run Boost::unit_test_framework)
我收到此错误:
Could NOT find Boost (missing: unit_test_framework) (found version
"1.71.0")
我能做些什么来修复它?
最佳答案
由于 Real Fresh 建议使用 vcpkg mananger https://github.com/microsoft/vcpkg
所以我试了一下,我遇到了几个问题,但在那之后它起作用了,所以我所做的是:
1.安装 vcpkg https://github.com/microsoft/vcpkg(按照说明操作)
您可能在设置时遇到错误(致命错误,无法执行将"file"重命名为“不同名称”的提取后操作)您需要手动重命名。
2.在vcpkg的主页上安装boost-test lib(您将看到有关如何安装软件包的说明)
3.你需要为cmake设置这些变量:
-DVCPKG_TARGET_TRIPLET=x86-windows (x86-windows in my case)
"-DCMAKE_TOOLCHAIN_FILE='root to vcpkg'/scripts/buildsystems/vcpkg.cmake" (in my case root to vcpkg = F:/Files/vcpkg
so the variable will be:
"-DCMAKE_TOOLCHAIN_FILE=F:/Files/vcpkg/scripts/buildsystems/vcpkg.cmake"
4.在您的 test/cmakelists.txt 文件中,您需要添加以下内容:
find_package (Boost REQUIRED COMPONENTS unit_test_framework)
target_link_libraries (your_test_exe Boost::unit_test_framework)
就我而言,我的 test/cmakelists.txt 是这样的:
cmake_minimum_required(VERSION 3.15)
project(My_String)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "--coverage" )
set(SOURCE_FILES MyStringTest.cpp)
set(Boost_DEBUG ON)
find_package (Boost REQUIRED COMPONENTS unit_test_framework)
include_directories(../Src)
add_executable (Boost_Tests_run ${SOURCE_FILES})
target_link_libraries (Boost_Tests_run Boost::unit_test_framework)
5.在您的 test.cpp 中,您需要包括:
#include <boost/test/included/unit_test.hpp> as it is!
in addition to the classes you are testing.
就我而言,它看起来像这样:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE MyString_Test_Suite
#include
#include "MyString.h"
#include "MyString.cpp"
#include <boost/test/included/unit_test.hpp>
经过一个多星期的谷歌搜索、反复试验,这终于对我有用了!
如果有人遇到同样的问题,希望它有所帮助。
感谢您的支持!
关于c++ - 找不到 Boost(缺少 : unit_test_framework) (found version "1.71.0"),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59189573/