Boost.Asio linking error - c++

I'm trying access an extern device via a serial port and want to use Boost.Asio for this propose.
I have build the boost libraries for MinGw and compiled the regex example successful.
But I have problems to compile my code if I include something from Boost.Asio:
#include <boost/asio/serial_port.hpp>
int main() {
return 0;
}
g++ -D _WIN32_WINNT=0x0501 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\SerialPortTest.o ..\src\SerialPortTest.cpp
g++ -LC:\boost-libs\boost\bin.v2\libs\thread\build\gcc-mingw-4.5.2\release\link-static\threading-multi -LC:\boost-libs\boost\bin.v2\libs\system\build\gcc-mingw-4.5.2\release\link-static\threading-multi -oSerialPortTest.exe src\SerialPortTest.o -lboost_thread-mgw45-mt-1_48 -lboost_system-mgw45-mt-1_48
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `WSAStartup#8'
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to `WSACleanup#0'
collect2: ld returned 1 exit status
For me it seems to be a linking problem, but I don't get it.

Add -lws2_32 flag to link against WinSockets library.
Also, this might be useful: MinGW linker error: winsock

You miss wsock32 library. Add this to your dependencies and it should work.

Related

How to link/include c++ libraries correctly

I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.

Boost library issues

I am trying to compile a simple c++ program using g++ which contains boost includes but i am getting the following error. I installed it from the tar file found in the boost site. I get the following error only with the asio library.
$ g++ -std=c++03 -Wall -pedantic -g -O2 tcp.cpp -lboost_system -lboost_date_time -lboost_thread -lboost_asio
/usr/bin/ld: cannot find -lboost_asio
collect2: error: ld returned 1 exit status
Boost Asio is header only.
Drop -lboost_asio
ASIO doesn't have a library built for it, so indicating -lboost_asio as an option doesn't really make sense.
However, ASIO does have a dependency on boost.system, which does require a library be built for it.
Make sure you've properly built the boost library in the first place (correct optimization flags, correct Runtime-Library linking, correct architecture, and so on), and that the generated library files (the .so files, in your case) are accessible to the executable.

linking error in boost asio

I am getting below linking error while compiling a simple piece of code using pthread.
The system i am working on is x86_64 (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) and am compiling as -
gcc -lpthread ~/temp/temp.cpp -lrt -L"/usr/lib/x86_64-redhat-linux5E/lib64/".
Its evident that the problem is with pthread library. So i have two questions.
Q1. is it fine to use x64 pthread libs with x86 code. (In this piece of code it doesnt matter but it does in my usage) (I guess its not but how might i differentiate between x64 and x86 libs?)
Q2. i see pthread and lrt both are present at /usr/lib but even if i give that path it says -lpthread not found.
What am i missing here? Thanks for your help.
Error :
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/tmp/cc2GQOUf.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Code :
#include <stdio.h>
#include <pthread.h>
int main()
{
pthread_t f1_thread;
return 0;
}
In compilation phase,compiler can use gcc or g++,but g++ will call gcc command automatically.But in link phase,linker can use g++ or gcc -lstdc++.Because the gcc command cannot link with the libraries that be used by c++ program automatically, so usually use g++to complete the link.
So the command g++ -g -lpthread ~/temp/temp.cpp -o temp should be ok.

c++ files to include for boost : asio

I'm following this tutorial however it does not state which libaries I need to include in order to get boost to work,current options for links are:
-I/usr/include/opencv2 -I/usr/include/boost_1_55_0 -I/usr/include/boost_1_55_0/boost -O0 -g3 -Wall -c -fmessage-length=0
however this returns the following erro:
which states that it can't find asio, am I doing something wrong or was assio the wrong library to link to? Or is there any other way to find out. Note that this is my 2nd c++ project(through I have a lot of java experience) and first with the heavy use of libraries so details are somewhat required.
Removing boost/asio gave me the following errors:
make all
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/include/opencv2 -L/usr/include/boost_1_55_0/boost -L/usr/include/boost_1_55_0 -L/usr/include/opencv2 -L/usr/lib -o "DisplayImage" ./src/Cap.o ./src/DisplayImage.o ./src/Filters.o ./src/sender.o -lopencv_imgproc -lopencv_highgui -lopencv_core
./src/sender.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost_1_55_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/include/boost_1_55_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/include/boost_1_55_0/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
./src/sender.o: In function `error_code':
/usr/include/boost_1_55_0/boost/system/error_code.hpp:323: undefined reference to `boost::system::system_category()'
./src/sender.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost_1_55_0/boost/asio/error.hpp:224: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [DisplayImage] Error 1
Build Finished **
I use an ubuntu (x64) laptop if it matters.
Most of boost is implemented in what's called "header-only" code. Through the generous use of C++ templates, there is no actual library code to which your code needs to link. However, there are, as you've seen some actual libraries as well. Generally, the help you seek is probably here: http://www.boost.org/doc/libs/1_55_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library
Your particular program uses the timer and system libraries and so you can probably use this command line to link your program:
g++ timer.cpp -o timer -lboost_timer -lboost_system
You can look at the bjam in boost/libs/asio/example/cpp03/tutorial/Jamfile.v2:
project
: requirements
<library>/boost/system//boost_system
<library>/boost/thread//boost_thread
<define>BOOST_ALL_NO_LIB=1
<threading>multi
<os>SOLARIS:<library>socket
<os>SOLARIS:<library>nsl
<os>NT:<define>_WIN32_WINNT=0x0501
<os>NT,<toolset>gcc:<library>ws2_32
<os>NT,<toolset>gcc:<library>mswsock
<os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
<os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
<os>HPUX:<library>ipv6
;
You can see that they build all the tutorial steps with
-lboost_system -lboost_thread -DBOOST_ALL_NO_LIB=1
on linux

Trouble with the compilation using libcurl on Windows XP

I downloaded the libcurl curl-7.34.0-devel-mingw32.zip from http://curl.haxx.se/gknw.net/7.34.0/dist-w32/curl-7.34.0-devel-mingw32.zip.
I use Eclipse Kepler with MinGW as the toolchain (GCC compiler). I created html.cpp and then test the code from the example http://curl.haxx.se/libcurl/c/simple.html:
I included the headers:
#include<iostream>
#include<curl/curl.h>
using namespace std;
There are 5 cURL functions. Only 2 of them (curl_easy_init,curl_easy_setopt)are said to be 'undefined reference'.
The compiler message:
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o html.o "..\\html.cpp"
g++ -static-libgcc -static-libstdc++ -o HTML.exe html.o
html.o:html.cpp:(.text.startup+0x12): undefined reference to `_imp__curl_easy_init'
html.o:html.cpp:(.text.startup+0x1e): undefined reference to `_imp__curl_easy_setopt'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: html.o: bad reloc address 0x1e in section `.text.startup'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
The other 3 functions said to be fine:
curl_easy_perform
curl_easy_strerror
curl_easy_cleanup
I have tried to set to properties to tell the MinGW linker to include the headers and library files. The same situation as the above or the compiler simply says the .a files not found. Adding -lcurl or -DCURL_STATICLIB to the linker's flag is useless as well.
I have also tried to copy the libraries(.a files) and the headers(.h files) of libcurl to the folders bin, include and lib inside C:\MinGW and C:\gcc-4.8.1. It turns out to be no change.
Sadly, the first challenge of using cURL is not coding but the compilation. How can I solve this problem?
Thanks you guys!