Building Boost_1_55_0's example asynchronous TCP daytime server - c++

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

Related

Netbeans : Adding "Compile Line - Additional Options" "-lboost_system" at the end of compile command

I am using Netbeans as IDE and have been trying to build a piece of code which uses boost library, but I am getting below error
g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/tcpproxy_server.o.d" -o build/Debug/GNU-Linux-x86/tcpproxy_server.o tcpproxy_server.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function __static_initialization_and_destruction_0(int, int)':
/usr/include/boost/system/error_code.hpp:221: undefined reference toboost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::system::error_code::error_code()':
/usr/include/boost/system/error_code.hpp:322: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:230: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::thread_exception::thread_exception(int, char const*)':
/usr/include/boost/thread/exceptions.hpp:51: undefined reference toboost::system::system_category()'
collect2: error: ld returned 1 exit status
So I did some online search for error and found out that I have to add "-lboost_system" in my compile command.
I added it in "project->properties->build->c++ compiler-> Additional Options" but still the same error.
g++ -lboost_system -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function __static_initialization_and_destruction_0(int, int)':
/usr/include/boost/system/error_code.hpp:221: undefined reference toboost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::system::error_code::error_code()':
/usr/include/boost/system/error_code.hpp:322: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:230: undefined reference toboost::system::system_category()'
build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::thread_exception::thread_exception(int, char const*)':
/usr/include/boost/thread/exceptions.hpp:51: undefined reference toboost::system::system_category()'
collect2: error: ld returned 1 exit status
I found out that I have to add it in the end of compile line, for example :
"g++ tcp_proxy.cpp -o tcpproxy -lboost_system"
This I tried and its working, but netbeans is adding "Addition Options" at the start
like:
g++ -lboost_system -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o
Is there any way I can configure netbeans to add my option in end ?
So, I did some more search and found a question with somewhat my problem
link to the post :
C++ Boost on linux via Netbeans remote developement: undefined reference to boost::filesystem::path::codecvt()
This says that I can use following way to link a library:
Project Properties > Linker > Libraries > Add Library > Select the .a files.
This somewhat solves my issue, I am getting success with this try now:
g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/tcpproxy_server.o.d" -o build/Debug/GNU-Linux-x86/tcpproxy_server.o tcpproxy_server.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o -lboost_system
But I am still not sure If this is the right approach OR why adding it in addition compiler options not working.

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.

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.

Can we put two files with main() in makefile?

I have client server code client.cpp and server.cpp both with main().
Server need to be executed first and remain alive till not uninterrupted.
server.cpp includes two cpp files that I have created:
#include "serverFunction.cpp"
#include "serverFunction2.cpp"
Both of this also include serverFunction.h .
How to write makefile for this? I have used pthread so -lpthread at the end.
Individually I compile in this manner:
g++ -o a LinServer.cpp -lpthread
I tried with this:
all: LinServer LinClient
LinServer:
g++ -o a LinServer.cpp -pthread
LinClient:
g++ -o b LinClient.cpp -pthread
But it gives this error:
LinServer.o: In function `main':
LinServer.cpp:(.text+0x6dd): undefined reference to `pthread_create'
LinServer.cpp:(.text+0x6e9): undefined reference to `pthread_detach'
LinServer.o: In function `__static_initialization_and_destruction_0(int, int)':
LinServer.cpp:(.text+0xb3e): undefined reference to `std::ios_base::Init::Init()'
LinServer.cpp:(.text+0xb55): undefined reference to `std::ios_base::Init::~Init()'
LinServer.o:(.eh_frame+0x7b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
make: *** [LinServer] Error 1
You have specified the library incorrectly:
g++ -o a LinServer.cpp -pthread
It should be -lpthread, not -pthread (the -l option means compiling with library).
Your problems are not related with having two main() functions, but the answer is - yes, you can compile two files with main() function in the same Makefile, but only if the files belongs to different output files (different binaries).
Your error messages look like there is something wrong with your linker settings or with your configuration of standard C++ library (the linker seems unable to see it).

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.