1

I am trying to add Boost library to my project using the CMakeLists.txt in the follwing way:

set(BOOST_INCLUDEDIR "C:/boost_1_57_0")
set(BOOST_LIBRARYDIR "C:/boost_1_57_0/stage/lib")

find_package(Boost 1.57.0 COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES})

However, I get the followng error: LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-1_57.lib'

libboost_filesystem-vc120-mt-1_57.lib is located in the stage/lib folder, so I don't know what is going on. I am compiling with Visual Studio 2013.

Any thoughts?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
danst_18
  • 497
  • 1
  • 5
  • 17
  • Does ***libboost_filesystem-vc120-mt-1_57.lib*** exist in your library search paths? Check your `additional library directories` setting in Visual Studio. Are you mixing 32 and 64 bit? – drescherjm Feb 20 '15 at 14:43
  • Yes, it is inside the `C:/boost_1_57_0/stage/lib` folder. I don't think I'm mixing 32 and 64 bits. – danst_18 Feb 20 '15 at 14:47
  • I am using QtCreator with Visual Studio 2013 compiler, but not VS2013... – danst_18 Feb 20 '15 at 14:56
  • I can not help with that. I do the reverse (build Qt applications using the Visual Studio IDE). – drescherjm Feb 20 '15 at 15:00
  • I still think the most likely problem a 32 / 64 bit conflict between boost and the application you are compiling. Remember they must match. If your are building a 32 bit application you need 32 bit boost. If you are building a 64 bit application you need 64 bit boost binaries. – drescherjm Feb 20 '15 at 15:03
  • How do I know which boost binaries do I have? I just compiled them following the Getting Started guide in their website, with the commands `boostrap` and `.\b2`. – danst_18 Feb 20 '15 at 15:08
  • I believe this is a linker problem and has nothing to do with CMake. Check that the architecture of your program matches architecture of the built boost .lib files. I'm betting one is x86, the other x64. The easiest way to check is to change the architecture of the project you're compiling from one to the other and try again. To ensure that boost is compiled for a specific architecture, use the address-model flag for .\b2, i.e. "address-model=64" for 64-bit. – Greg Kramida Feb 20 '15 at 19:29
  • Was this issue ever resolved? – Greg Kramida Feb 23 '15 at 17:01
  • I checked the architecture configuration and that's not the problem. I got rid of the initial error by adding `add_definitions(-DBOOST_ALL_NO_LIB)` and `add_definitions(-DBOOST_ALL_DYN_LINK)` as suggested here http://stackoverflow.com/a/6646746/3338917. However, now I get a lot of link errors everywhere in my code where boost is used. I am considering rewriting the code using the Qt library, since boost is only used a few times and I am already using Qt in all the others components that my project has. – danst_18 Feb 23 '15 at 17:29

3 Answers3

0

Try setting the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREADED CMake variables to ON before using find_package, i.e.:

set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
find_package( Boost 1.57.0 COMPONENTS filesystem )

I've come across this problem before and it seems as though, on multithreaded windows systems, the Boost bootstrap installer compiles multithreaded, static libraries by default. However, the CMake FindBoost script (which is used by find_package) searches for single-threaded, dynamic libraries by default.

neurotempest
  • 147
  • 5
0

Since you're using VS compiler I'll say you're working on Windows. The error refers to the linker, which is failing to find boost libraries, as noticed.

Taking into account that the library exists in the boost path, my solution was to do a file(COPY) for the specific library, as a last resort.

if(WIN32)
    set(BOOST_ROOT "C:/boost_1_57_0")
    set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib/)
endif()

find_package(Boost 1.57.0 EXACT REQUIRED system filesystem)

if(Boost_FOUND)
    message(STATUS "found boost, Boost_LIBRARIES <" ${Boost_LIBRARIES} ">")
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    set(Boost_USE_MULTITHREADED      ON)
    set(Boost_USE_STATIC_RUNTIME     OFF)
else()
    message(STATUS "boost not found")
endif()

target_link_libraries(boost_test ${Boost_LIBRARIES})
file(COPY "${Boost_LIBRARY_DIRS}/boost_filesystem-vc120-mt-1_57.dll" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")

You may add some log messages to the CMake in order to know the values returned in the find_package.

Lejuanjowski
  • 139
  • 1
  • 2
  • 6
0
  1. Make sure the architecture (x64) matches.

    $ cmake -A x64 ..
    
  2. Use link_directories command before adding executables just like include_directories.

    link_directories(${Boost_LIBRARY_DIRS})
    
Burak
  • 2,251
  • 1
  • 16
  • 33