找不到Boost(缺少:unit_test_framework)(找到版本“1.71.0”)

问题描述 投票:0回答:3

我需要使用 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")

我可以做什么来修复它?

c++ boost cmake
3个回答
4
投票

正如 Real Fresh 建议使用 vcpkg 管理器 https://github.com/microsoft/vcpkg 所以我尝试了一下,我遇到了几个问题,但之后它起作用了,所以我所做的是:

1.安装vcpkg https://github.com/microsoft/vcpkg(按照说明操作) 您在设置时可能会遇到错误(致命错误,无法执行解压后操作,将“文件”重命名为“不同名称”),您需要手动重命名。

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>

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


2
投票

我在我的 Ubuntu 20 机器上使用

apt-get install libboost-all-dev
解决了相同的错误消息。


0
投票

编译gqrx同样的问题

安装libboost_test-devel

libboost_test-devel 中的 zypper 正在加载存储库数据... 读取已安装的包... 解决包依赖关系...

将安装以下 2 个新软件包: libboost_test-devel libboost_test1_86_0-devel

要安装 2 个新软件包。

包下载大小:24.5 KiB

软件包安装大小变化: | 将安装的软件包需要 19.5 KiB 19.5 KiB | - 0 B 由将被删除的软件包释放

后端:classic_rpmtrans 继续? [是/否/v/...?显示所有选项] (y): 检索:libboost_test1_86_0-devel-1.86.0-1.2.x86_64(主存储库(OSS))(1/2),16.8 KiB
正在检索:libboost_test1_86_0-devel-1.86.0-1.2.x86_64.rpm ................................ ...................................................... .....[完成(1.0 KiB/s)] 检索:libboost_test-devel-1.86.0-2.1.noarch(主存储库(OSS))(2/2),7.7 KiB
正在检索:libboost_test-devel-1.86.0-2.1.noarch.rpm ...................................... ...................................................... ........................[完成(3.8 KiB/s)]

检查文件冲突:................................................................ ...................................................... .......................................[完毕] (1/2) 安装:libboost_test1_86_0-devel-1.86.0-1.2.x86_64 ................................ ...................................................... ..................[完毕] (2/2) 安装:libboost_test-devel-1.86.0-2.1.noarch ................................ ...................................................... ........................[完毕] 运行事务后脚本................................................................ ...................................................... ................................[完成]

© www.soinside.com 2019 - 2024. All rights reserved.