我正在尝试在Ubuntu 11.10上编译一个使用Boost库的程序.我安装了Ubuntu Repository中的1.46-dev Boost库,但编译程序时出错.
undefined reference to boost::system::system_category()
我做错了什么?
一整天都在寻找解决方案,但没有快乐.
我有一个包含2个项目的CMake解决方案.一个是链接到boost的静态库,另一个是链接到boost和我自己的静态库的可执行文件.问题是:在Linux中它用gcc编译得很好.但是在VS2008中,我只为program_options获得以下类型的链接器错误.
libboost_program_options-vc90-mt-gd-1_46_1.lib(options_description.obj) : error LNK2005: "public: class boost::program_options::options_description_easy_init & __thiscall boost::program_options::options_description_easy_init::operator()(char const *,char const *)" (??Roptions_description_easy_init@program_options@boost@@QAEAAV012@PBD0@Z) already defined in boost_program_options-vc90-mt-gd-1_46_1.lib(boost_program_options-vc90-mt-gd-1_46_1.dll)
Run Code Online (Sandbox Code Playgroud)
看起来它链接到静态库和DLL库...但为什么呢?
所以我有一个带有CMakeFile的解决方案目录,如下所示:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT( BBlockTools )
SET( TopDir ${CMAKE_CURRENT_SOURCE_DIR} )
ADD_SUBDIRECTORY( Utilities )
ADD_SUBDIRECTORY( BBlockFixer )
Run Code Online (Sandbox Code Playgroud)
然后是两个项目目录.Utilities是一个静态库,通过以下CMakeFile创建:
PROJECT( Utilities )
SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES ( ${Boost_LIBRARY_DIRS} )
INCLUDE_DIRECTORIES ( ${Boost_INCLUDE_DIRS} )
SET( src_h Utilities.h )
SET( src_cpp Utilities.cpp )
ADD_LIBRARY( Utilities STATIC ${src_h} ${src_cpp} )
TARGET_LINK_LIBRARIES( Utilities
${Boost_SYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_LIBRARIES}
)
Run Code Online (Sandbox Code Playgroud)
这个CMakeFile创建的第二个项目:
PROJECT( BBlockFixer …Run Code Online (Sandbox Code Playgroud) 我搜索并发现很多人都有同样的问题,但没有解决方案.
我正在使用CMake为MinGW生成Makefile,在编译时我收到错误:
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x5e): undefined reference to `_imp___ZN5boost6thread4joinEv'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x71): undefined reference to `_imp___ZN5boost6threadD1Ev'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x88): undefined reference to `_imp___ZN5boost6threadD1Ev'
Run Code Online (Sandbox Code Playgroud)
这似乎是一个链接问题,我明白了.我的CMake配置是:
project(boosttest)
cmake_minimum_required(VERSION 2.6)
include_directories(${boosttest_SOURCE_DIR}/include c:/boost_1_48_0/)
link_directories(c:/boost_1_48_0/lib)
file(GLOB_RECURSE cppFiles src/*.cpp)
add_executable(boosttest ${cppFiles})
target_link_libraries(boosttest libboost_thread-mgw46-mt-1_48.a)
Run Code Online (Sandbox Code Playgroud)
首先我尝试使用find_package(Boost COMPONENTS thread)它,它的工作方式相同,所以我想尝试手动执行此操作,但仍然会遇到相同的错误.
有什么见解吗?
我使用bjam编译它为mingw并作为静态链接.还试过做:
add_library(imp_libboost_thread STATIC IMPORTED)
set_property(TARGET imp_libboost_thread PROPERTY IMPORTED_LOCATION c:/boost_1_48_0/lib/libboost_thread-mgw46-mt-1_48.a)
target_link_libraries(boosttest imp_libboost_thread)
Run Code Online (Sandbox Code Playgroud)
我仍然得到相同的错误消息.