简体   繁体   中英

Configuring Boost Library in CLion

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?

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 (eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM