使用CMake为MSVC9生成的项目文件链接到boost库失败

时间:2011-06-24 13:44:36

标签: boost cmake linker-errors

我正在尝试通过使用CMake创建MSVC9.0项目文件来构建具有boost库的应用程序。

我收到以下错误:

  

错误3致命错误LNK1104:无法打开文件'libboost_system-vc90-mt-gd-1_44.lib'

这是CMake配置

cmake_minimum_required(VERSION 2.8)
PROJECT( TestProject)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
set(BOOST_ROOT "D:/boost_1_44_0")
set(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE( Boost 1.44.0 REQUIRED unit_test_framework system)

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${BOOST_ROOT})
LINK_DIRECTORIES(${LINK_DIRECTORIES} "D:/boost_1_44_0/stage/lib")


SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
SET(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
ADD_EXECUTABLE(testapp
main.cpp)

TARGET_LINK_LIBRARIES(testapp
${Boost_SYSTEM_LIBRARY}
)

SET_TARGET_PROPERTIES( testapp PROPERTIES DEBUG_POSTFIX "d" )

我使用以下选项为静态和共享(调试和发布)构建了增强功能。

bjam toolset=msvc  variant=debug   link=shared runtime-link=shared threading=multi --build-type=complete stage
bjam toolset=msvc  variant=release link=shared runtime-link=shared threading=multi --build-type=complete stage
bjam toolset=msvc  variant=debug   link=static runtime-link=static threading=multi --build-type=complete stage
bjam toolset=msvc  variant=release link=static runtime-link=static threading=multi --build-type=complete stage

我不确定我在配置中缺少什么。有什么建议? 感谢。

2 个答案:

答案 0 :(得分:21)

首先,您是否检查了舞台目录“D:/ boost_1_44_0 / stage / lib”中是否存在“'libboost_system-vc90-mt-gd-1_44.lib”?

第二:我以前使用Boost和CMake的find_package(Boost)时遇到的最常见问题是对自动链接的干扰。您可以通过向编译标志添加定义来禁用它

add_definitions( -DBOOST_ALL_NO_LIB )

但是您可能需要指定是否要链接到动态或静态版本

set( Boost_USE_STATIC_LIBS ON ) # or Off, depending on what you want
find_package( Boost 1.44.0 REQUIRED unit_test_framework system)

当然,您始终可以检查生成的visual studio文件,以查看哪些链接库实际添加到您的项目中。

答案 1 :(得分:1)

经过多次尝试,我能够在Windows上使用Boost编译项目。 这是CMakeLists.txt来源:

cmake_minimum_required (VERSION 2.6)

project (SendCommand)

include_directories(./)

set(BOOST_ROOT F:/boost_1_55_0/)
set(BOOST_INCLUDEDIR F:/boost_1_55_0/)
set(BOOST_LIBRARYDIR F:/boost_1_55_0/lib32-msvc-10.0/)

set(Boost_INCLUDE_DIRS F:/boost_1_55_0/)
set(Boost_LIBRARY_DIRS F:/boost_1_55_0/lib32-msvc-10.0/)

add_definitions(-DBOOST_ALL_NO_LIB)

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)

find_package(Boost 1.55.0 REQUIRED COMPONENTS system thread)

include_directories(${Boost_INCLUDE_DIRS}) 
add_executable(SendCommand send_command.cpp ivdlp_packet.cpp) 
target_link_libraries(SendCommand ${Boost_LIBRARIES})

有关详细信息,您可以使用我撰写的文档: https://docs.google.com/document/d/1nE7kYBRQAWbR4rGkMmA5-Hg88M9vS_kAjO4Tc9Rq5zU/pub