2

I have recently ran into an error when compiling my program with the boost serialization library.

I have read across many forums that I need to link the boost serialization library (-lboost_serialization). Which, I did.

However, there is one error that I am getting on the code:

/usr/local/boost_1_64_0/boost/serialization/throw_exception.hpp:36: undefined reference to `boost::archive::archive_exception::archive_exception(boost::archive::archive_exception const&)' 

So I thought, what if I have to also include the exception library? So I added this into the libraries for my compiler settings:

-lboost_exception 

However, now it is saying that it is unable to find the boost_exception library. Which is interesting because I am able to see it in the file system!

I am wondering if I am doing the right thing or am I forgetting to add something to my compiler settings? Any help will be much appreciated!

Edit:

Here is the code:

// MS compatible compilers support #pragma once #if defined(_MSC_VER) # pragma once #endif // boost/throw_exception.hpp // // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include <boost/config.hpp> #ifndef BOOST_NO_EXCEPTIONS #include <exception> #endif namespace boost { namespace serialization { #ifdef BOOST_NO_EXCEPTIONS inline void throw_exception(std::exception const & e) { ::boost::throw_exception(e); } #else template<class E> inline void throw_exception(E const & e){ throw e;// Error occurs here } #endif } // namespace serialization } // namespace boost 

Here is a code sample where the breakage occurs

#include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <vector> #include "boost/serialization/vector.hpp" #include <string> #include <fstream> int main() { std::ofstream ofs("/home/phillip/test.txt"); std::vector<std::string> tests; boost::archive::text_oarchive oa(ofs); oa << tests; } 
3
  • Can you add source code sample you are compiling? What is your compiler? Commented Aug 2, 2017 at 10:26
  • I am compiling with gcc Commented Aug 2, 2017 at 16:42
  • Normally, you need to link serialization library via -lboost_serialization. That is correct. You do not need to link exception library now. So far it looks as boost paths problem. Can you post full output of your compilation with "-v" gcc flag added? It is better to see all errors/warnings. Commented Aug 2, 2017 at 19:08

1 Answer 1

1

I've seen this happen when linking to the boost library when compiling with a different compiler/version/flags.

E.g. many Boost Serialization answer programs wouldn't link when compiling with Clang on Coliru whereas they'd run fine when compiling with Gcc

Sign up to request clarification or add additional context in comments.

4 Comments

I compiled the boost library with gcc
Make sure it is using the same flags. Otherwise, like @pe3k the most likely cause is that the compiler is finding a different version of the library in another path (and that library was (compiled using) a different version of boost/gcc/flags). Note that Boost.System is also a dependency.
so the flags that I use to compile my program, I should use to compile boost?
ok, thanks for letting me know, I will try this once I get a chance. Maybe this weekend? For now, I downloaded a version of the boost library using sudo apt-get and everything works. It is running 1.58. I am only using this so that development will not be hindered but prefer to use the latest version

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.