Search code examples
c++linkercmakexcode5boost-serialization

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. Everything was going fine for our different dependencies, including Boost::System, but we cannot get it to compile this minimal example for Boost::Serialization.

CMake file

# 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

#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
  • CMake 2.8-12
  • Boost 1.55
  • OSX 10.8

EDIT:

The two Boost libraries listed in the CMake are actually found (They are listed on CMake output as such).

Boost has been built first time with default parameters and a second time following the instructions in this post. 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.)

The problem

We are getting several (undefined symbols) linker errors :

Undefined symbols for architecture 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::text_oarchive_impl(std::ostream&, unsigned int)", referenced from: boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int) in main.o
"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

Any direction ? (As you can see, we are asking to link the application against both Boost::Serialization and Boost::System).


Solution

  • You need to check that all libraries is actually found by find_package command. Easiest way to do it is to add REQUIRED sub-option:

    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. I'm using custom boost build (not system) with static libraries.

    PS

    IMHO there is no need to clear BOOST_ROOT variable:

    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.