Trouble using boost, cannot open shared object file - c++

So I'm trying to compile and run a simple boost timer program
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace boost::asio;
io_service io;
deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello World!" << std::endl;
return 0;
}
The first thing I tried when compiling this program was to do
g++ -I /home/vagrant/boost_1_60_0 main.cpp
which gave me an error of
/tmp/cc8Ytqko.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0xfc): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x108): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x114): undefined reference to `boost::system::system_category()'
/tmp/cc8Ytqko.o: In function `boost::system::error_code::error_code()':
main.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cc8Ytqko.o: In function `boost::asio::error::get_system_category()':
main.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
So then I did some research and it seems I needed to build the boost_system binaries so I went to directory boost was located in and ran
./bootstrap.sh
./b2 --with-system
Then I compiled again
g++ -I /home/vagrant/boost_1_60_0 main.cpp -L/home/vagrant/boost_1_60_0/stage/lib/ -lboost_system
and this didn't give me any error but when I ran the executable
vagrant#vagrant-ubuntu-trusty-64:/vagrant$ ./a.out
./a.out: error while loading shared libraries: libboost_system.so.1.60.0: cannot open shared object file: No such file or directory
Don't really know what I need to do here

liibboost_system.so.1.60.0 cannot be found in the list of directories searched by the dynamic linker. The non-default shared-object location is not stored in the binary by default. The environment variable LD_LIBRARY_PATH can be used to add directories that will be searched before the standard locations:
LD_LIBRARY_PATH=/home/vagrant/boost_1_60_0/stage/lib/ ./a.out
This will only work for the current bash environment, and there are also ways to store the path in the executable so that the environment variable is not needed.

Related

How to build a static boost library?

More specifically static boost::iostreams with zlib (aka libz) support.
The problem is, that when I try to link static library it throws unresolved symbols. At the same time when I try to link dynamic library everything works...
I've been searching for answer for several days and can't find it yet.
The command I use to build shared library: b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=shared runtime-link=shared -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"
The command I use to build static library: b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=static runtime-link=static -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"
Example of the program:
#include <iostream>
#include <boost/iostreams/filter/gzip.hpp>
int main(int argc, char* argv[]) {
int a = boost::iostreams::zlib::default_compression;
std::cout << a << std::endl;
return 0;
}
The exception I get:
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x124): undefined reference to `crc32'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x181): undefined reference to `deflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1a1): undefined reference to `inflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1cc): undefined reference to `deflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1e5): undefined reference to `inflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x201): undefined reference to `inflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x214): undefined reference to `deflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x3c9): undefined reference to `inflateInit2_'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x469): undefined reference to `deflateInit2_'
collect2.exe: error: ld returned 1 exit status
makefile:15: recipe for target 'Source.exe' failed
mingw32-make: *** [Source.exe] Error 1
The terminal process terminated with exit code: 1
UPDATE:
Also in case the problem may be related to zlib or bz2:
I got bz2 lib here: http://gnuwin32.sourceforge.net/packages/bzip2.htm
and zlib here: http://gnuwin32.sourceforge.net/packages/zlib.htm
And I tried to build zlib myself with their win32 makefile. I got libz.a and tried to build with it as well, nothing's changed.
As you may know, to solve this problem on unix you just need to add -lz argument.
The same works for windows users. You just (more likely) don't have zlib.a/lib location in your PATH environment variable. So to solve the problem you shall add 2 arguments: -Lpath_to_zlib -lz. Such a simple solution and I've spent hours trying to fix it.

g++ building c++ program with boost dependencies

I want to compile and run a simple c++ websocket application with g++ on windows.
Boost was installed like this:
./bootstrap.bat mingw
./b2.exe install --prefix=C:/boostLibs toolset=gcc
My c++ includes look like this:
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
This websocket sample was provided by https://github.com/zaphoyd/websocketpp
In order to build the project I issue this command:
g++ -Wno-deprecated -I ./cppServer/libs/ -I C:\boostLibs\include\boost-1_55 -L C:\boostLibs\lib -g ./cppServer/server.cpp -lboost_system
Which leeds me to this error message:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lboost_system
collect2.exe: error: ld returned 1 exit status
If I try to build without -lboost_system, I get a very long exception, starting with:
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:223: undefined reference toboost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:224: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system10error_codeC1Ev':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:323: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system4errc20make_error_conditionENS1_6errc_tE':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:488: undefined reference to boost::system::generic_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost16thread_exceptionC2EiPKc'
So what am I missing? I can't figure it out right now.
Link to boost_system as -lboost_system-mgw63-mt-1_55, because this is what those files are called. See boost library naming for more details.
When you build boost you may like to specify --layout=system to b2.exe so that your files do not have that -mgw63-mt-1_55 in the filename and then just use -lboost_system when linking against it.

quantlib multithreading/enable-sessions build error on Linux

Getting below errors after compiling quantlib 1.9.2 with boost 1.5.4 on Fedora 20 with enable-sessions flag, and running one of the examples, any ideas?
cd QuantLib-1.9.2/
nohup ./configure --enable-sessions --enable-thread-safe-observer-pattern &
tail -f nohup.out
nohup make &
tail -f nohup.out
cp ql/.libs/libQuantLib.so.0.0.0 /lib64/libQuantLib.so
cp ql/.libs/libQuantLib.so.0.0.0 /lib64/libQuantLib.so.0
cd Examples/BermudanSwaption
g++ BermudanSwaption.cpp -o BermudanSwaption -lQuantLib
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/libQuantLib.so:
undefined reference to `pthread_mutexattr_destroy'
undefined reference to `QuantLib::sessionId()'
undefined reference to `pthread_mutexattr_init'
undefined reference to `boost::system::system_category()'
undefined reference to `pthread_mutexattr_settype'
undefined reference to `boost::system::generic_category()'
collect2: error: ld returned 1 exit status
Note: Im trying to enable sessions since getting segfault when quantlib is used in multi-threaded client application
answering my own question
1) the pthread and boost_system error disappeared after adding -lboost_system flag to compiler command line:
g++ BermudanSwaption.cpp -o BermudanSwaption -lQuantLib -lboost_system
this helped: undefined reference to boost::system::system_category() when compiling
2) to solve sessionId error I added sessionId function in BermudanSwaption.cpp as suggested here under QL_ENABLE_SESSIONS: https://github.com/lballabio/QuantLib/blob/master/ql/userconfig.hpp
"You will have to provide and link with the library a sessionId()
function in namespace QuantLib, returning a different session id
for each session"
after I commented out #if defined(QL_ENABLE_SESSIONS) in BermudanSwaption.cpp it compiled without errors:
//#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {
Integer sessionId() { return 0; }
}
//#endif

Compiling C++ source file using Boost.Thread

I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example:
#include <boost/thread.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void thread()
{
for(int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << std::endl;
}
}
int main()
{
boost::thread t(thread);
t.join();
}
However, when I compile this with the following from the command line:
$ g++ example61.cpp -o example61 -I /usr/local/include
I get the following output:
/tmp/cc6bVu1F.o: In function `main':
example6.cpp:(.text+0x9d): undefined reference to `boost::thread::join()'
example6.cpp:(.text+0xae): undefined reference to `boost::thread::~thread()'
example6.cpp:(.text+0xc6): undefined reference to `boost::thread::~thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data_base::thread_data_base()':
example6.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc6bVu1F.o: In function `void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)':
example6.cpp:(.text._ZN5boost11this_thread5sleepINS_10posix_time7secondsEEEvRKT_[void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)]+0x35): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/tmp/cc6bVu1F.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
example6.cpp:(.text._ZN5boost6threadC2IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE]+0x30): undefined reference to `boost::thread::start_thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
example6.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc6bVu1F.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
I don't know how to interpret this. Can anyone help? Thank you so much!
That is a linking error. It means your code is correct and you include the correct headers, but the compiler doesn't link against the boost threading library. To fix this, you need to compile like this:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread
If you've installed the Boost threading library to a non-standard path, you must also add it to the search path:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib
You need to link with the library. Some Boost libraries are implemented entirely in the header files and do not need a library. But others, like thread, are implemented partly in headers and partly in compiled library code.
I believe that you need to add -lboost_thread-mt to your compile command.
Boost thread are not a template only library. You need to add a -lboost_thread while linking /compiling.
Most of the libraries in boost are implemented in headers. They can simply be included like you have done. Boost thread on the other hand, is of such a nature that you need to depend on its compiled units, only the declaration of its function are readily available to you in the header. So the compiler, or more correctly the linker, which is responsible for linking your calls to the declared functions /classes need to know where to look for these symbols. By invoking the compiler with a -lboost_thread you tell it to link to the library (-l) boost thread.
Following your comments I share with you compilation string for pocketcpp compilation tool:
g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
Good luck,
I commented above, but wanted to share the full command line here.
g++ -std=c++11 thread_example.cpp -lboost_thread -lboost_system
[I'm using thread_example.cpp as the source filename; please replace with your own]

undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.