7

I am need to use boost unit_test_framework and I am using Cmake command to find it:

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)

I get this error:

Could NOT find Boost (missing: unit_test_framework) (found version
  "1.71.0")

What can I do to fix it?

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Amjad Alissa
  • 111
  • 1
  • 6
  • 1
    I assume you should specify the directory where the boost libraries (.lib or .dll files for MSVC compiler) lie by assigning the path string to the `BOOST_LIBRARYDIR` variable. – Deedee Megadoodoo Dec 05 '19 at 07:38
  • 2
    CMake cannot find `unit_test_framework` library in your Boost installation. Run `cmake` with additional `-DBoost_DEBUG=ON` option: with that option CMake will print **exact library name**, corresponded to `unit_test_framework`, and **exact directories** where it searches this library. Then try to find this library under the directory you specified as `BOOST_ROOT`. – Tsyvarev Dec 05 '19 at 07:49
  • `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") set(BOOST_LIBRARY_DIR "C:\\Program Files\\Boost\\boost_1_71_0\\stage\\lib") 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)` Still the same error! – Amjad Alissa Dec 05 '19 at 10:46
  • CMake Error at C:/Users/aliss/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.5233.103/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find Boost (missing: unit_test_framework) (found version "1.71.0") – Amjad Alissa Dec 05 '19 at 10:47
  • As an alternative you can use library manager, vcpkg, for example. It's easy to install and use (you have just to specify the cmake toolchain file path before the project generation, read more about it here: https://github.com/microsoft/vcpkg). – Deedee Megadoodoo Dec 06 '19 at 04:06
  • 1
    What I don't get is how it CMAKE can say `Could NOT find Boost`, and then turn around and report `found version "X"`. It both can **and** cannot find boost?! – Mark Storer Apr 23 '20 at 19:29
  • Maybe unrelated, but Boost 1.71 is supported by CMake 3.15.3, not 3.15.2 or below, see https://stackoverflow.com/a/42124857/2799037 – usr1234567 Sep 14 '22 at 20:55

2 Answers2

4

As Real Fresh suggested to use vcpkg mananger https://github.com/microsoft/vcpkg And So I gave it a try, I faced couple of issues but after that It worked so what I did is:

1.install vcpkg https://github.com/microsoft/vcpkg (Follow the instructions) You migh have the error when setting up (Fatal error, could not do post-extract operation renaming 'File' to 'Different name' ) You need to rename the manually.

2.install boost-test lib (you will see instruction on how to install packages) on the home page of vcpkg

3.you need to set these variables for 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.in your test/cmakelists.txt file you need to add the following:

find_package (Boost REQUIRED COMPONENTS unit_test_framework)
target_link_libraries (your_test_exe Boost::unit_test_framework)

in my case I have my test/cmakelists.txt like this:

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.In your test.cpp you need to include:

#include <boost/test/included/unit_test.hpp> as it is!
in addition to the classes you are testing.

in my case it looks like this:

#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>

After more than a week of googling, trial and error this finally worked for me! Hope it helps if any one face the same issue. Thank you Real Fresh for your support!

Amjad Alissa
  • 111
  • 1
  • 6
1

I had resolved the same error message with apt-get install libboost-all-dev on my Ubuntu 20 machine.

BZKN
  • 1,499
  • 2
  • 10
  • 25