简体   繁体   English

Boost序列化:从CMake生成的XCode 5项目中的链接器错误(带有最少的完整示例)

[英]Boost Serialization : Linker errors in XCode 5 project, generated from CMake (w/ minimal complete example)

In our main project, the build management is deferred to CMake. 在我们的主要项目中,将构建管理推迟到CMake。 Everything was going fine for our different dependencies, including Boost::System, but we cannot get it to compile this minimal example for Boost::Serialization. 对于我们不同的依赖项,包括Boost :: System,一切都进行得很好,但是我们无法获取它来编译Boost :: Serialization的最小示例。

CMake file CMake文件

# Untested with previous versions, yet should work
cmake_minimum_required(VERSION 2.8.11)

project(SerialCmake)

# Boost dependency
set(BOOST_ROOT CACHE PATH "Path to Boost library")
find_package(Boost 1.55 COMPONENTS serialization system)

# Create the target
add_executable(${PROJECT_NAME} main.cpp)

include_directories(${Boost_INCLUDE_DIR})

target_link_libraries(${PROJECT_NAME}
    ${Boost_LIBRARIES})

CPP main file CPP主文件

#include <fstream>
#include <boost/archive/text_oarchive.hpp>

class Serializable
{
    friend class boost::serialization::access;

public:
    Serializable(int a): a(a)
    {}

private:
    template <class Archive>
    inline void serialize (Archive &ar, const unsigned int version)
    {
        ar & a;
    }

    int a;
};

int main(int argc, char **argv)
{
    Serializable s1(1);

    // Save
    {
        std::ofstream ofs(argv[1]);
        boost::archive::text_oarchive oa(ofs);
        oa << s1;
    }
    return 0;
}

Some more info 一些更多的信息

Our versions : 我们的版本:

  • XCode 5.0.2 XCode 5.0.2
  • CMake 2.8-12 CMake的2.8-12
  • Boost 1.55 提升1.55
  • OSX 10.8 OSX 10.8

EDIT: 编辑:

The two Boost libraries listed in the CMake are actually found (They are listed on CMake output as such). 实际上找到了CMake中列出的两个Boost库(就这样在CMake输出中列出了它们)。

Boost has been built first time with default parameters and a second time following the instructions in this post . Boost是首次使用默认参数构建的,第二次是根据这篇文章中的指示构建 The errors are the same with the two builds. 两个版本的错误相同。 (Actually, I think both builds are alright, since using the libraries in a non CMake project, by adding them to XCode as described in the same post, does work.) (实际上,我认为这两个版本都可以,因为在非CMake项目中使用这些库,可以通过如同一篇文章中所述将它们添加到XCode中来工作。)

The problem 问题

We are getting several (undefined symbols) linker errors : 我们收到几个(未定义符号)链接器错误:

Undefined symbols for architecture x86_64: 架构x86_64的未定义符号:
"boost::archive::text_oarchive_impl::save(std::string const&)", referenced from: void boost::archive::save_access::save_primitive(boost::archive::text_oarchive&, std::string const&) in main.o “ boost :: archive :: text_oarchive_impl :: save(std :: string const&)”,引用自:void boost :: archive :: save_access :: save_primitive(boost :: archive :: text_oarchive&,std :: string const&) main.o
"boost::archive::text_oarchive_impl::text_oarchive_impl(std::ostream&, unsigned int)", referenced from: boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int) in main.o 引用自main.o中的boost :: archive :: text_oarchive :: text_oarchive(std :: ostream&,unsigned int)“ boost :: archive :: text_oarchive_impl :: text_oarchive_impl(std :: ostream&,unsigned int)”
"boost::archive::basic_text_oprimitive::~basic_text_oprimitive()", referenced from: boost::archive::text_oarchive_impl::~text_oarchive_impl() in main.o ld: symbol(s) not found for architecture x86_64 “ boost :: archive :: basic_text_oprimitive ::〜basic_text_oprimitive()”,引用自:main.o中的boost :: archive :: text_oarchive_impl :: ~~ text_oarchive_impl()ld:架构x86_64找不到符号

Any direction ? 有方向吗? (As you can see, we are asking to link the application against both Boost::Serialization and Boost::System). (如您所见,我们要求将应用程序与Boost :: Serialization和Boost :: System链接)。

You need to check that all libraries is actually found by find_package command. 您需要检查find_package命令是否确实找到了所有库。 Easiest way to do it is to add REQUIRED sub-option: 最简单的方法是添加REQUIRED子选项:

find_package(Boost 1.55 REQUIRED system serialization)

Works fine for me. 对我来说很好。 Xcode 5.0.2, Boost 1.55, CMake 2.8.12.1, OS X 10.9. Xcode 5.0.2,Boost 1.55,CMake 2.8.12.1,OS X 10.9。 I'm using custom boost build (not system) with static libraries. 我正在使用带有静态库的自定义增强构建(不是系统)。

PS PS

IMHO there is no need to clear BOOST_ROOT variable: 恕我直言,无需清除BOOST_ROOT变量:

set(BOOST_ROOT CACHE PATH "Path to Boost library")

If boost is already found (by other parent project) you will do find-work twice, if project use custom boost location, you will rewrite it. 如果已经(通过其他父项目)找到了boost,则将进行两次查找工作,如果项目使用自定义的boost位置,则将其重写。

For what its worth, I had a similar problem after adding back in serialization to one of my targets, and it was just that cmake wasn't picking up the change in CMakeLists.txt so had to remove the cache: 对于它的价值,在将序列化添加回我的目标之一后,我遇到了类似的问题,只是cmake并未在CMakeLists.txt中进行更改,因此不得不删除缓存:

I merely added the "serialization" to: 我只是将“序列化”添加到:

FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem date_time serialization)

and had this error: 并出现此错误:

  The following Boost libraries could not be found:

      boost_serialization

so solved with: 因此解决:

rm CMakeCache.txt
cmake ./

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM