小编Ste*_*fan的帖子

CMake链接到提升.错误LNK2005

一整天都在寻找解决方案,但没有快乐.

我有一个包含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)

c++ boost cmake visual-studio-2008 libraries

7
推荐指数
1
解决办法
4644
查看次数

标签 统计

boost ×1

c++ ×1

cmake ×1

libraries ×1

visual-studio-2008 ×1