gpt4 book ai didi

c++ - 找不到 Boost(缺少 : unit_test_framework) (found version "1.71.0")

转载 作者:行者123 更新时间:2023-12-02 01:30:16 24 4
gpt4 key购买 nike

我需要使用 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管理器https://github.com/microsoft/vcpkg所以我尝试了一下,我遇到了几个问题,但之后它起作用了,所以我所做的是:

1.安装vcpkg https://github.com/microsoft/vcpkg (按照说明操作)您在设置时可能会遇到错误( fatal error ,无法执行解压后操作将"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>

经过一个多星期的谷歌搜索、反复试验,这终于对我有用了!如果有人遇到同样的问题,希望它能有所帮助。感谢Real Fresh的支持!

关于c++ - 找不到 Boost(缺少 : unit_test_framework) (found version "1.71.0"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59189573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com