'Boost serialization link error and GLIBCXX_USE_CXX11_ABI in debug builds

I am using Boost serialization 1.78.0. I am getting link errors as follows:

boost/1.78.0/lib/native/include/boost/archive/detail/oserializer.hpp:95: 
undefined reference to `boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::char_traits<char> >::save(std::string const&)'

I am passing _GLIBCXX_USE_CXX11_ABI=0 to provide backwards compatibility with libraries built with the old ABI standard (we link against several such libraries).

This only affects debug builds. In release builds I do not get the link error. Rebuilding Boost with and without the flag in debug has no effect on the generated binaries. In release mode the binaries are different sizes with the flag set/not set. This would suggest the flag is not doing anything in debug or is otherwise being invalidated by another compiler option. I am not building Boost as c++11 (my client app is c++11).

Here is a minimal example (Ubuntu 18.04 with gcc 7.5.0):

Boost build command line:

./b2 --with-serialization cxxflags=-fPIC define=_GLIBCXX_USE_CXX11_ABI=0 define=BOOST_NO_CXX11_SCOPED_ENUMS=1 cflags=-fPIC variant=debug runtime-link=static link=static -d2 stage --stagedir=/home/code/Boost/1.78.0/debug

archivetest.cpp:

#include "boost/archive/binary_oarchive.hpp"
#include "boost/serialization/vector.hpp"
#include <vector>
#include <string>
#include <fstream>

int main()
{
    std::vector<std::string> toSerialize;
    const std::string filePath = "archivetest.dat";
    std::ofstream ofs(filePath.c_str(), std::fstream::binary | std::fstream::out);
    boost::archive::binary_oarchive oa(ofs);
    oa << toSerialize;
}

Compile:

gcc -c -x c++ /home/code/archivetest/archivetest.cpp -I /home/code/Boost/1.78.0/lib/native/include -g1 -o "/home/code/archivetest/archivetest.o" -Wall -O0 -D_DEBUG -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0

Link:

g++ -o "/home/code/archivetest/archivetest.out" -L/home/code/Boost/1.78.0/debug /home/code/archivetest/archivetest.o "-lboost_serialization"

In the example above, archivetest links and runs correctly with _GLIBCXX_USE_CXX11_ABI=1 in debug and release mode.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source