0

It is my First time to use Boost lib, So of course I ran into problem:

First this is the CMAKELISTS.txt for my project:

    ===========================================
    src/CMAKELISTS.txt:
    =========================================== 
    cmake_minimum_required(VERSION 3.15)
    project(My_String)

    set(CMAKE_CXX_STANDARD 17)
    set(SOURCE_FILES MyString.cpp MyString.h main.cpp)

    add_executable(My_String_src ${SOURCE_FILES})
    ===========================================
    test/CMAKELISTS.txt:
    ===========================================
    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_INCLUDE_DIR "C:\\Program Files\\Boost\\boost_1_71_0")
    set(Boost_LIBRARIES "C:\\Program Files\\Boost\\boost_1_71_0")

    find_package (Boost 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_LIBRARIES})
    ==============================================
    top_level CMAKELISTS.txt for the whole project:
    ==============================================
    cmake_minimum_required(VERSION 3.15)
    project(My_String)

    set(CMAKE_CXX_STANDARD 17)

    add_subdirectory(src)
    add_subdirectory(test)

Note: I have built the BOOST lib!enter code here In MyStringTest.cpp I have:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE MyString_Test_Suite

#include <iostream>
#include "MyString.h"
#include "MyString.cpp"
#include <boost/test/unit_test.hpp>

The error that I get when I build the project:

    [ 50%] Building CXX object test/CMakeFiles/Boost_Tests_run.dir/MyStringTest.cpp.obj
    [100%] Linking CXX executable Boost_Tests_run.exe
    C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\ar.exe: unable to rename 'CMakeFiles\Boost_Tests_run.dir/objects.a'; reason: File exists
    mingw32-make.exe[3]: *** [test\CMakeFiles\Boost_Tests_run.dir\build.make:86: test/Boost_Tests_run.exe] Error 1
    mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:141: test/CMakeFiles/Boost_Tests_run.dir/all] Error 2
    mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:148: test/CMakeFiles/Boost_Tests_run.dir/rule] Error 2
    mingw32-make.exe: *** [Makefile:130: Boost_Tests_run] Error 2

What is wrong with my configurations? And what do i need to do to fix it?

Amjad Alissa
  • 111
  • 1
  • 6
  • Does this answer your question? [How to link C++ program with Boost using CMake](https://stackoverflow.com/questions/3897839/how-to-link-c-program-with-boost-using-cmake) See also [cmake doc](https://cmake.org/cmake/help/v3.15/module/FindBoost.html). – Marek R Dec 04 '19 at 13:28

1 Answers1

0

The code is telling the linker to link this library ../src to your executable. This is an invalid operation, so remove this line:

target_link_libraries(Boost_Tests_run ../src)

The arguments to target_link_libraries() are reserved for targets and library names (including full paths to library files). The ../src argument does not fit either of those criteria, as it is simply a directory.


EDIT: Another potential issue is your Boost_LIBRARIES variable. Again, this should not be provided to target_link_libraries() if it just contains a directory path. It should instead contain the full path to the libraries, including the library names.

However, the modern FindBoost.cmake module provides a better approach. If you know the Boost components you want to use (e.g. filesystem), you should specify those components in your find_package() call:

find_package(Boost REQUIRED COMPONENTS filesystem)

Then, the find module will populate imported targets for you, and you can link to them like this instead:

target_link_libraries(Boost_Tests_run PUBLIC Boost::filesystem)

I suggest taking a look at some of the examples on this page.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • After removing the line that you suggested to remove, I get this error when bulding the project: [ 60%] Built target My_String_src [ 80%] Linking CXX executable Boost_Tests_run.exe C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\ar.exe: unable to rename 'CMakeFiles\Boost_Tests_run.dir/objects.a'; reason: File exists mingw32-make.exe[2]: *** [test\CMakeFiles\Boost_Tests_run.dir\build.make:86: test/Boost_Tests_run.exe] Error 1 mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:141: test/CMakeFiles/Boost_Tests_run.dir/all] Error 2 mingw32-make.exe: *** [Makefile:83: all] Error 2 – Amjad Alissa Dec 04 '19 at 15:29
  • @AmjadAlissa I expanded my answer. Like in the first answer I posted, you **cannot** simply use directories in calls to `target_link_libraries()`, they must be actual libraries. I hope this is helpful. – Kevin Dec 04 '19 at 16:47
  • thank you very much for your answers! It is the first time i use boost and i really spent alot of time trying to figure out how to make the project work but with no success so far. Again I did exactly as you said, I modified the cmakelists.txt to be like: find_package(Boost REQUIRED COMPONENTS unit_test_framework) 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") – Amjad Alissa Dec 04 '19 at 16:55