Baffo rasta
Baffo rasta

Reputation: 165

Can't link Boost 1.63.0 through CMake

what I'm trying to do is as you can guess from the title to link Boost libraries through CMake (I'm working with CLion to write cross platform code, so I have no other chance). I am sure I built everything correctly cause when I use it inside Visual Studio it works with no problem at all. Here's my CMake code:

cmake_minimum_required(VERSION 3.7)
project(BoostHello)

set(BOOST_ROOT C:/boost_1.63.0)
find_package(BOOST 1.6.0 REQUIRED)
include_directories( ${Boost_INCLUDE_DIR} )
set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(BoostHello ${SOURCE_FILES})
target_link_libraries( BoostHello ${Boost_LIBRARIES} )

And here are my compilation errors:

"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" --build C:\Users\Admin\CLionProjects\BoostHello\cmake-build-debug --target all --     -j 8
Scanning dependencies of target BoostHello
[ 50%] Building CXX object CMakeFiles/BoostHello.dir/main.cpp.obj
[100%] Linking CXX executable BoostHello.exe
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:6: undefined reference to     `boost::filesystem::path::root_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:7: undefined reference to     `boost::filesystem::path::relative_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:8: undefined reference to `boost::filesystem::path::filename() const'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function     `_static_initialization_and_destruction_0':
C:/boost_1.63.0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function     `ZN5boost10filesystem11path_traits7convertEPKwS3_RNSt7__cxx1112basic_stringIcSt1    1char_traitsIcESaIcEEE':
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to     `boost::filesystem::path::codecvt()'
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to `boost::filesystem::path_traits::convert(wchar_t const*, wchar_t const*,     std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>     >&, std::codecvt<wchar_t, char, int> const&)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\BoostHello.dir\build.make:96: recipe for target 'BoostHello.exe'     failed
mingw32-make.exe[2]: *** [BoostHello.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/BoostHello.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/BoostHello.dir/all'     failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed

And eventually here's the code I'm trying to compile:

#include <iostream>
#include <boost/filesystem.hpp>

int main(int argc, char** argv) {
    boost::filesystem::path myPath =     {L"C:/Users/Admin/ClionProjects/BoostHello"};
    std::cout << "Root:\t"      << myPath.root_path()            << std::endl;
    std::cout << "Relative:\t"  << myPath.relative_path()        << std::endl;
    std::cout << "Filename:\t"  << myPath.filename()             << std::endl;
    return 0;
}

What am I doing wrong? I am compiling with MinGW. Thanks in advance!

Upvotes: 2

Views: 889

Answers (1)

oLen
oLen

Reputation: 5547

Replace

find_package(BOOST 1.6.0 REQUIRED)

by

find_package(Boost 1.63.0 REQUIRED filesystem system)

otherwise CMake doesn't know which boost library to link against.

Upvotes: 2

Related Questions