Failing to include Boost library - c++

I'm trying to run a example code from here.
I've installed the boost library by using
sudo apt-get install libboost-all-dev
and I've included it into my project like this:
QMAKE_CXXFLAGS += -std=c++0x -pthread
LIBS += -lpthread
LIBS += -lboost_system
(using QtCreator).
But I'm still getting the following error-messages:
/usr/lib/x86_64-linux-gnu/qt5/bin/qmake -spec linux-g++-64 CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../websocket/websocket.pro
g++ -m64 -o websocket main.o -lpthread -lboost_system
main.o: In function `websocketpp::random::random_device::int_generator<unsigned int, websocketpp::concurrency::basic>::~int_generator()':
/home/alex/C++/build-websocket-Desktop-Debug/../websocket/websocketpp/random/random_device.hpp:53: undefined reference to `boost::random::random_device::~random_device()'
main.o: In function `websocketpp::random::random_device::int_generator<unsigned int, websocketpp::concurrency::basic>::int_generator()':
/home/alex/C++/build-websocket-Desktop-Debug/../websocket/websocketpp/random/random_device.hpp:60: undefined reference to `boost::random::random_device::random_device()'
/home/alex/C++/build-websocket-Desktop-Debug/../websocket/websocketpp/random/random_device.hpp:60: undefined reference to `boost::random::random_device::~random_device()'
main.o: In function `boost::asio::detail::chrono_time_traits<boost::chrono::steady_clock, boost::asio::wait_traits<boost::chrono::steady_clock> >::now()':
/usr/include/boost/asio/detail/chrono_time_traits.hpp:45: undefined reference to `boost::chrono::steady_clock::now()'
main.o: In function `unsigned int boost::random::detail::generate_uniform_int<boost::random::random_device, unsigned int>(boost::random::random_device&, unsigned int, unsigned int, mpl_::bool_<true>)':
/usr/include/boost/random/uniform_int_distribution.hpp:66: undefined reference to `boost::random::random_device::operator()()'
/usr/include/boost/random/uniform_int_distribution.hpp:114: undefined reference to `boost::random::random_device::operator()()'
/usr/include/boost/random/uniform_int_distribution.hpp:201: undefined reference to `boost::random::random_device::operator()()'
collect2: error: ld returned 1 exit status
make: *** [websocket] Error 1
15:41:25: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project websocket (kit: Desktop)
When executing step 'Make'
15:41:25: Elapsed time: 00:00.
There also seem to be problems with the websocketpp library, but I think they are based on boost, too.
Thanks!

You also need:
LIBS+=-lboost_random

Related

Failure linking when using an extra library

When I build a simple C++ program with glog library (Google's logging library which is not used in the code), I get "undefined reference" errors. When I remove the -lglog from the build command, the link succeeds.
Notice that the library that I added to the link is not used in the code at all and despite that it caused the build to fail. In addition, the glog and log4cpp libraries are supposed to be independent.
Can you explain this unusual behavior?
Environment: Ubuntu 14.04
Code:
//test.cpp
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
int main() {
log4cpp::Appender *appender;
appender = new log4cpp::FileAppender("default", "program.log");
return 0;
}
Working build command:
$ g++ test.cpp -llog4cpp -lpthread
Failing build command:
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
EDIT:
This command also builds successfully:
g++ test.cpp -llog4cpp -lpthread -lglog
This command fails (change the order of libs):
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
This succeeds:
$ g++ test.cpp -pthread -llog4cpp
This fails:
$ g++ test.cpp -pthread -llog4cpp -lglog
EDIT 2:
I studied the duplicate suggestions (1) and (2) to find out maybe there's something useful to me there, but it turned out irrelevant because these cases doesn't address the situation where a library that is not used in the code is added to the link and make it fail.
EDIT 3:
The files from my environment (glog libs, log4cpp libs, used log4cpp headers and test.cpp): log_test.zip.

Undefined reference to pthread for Gtest

I have been scratching my head since yesterday trying to make gtest work but I just can't fix it after reading the links below.
undefined reference to `pthread_key_create' (linker error)
error during making GTest
The compilation error displayed is this:
g++ main.o tests.o var.o -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs
-lgtest -lgmock -lpthread -o test
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
My Makefile is:
CXXFLAGS=-I ../gmock/include -I ../gmock/gtest/include
test:main.o tests.o var.o
g++ $^ -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs -lgtest -lgmock -lpthread -o $#
I am still in the process of learning Linux, compiling and linking source files.
The Error is about linking error on pthread.
You have to make the pthread flag to -pthread and the follow CXX should do the trick.
LDLIBS = -L../gmock/lib/.libs -L../gmock/gtest/lib/.libs -lgtest -lgmock
test:main.o tests.o var.o
g++ -isystem $(LDLIBS) -pthread $^ -o $#
This link have a good Makefile which addresses all your problems and it follows general standards on creating a Makefile

compile with boost on ubuntu 14.04 : undefined reference to `boost::thread::get_id() const'

I am trying to compile a simple boost program which uses threading on ubuntu 14.04 ( using boost 1.54 ).
the output of compilation is like this:
09:30:25 **** Incremental Build of configuration Debug for project sdkTest ****
make all
Building target: sdkTest
Invoking: GCC C++ Linker
g++ -L"/home/khan/git/sdkLinux/libFingiSdk/Debug" -o "sdkTest" ./src/CommunicationCoreTest.o ./src/CoreTest.o ./src/LoggerTest.o ./src/ProgramOptions.o ./src/sdkTest.o -lfingidevicesdk -lz -lssl -lcurl -lpthread -lboost_system -lboost_regex -lboost_signals -lboost_thread -lboost_chrono -lboost_program_options -lboost_filesystem -lboost_iostreams -lboost_unit_test_framework -lpython2.7 -lboost_python
/home/khan/git/sdkLinux/libFingiSdk/Debug/libfingidevicesdk.so: undefined reference to `boost::thread::get_id() const'
/home/khan/git/sdkLinux/libFingiSdk/Debug/libfingidevicesdk.so: undefined reference to `boost::filesystem3::detail::status(boost::filesystem3::path const&, boost::system::error_code*)'
/home/khan/git/sdkLinux/libFingiSdk/Debug/libfingidevicesdk.so: undefined reference to `boost::thread::join()'
collect2: error: ld returned 1 exit status
make: *** [sdkTest] Error 1
09:30:25 Build Finished (took 413ms)
i have install boost by using
sudo apt-get install libboost-all-dev
and in the compilation i have added reference to libboost_thread , pthread and so on. ( infact this used to compile fine on ubuntu 12.10,boost 1.48).
Any idea what could be wrong here?
/home/khan/git/sdkLinux/libFingiSdk/Debug/libfingidevicesdk.so: undefined reference to `boost::thread::get_id() const'
You need to link libfingidevicesdk.so with the boost thread library as well.

Building Boost_1_55_0's example asynchronous TCP daytime server

I'm trying to build Boost's example of an asynchronous TCP daytime server found on this link:
http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html
Here what I am trying to build the example with:
g++ -o server server.cpp -I ~/boost/include -L~/boost/lib -lboost_system
Here are the errors I am getting:
g++ -o server server.cpp -I ~/boost/include -L~/boost/lib -lboost_system
/tmp/ccF38gvh.o: In function `__static_initialization_and_destruction_0(int, int)':
server.cpp:(.text+0x221): undefined reference to `boost::system::generic_category()'
server.cpp:(.text+0x22d): undefined reference to `boost::system::generic_category()'
server.cpp:(.text+0x239): undefined reference to `boost::system::system_category()'
/tmp/ccF38gvh.o: In function `boost::system::error_code::error_code()':
server.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/ccF38gvh.o: In function `boost::asio::error::get_system_category()':
server.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
What do I need to add to get this to successfully build?
I'm pretty sure your command line is different.
In particular, could it be you have the source listed /after/ the libs?
Specificly
g++ -o server -I "$HOME/boost/include" -L"$HOME/boost/lib" -lboost_system server.cpp
would not work, and
g++ -o server server.cpp -I "$HOME/boost/include" -L"$HOME/boost/lib" -lboost_system
should work.
Other than that, see also c++ files to include for boost : asio. The jam-file for the tutorials build everything with -lboost_system -lboost_thread -DBOOST_ALL_NO_LIB=1 (which is likely overkill)
Update Also found this How to compile boost async_client.cpp

g++ cannot link to libdl even with -ldl flag

I am attempting to compile an example application for a USB camera (mvBlueFOX) sold by Matrix Vision. They provide me with the source code for the application, a make file, and a set of pre-compiled shared libraries. However, the make file fails to successfully build an executable. A "make" returns:
carter#carter-Lenovo-G780:~/mvimpact_acquire-x86-1.12.22.254/apps/LiveSnap$ make
make[1]: Entering directory `/home/carter/mvimpact_acquire-x86-1.12.22.254/apps/LiveSnap/x86'
/usr/bin/g++ -O2 -Wall -W -fPIC -D_REENTRANT -D_GNU_SOURCE -D_MAJOR_VERSION=1 -D_MINOR_VERSION=12 -D_BUILD_VERSION=22 -D_BUILD2_VERSION=254 -DMALLOC_TRACE -DNDEBUG -I../../.. -o LiveSnap LiveSnap.o -L../../../lib/x86 -lmvDeviceManager -lmvPropHandling -lm -lpthread -ldl
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlsym'
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlopen'
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlclose'
collect2: ld returned 1 exit status
make[1]: *** [LiveSnap] Error 1
make[1]: Leaving directory `/home/carter/mvimpact_acquire-x86-1.12.22.254/apps/LiveSnap/x86'
make: *** [all] Error 2
A simplified version of the link command:
g++ -o LiveSnap x86/LiveSnap.o -lmvDeviceManager -ldl
Still returns:
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlsym'
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlopen'
../../../lib/x86/libmvPropHandling.so: undefined reference to `dlclose'
libdl.so is being found, however the process still fails. Is this an issue with my version of libdl or is there another problem?
If your gcc version is above 4.6.2 just add the flag '-Wl,--no-as-needed' before -lmvDeviceManager
For those interested in a work around to this problem, I was able to successfully build and run a newer version of the code that I downloaded from the Movie-Matrix website. The version provided on the CD with my camera was 1.2.22 while the version from the website was 2.5.2. Why the older version fails to build is still unknown, but the problem seems to be solved in newer versions of the software.