CMake的连接,以提高。错误LNK2005 [英] CMake linking to boost. error LNK2005

查看:225
本文介绍了CMake的连接,以提高。错误LNK2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在寻找一整天的解决方案,但没有喜悦。

Been looking around all day for a solution but no joy.

我有2个项目CMake的一个解决方案。一来是哪个环节提高静态库,另一个是哪个环节,来推动和我自己的静态库的可执行文件。
问题是:在Linux中它编译罚款与海湾合作委员会。但在VS2008我得到program_options以下类型的链接错误而已。

I have a CMake solution with 2 projects. The one is a static library which links to boost, and the other is an executable which links to boost and my own static library. Problem is: In Linux it compiles fine with gcc. But in VS2008 I get the following type of linker errors for program_options only.

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)

看起来像它链接到两个静态库和DLL的lib ...但是为什么呢?

Looks like it's linking to both the static lib and the dll lib...but why?

所以我有一个CMakeFile这样一个解决方案目录:

So I have a solution directory with a CMakeFile like this:

    CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
    PROJECT( BBlockTools )

    SET( TopDir ${CMAKE_CURRENT_SOURCE_DIR} )
    ADD_SUBDIRECTORY( Utilities )
    ADD_SUBDIRECTORY( BBlockFixer )

和则两项目目录。公用事业是一个静态库,并通过以下CMakeFile创建:

And then the two project directories. Utilities is a static library and is created by following 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}
)

和本CMakeFile创建的第二个项目:

And the second project created by this CMakeFile:

PROJECT( BBlockFixer )

SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost COMPONENTS system filesystem program_options REQUIRED)
LINK_DIRECTORIES ( ${BBlockTools_BINARY_DIR}/Utilities/Debug ${Boost_LIBRARY_DIRS} )
INCLUDE_DIRECTORIES ( ${TopDir} ${Boost_INCLUDE_DIRS} )

SET( src_cpp fixerMain.cpp )

ADD_EXECUTABLE( BBlockFixer ${src_cpp} )

TARGET_LINK_LIBRARIES( BBlockFixer 
  Utilities
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_PROGRAM_OPTIONS_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
  ${Boost_LIBRARIES}
)

我是新来的CMake,所以我可能会做一些非常糟糕,但我真的无法找出发生了什么事情。我开始喜欢删除输入program_options链接,解决了这个问题玩弄的VS项目(定影液)(直到我再次运行cmake)。但我觉得,是不是因为从它的外观它是联系两者的DLL lib和对静态库由于某种原因,解决方案......
我试图从FIND_PACKAGE但犯规帮助去除从$ {TARGET_LINK_LIBRARIES} Boost_PROGRAM_OPTIONS_LIBRARY从BBlockFixer和program_options。

I'm new to CMake so I might be doing something very bad but I really can't find out what's going on. I started playing around with the VS Project(Fixer) like removing the link input to program_options which fixes the problem(until I run cmake again). But I think that isn't the solution since from how it looks it's linking both to the dll lib and to the static lib for some reason... I tried removing ${Boost_PROGRAM_OPTIONS_LIBRARY} from BBlockFixer from TARGET_LINK_LIBRARIES and program_options from FIND_PACKAGE but doesnt help.

这是我的CMake明白我BBlockFixer继承我的静态库的链接program_options这应该是所有的罚款。但是,在没有此boost_program_options-vc90-MT-GD-1_46_1.lib(boost_program_options-vc90-MT-GD-1_46_1.dll)获取链接到我的项目?

From what I understand in CMake my BBlockFixer inherits the links to program_options from my static lib which should be all fine. But where did this boost_program_options-vc90-mt-gd-1_46_1.lib(boost_program_options-vc90-mt-gd-1_46_1.dll) get linked into my project?

任何帮助将是AP preciated因为我变得绝望。它不能真的那么难...

Any help would be appreciated because I'm becoming desperate. It can't really be that hard...

PS。这TOPDIR我设置的,这样我可以包括从实用.h文件。但我敢肯定,必须有做一个更清洁的方式或?

PS. this TopDir I set is so that I can include the .h file from the Utilities. But I'm sure there must be a cleaner way to do it or?

推荐答案

我的猜测是,你还是自动链接:在MSVC,提升使用一些杂对automically指示编译器链接到必要的库并没有再无需手动指定所有链接的目标,以连接器。当然,这与咬伤的依赖解析CMake的,而你手动指定target_link_libraries。

My guess is that you are still auto-linking: On MSVC, boost uses some pragma's to automically instruct the compiler to link to the necessary libraries and there is then no need to specify all the link-targets manually to the linker. Of course, this bites with the dependency-resolution of CMake and you specifying target_link_libraries manually.

看看我的<一个href=\"http://stackoverflow.com/questions/6468681/problem-linking-to-boost-library-with-cmake-generated-project-file-for-msvc9\">answer到哪里我建议以下额外的定义来禁用自动链接一个类似的问题:

Take a look at my answer to a similar question where I suggested the following extra definition to disable auto-linking:

add_definition( -DBOOST_ALL_NO_LIB )

您可能需要添加动态链接并根据加速版本,当发生以下组件:

You may need to add the following when linking dynamically and depending on the Boost version and the components:

add_definitions( -DBOOST_ALL_DYN_LINK )

还请注意,您指定的find_package()调用所需的升压组件,然后在target_link_libraries再次手动指定它们()调用。根据你的需要了一下,你也可以用这个代替

Note also that you specify the Boost components you want in the find_package() call and then specify them again manually in your target_link_libraries() call. Depending a bit on your need, you could also replace this with

target_link_libraries( ${Boost_LIBRARIES} )

这篇关于CMake的连接,以提高。错误LNK2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆