我正在尝试使用CMakeLists.txt以下面的方式将Boost库添加到我的项目中:
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})
但是,我收到了以下错误:LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-1_57.lib'
libboost_filesystem-vc120-mt-1_57.lib
位于stage/lib
文件夹中,因此我不知道发生了什么。我正在使用Visual Studio 2013进行编译。
有什么想法吗?
答案 0 :(得分:0)
在使用Boost_USE_STATIC_LIBS
之前,请尝试将Boost_USE_MULTITHREADED
和ON
CMake变量设置为find_package
,即:
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
find_package( Boost 1.57.0 COMPONENTS filesystem )
我之前遇到过这个问题,似乎在多线程Windows系统上,Boost引导程序安装程序默认编译多线程静态库。但是,CMake FindBoost脚本(由find_package
使用)默认搜索单线程动态库。
答案 1 :(得分:0)
由于您使用的是VS编译器,所以我会说您正在Windows上工作。 该错误是指链接器,如前所述,该链接器未能找到boost库。
考虑到该库存在于boost路径中,我的解决方案是对特定库做一个file(COPY)
,作为最后的选择。
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}")
您可以向CMake添加一些日志消息,以便了解在
find_package
。