Why can't I compile the Boost.Serialization demo? - c++

I am running Ubuntu 16.04. I'm trying to get Boost.Serialization set up, and I'm running into trouble compiling the first demo from the tutorial (titled A Very Simple Case). I have followed the Boost Getting Started page for installing Boost and building Serialization, doing the following:
download the code from SourceForge (I have tried different versions including 1.67.0 and 1.55.0)
cd boost_1_55_0
./bootstrap.sh --prefix=/usr/local --with-libraries=serialization
sudo ./b2 install
check that /usr/local/include and /usr/local/lib include the relevant Boost files
and then I compile demo.cpp using
g++ -o demo demo.cpp -I /usr/local/include/boost -l boost_serialization
and get the following error:
/tmp/cc9a7HRg.o: In function boost::archive::text_iarchive::text_iarchive(std::istream&, unsigned int)':
demo.cpp:(.text._ZN5boost7archive13text_iarchiveC2ERSij[_ZN5boost7archive13text_iarchiveC5ERSij]+0x36): undefined reference toboost::archive::detail::shared_ptr_helper::shared_ptr_helper()'
/tmp/cc9a7HRg.o: In function boost::archive::text_iarchive::~text_iarchive()':
demo.cpp:(.text._ZN5boost7archive13text_iarchiveD2Ev[_ZN5boost7archive13text_iarchiveD5Ev]+0x24): undefined reference toboost::archive::detail::shared_ptr_helper::~shared_ptr_helper()'
collect2: error: ld returned 1 exit status
The line causing the error is
boost::archive::text_iarchive ia(ifs);
It is worth mentioning that the output line
boost::archive::text_oarchive oa(ofs);
was causing a similar error before the most recent re-install. Also, if I comment out all of the input (including the line causing the error), the program compiles fine, but causes a segmentation fault upon running. I have included cout << "done" << endl; at the end of main, but it is not reached.
Does anybody know what might be causing this error? Sorry if this seems like a silly question. I am very new to C++ development. Thank you in advance!

sudo apt-get install libboost-all-dev
worked for me.
This command is taken from the same source as lboyd's solution:
https://www.technical-recipes.com/2012/getting-started-with-the-boost-libraries-in-ubuntu-linux/
then I compile with
g++ serialization.cpp -lboost_serialization -lboost_system

For anybody else running into this issue, I managed to fix it by manually uninstalling everything I had installed from source (deleting the relevant Boost files from /usr/locale/include/ and /usr/local/lib/ as well as the root Boost directory) and installing using apt-get as described here.

Related

ld linker error "cpu model hidden symbol"

I'm getting an ld error when attempting to compile an sfml program on ubuntu 16.04. This is apparently a known issue, and there is supposed to be a workaround, but I don't understand what is it...
http://web.archive.org/web/20160509014317/https://gitlab.peach-bun.com/pinion/SFML/commit/3383b4a472f0bd16a8161fb8760cd3e6333f1782.patch
The error spat out by ld is
hidden symbol `__cpu_model' in /usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc.a(cpuinfo.o) is referenced by DSO
There is no relevant code to this - as I understand it this error is produced on all ubuntu 16.04 systems with g++ 5, if the program to be linked contains objects such as sf::Texture and sf::Sprite. (I don't know any more detail than this.)
I have tried also compiling with g++ 4.9, but the same error occurs.
My compile line is g++-4.9 --std=c++11 -Wall main.cpp -lsfml-graphics -lsfml-window -lsfml-system -o a.out
Has anyone else experienced this error and resolved it successfully?
I've had to fix this issue several times. Instead of applying the patch, you can manually fix it by editing the file SFML/src/SFML/Graphics/CMakeLists.txt. At line 149, you will find the following:
if(SFML_COMPILER_GCC)
set_source_files_properties(${SRCROOT}/ImageLoader.cpp PROPERTIES COMPILE_FLAGS -fno-strict-aliasing)
endif()
After the endif(), insert the following:
if(SFML_COMPILER_GCC AND BUILD_SHARED_LIBS)
list(APPEND GRAPHICS_EXT_LIBS "-lgcc_s -lgcc")
endif()
Then, in the top-level SFML folder, run the following:
mkdir build && cd build
cmake .. -DSFML_BUILD_EXAMPLES=ON -DSFML_BUILD_DOCS=ON
make
sudo make install
sudo ldconfig
This will get it built and installed without the compiler error. (Note: Remove the -D flags from cmake if you don't want docs or examples)
I ran this in the SFML source directory before running the standard cmake...make:
curl https://gitlab.peach-bun.com/pinion/SFML/commit/3383b4a472f0bd16a8161fb8760cd3e6333f1782.patch \
| patch -p1
and that solved the problem
I've got the same linker error when trying to build SFML 2.4.2 with examples, specifically with opengl and shader ones.
Inspired by the #Joshua solution, I tried to change the compiler from GCC to Clang. It worked.
I am sharing here because it seems to be a simpler solution if you have no restrictions to use Clang.
Just download SFML and change to its directory. And...
mkdir build && cd build
cmake .. -DSFML_BUILD_EXAMPLES=ON -DCMAKE_CXX_COMPILER=clang++
make
sudo make install
sudo ldconfig
Removing -fvisibility=hidden from compiler options worked for me.

using sndfile library when compiling 32bit linux application on Fedora 20 x86_64

I'm currently using Fedora 20 x86_64.
I've built libsndfile with these commands:
sudo ./configure BASE_FLAGS=-32
sudo make
sudo make install
This all goes well and I can find the compiled library in /usr/local/lib.
I want to use this in an application that will be built in 32bit.
The problem occurs when I try to compile that application. I get the following error:
/usr/bin/ld: skipping incompatible /builddir/Code/Platformer/../Dependencies/libsndfile/lib/Linux/libsndfile.so when searching for -lsndfile
/usr/bin/ld: skipping incompatible /builddir/Code/Platformer/../Dependencies/libsndfile/lib/Linux/libsndfile.a when searching for -lsndfile
/usr/bin/ld: cannot find -lsndfile
The "skipping incompatible" might suggest that I'm doing something wrong when compiling sndfile but since I'm kind of new to this I can't find what it is. I tried different configurations but they all have the same result.
If this needs more code or something, let me know what you need.
For anyone with the same problem - try to look here:
ld cannot find an existing library
Generally there is probably a symlink missing, because the '-devel' library is not istalled. That is why
sudo yum install libsndfile-devel.*
fixed it for me.

Error while building app in cross-compiled qt in raspberry

I'm trying to build and app on a cross-compiled qt5 in a raspberry pi.
This app is based on this rf24l01 library. When I compile a cpp program from the RPi, everything works fine, but when I tried from my pc running ubuntu 12.04 (32bits) the following error is through:
error: cannot find -lrf24-bcm
I point to the library using this line in the pro file:
LIBS += -L/mnt/rpi/usr/local/lib -lrf24-bcm
AFAIK, this is the path where the lib is located:
This is the result of `ls /mnt/rpi/usr/local/lib:
librf24-bcm.so libwiringPiDev.so libwiringPi.so.2.0
librf24-bcm.so.1 libwiringPiDev.so.2.0 python2.6
librf24-bcm.so.1.0 libwiringPi.so python2.7
Finally, this is the appeared error in detail:
/home/atron/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++ -Wl,-rpath-link,/mnt/rpi/opt/vc/lib -Wl,-rpath-link,/mnt/rpi/usr/lib/arm-linux-gnueabihf -Wl,-rpath-link,/mnt/rpi/lib/arm-linux-gnueabihf --sysroot=/mnt/rpi -Wl,-rpath,/usr/local/opt/lib -o homekit main.o -L/mnt/rpi/usr/local/lib -lrf24-bcm -L/mnt/rpi/usr/local/opt/lib -lQt5Network -L/usr/local/opt/lib -lQt5Core -lpthread
/home/atron/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lrf24-bcm
collect2: error: ld returned 1 exit status
What I`m doing wrong?
Thanks in advance,
I don't know if this is "technically" correct, but solved my problem.
The main idea is create a symbolic link of the library to my pc /usr/local/lib folder:
sudo ln -s /mnt/rpi/usr/local/lib/librf24-bcm.so /usr/local/lib/librf24-bcm.so
sudo ln -s /mnt/rpi/usr/local/lib/librf24-bcm.so.1 /usr/local/lib/librf24-bcm.so.1
sudo ln -s /mnt/rpi/usr/local/lib/librf24-bcm.so.1.0 /usr/local/lib/librf24-bcm.so.1.0
Then, on QT creator *pro file you only need to add the lib reference:
LIBS=-lrf24-bcm
I hope this could help to some newbie like me.
Regards,

Boost.MPI on Ubuntu 12.04

Background
Hi All,
I'm trying to use Boost::MPI, at the moment I'm just trying to run the simple first example from the tutorial. I am having trouble building/running it.
I installed boost using apt-get and installed boost_mpi & boost_serialization (1.48.0) using synaptic package manager. I installed MPICH2 using apt-get.
Even though OpenMPI was never explicitly installed it appears to be on my system, I assume this is a dependency for Boost::MPI but it appears MPICH2 and OpenMPI are treading on each other's toes.
Info
If I build using
g++ test.cpp -I/usr/include/mpich2 -L/usr/lib -lboost_mpi -lboost_serialization
then run using
mpiexec -n 2 ./a.out
It throws a bunch of errors which seem to come from OpenMPI. If I try and build by linking against the OpenMPI library using
g++ test.cpp -L/usr/lib -lboost_mpi -lboost_serialization -lmpi -I/usr/include/openmpi
I get the following errors:
/usr/bin/ld: /tmp/ccJ5ezv7.o: undefined reference to symbol 'ompi_op_set_cxx_callback'
/usr/bin/ld: note: 'ompi_op_set_cxx_callback' is defined in DSO /usr/lib/libmpi.so.0 so try adding it to the linker command line
/usr/lib/libmpi.so.0: could not read symbols: Invalid operation
If I try building using mpic++ with the following command
mpic++ test.cpp -lboost_mpi -lboost_serialization
It will not link returning a bunch of errors of the form
/usr/lib/libmpich.so: undefined reference to `MPL_trid'
so I tried linking against libmpi i.e.
mpic++ test.cpp -lboost_mpi -lboost_serialization -lmpi
This builds but on running with mpiexec yields the following errors
Fatal error in PMPI_Errhandler_set: Invalid communicator, error stack:
PMPI_Errhandler_set(118): MPI_Errhandler_set(comm=0x370500, errh=0x370be0) failed
PMPI_Errhandler_set(70).: Invalid communicator
Question
It seems to me that somehow OpenMPI and MPICH2 are getting intertwined where there really shouldn't. Does anybody know how I can build against only OpenMPI or MPICH2 then run using the correct mpiexec?
Mixing code compiled against different MPI libraries is not supported in general. If your Boost::MPI is linked against Open MPI, then you must use Open MPI for the rest of your application.
To get the mpic++ as well as the other compiler wrappers and all the header files you should install the -dev package for Open MPI. If the library has been installed as a dependency then only the run-time part will be there.

libboost-system linker errors when cross-compiling to x86

I'm trying to build a 32-bit application on Ubuntu 11.04 x64. I'm having some issues with the build because of linker errors with libboost. The build statement has -lboost_system in it, but when I try to build I get a bunch of these:
CommunicationModule.cpp:(.text+0x68c1): undefined reference to boost::system::generic_category()
CommunicationModule.cpp:(.text+0x68d7): undefined reference to boost::system::system_category()
Everything I've found on google says I need to link to the boost_system library. One place I found says to try linking to it directly, but when i do locate boost_system the result is empty. When I try doing a sudo apt-get install libboost-system-dev it tells me that it's already installed. I'm kind of at a loss here. The library is installed, but it's not being found by locate?
Can anyone tell me what I need to do to properly link to boost::system? I'm fairly new to linux and the complexities of compilers so any help here would be appreciated.
Update:
Here is the output of dpkg -L libboost-system1.42-dev:
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libboost-system1.42-dev
/usr/share/doc/libboost-system1.42-dev/copyright
/usr/share/doc/libboost-system1.42-dev/NEWS.Debian.gz
/usr/share/doc/libboost-system1.42-dev/README.Debian.gz
/usr/lib
/usr/lib/libboost_system.a
/usr/lib/libboost_system-mt.so
/usr/lib/libboost_system-mt.a
/usr/lib/libboost_system.so
Is there a flag I can use to link to one of these directly? I tried using -L /usr/lib/libboost_system.so and -L /usr/lib/libboost_system-mt.so and neither of those fixed the issue. Same with just adding /usr/lib/libboost_system.a and /usr/lib/libboost_system-mt.a to the build statement.
Here is the compilation line:
g++ -m32 -Wl,-O1 -o UTNaoTool [.o files] -L/usr/lib32 -lqglviewer-qt4 -lqwt-qt4 -lboost_system -lboost_thread -lQtXml -lQtOpenGL -lQtGui -lQtNetwork -lQtCore -lGLU -lpthread
Update 2:
I downloaded boost 1.49 and built everything for 32-bit and that seemed to help. A lot of the errors went away, but now I still have these:
CommunicationModule.cpp:(.text+0x68c1): undefined reference to
boost::system::get_generic_category()
Note that the function is different. So all of my errors are regarding undefined references to get_system_category() and get_generic_category() now. I tried adding a -lboost_filesystem to the build command but that didn't fix this, and I made sure it was referencing the 32-bit library that I built when I built libboost_system.
Looking at my own installation, it seems libboost-system-dev does not install the libraries. Using dpkg to tell me what was installed bz libboost-system-dev I get:
$ dpkg -L libboost-system-dev
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libboost-system-dev
/usr/share/doc/libboost-system-dev/copyright
/usr/share/doc/libboost-system-dev/changelog.gz
Poking around, I think you need to install libboost-system1.48.1 (or some other version).
sudo apt-get install libboost-system1.XX.Y
You can also search fo rthe libraries using the find command, for example, search under /usr for all files starting with libboost_system:
find /usr -name "libboost_system*"
Edit: Since you are cross-compiling from a 64 bit OS to a 32 bit one, you need 32 bit versions of the boost libraries. I would be tempted to set up a small 32 bit virtual machine to do this, rather than cross-compiling all the dependencies.
I had the same problem with boost_serialization here is what i found out after couple of googling..
first this library need to be compiled separately :
so after downloading the boost library ,extract it and execute sudo ./bootstrap.sh' then
sudo ./b2 --with-system
after this step you should be find a result when executing locate boost_system
then to link it manually I did:
both should work
g++ boostexample.cpp -o run /PATH/libboost_serialization.a
g++ boostexample.cpp -o run -L/PATH/ -lboost_serialization
well this is a little work around and I'm still looking for how to link the library properly
I hope this helped :)