compiling boost.asio using g++ compiler - c++

I'm new to g++ compiler and also trying to start coding with boost.asio libraries, but have some difficulties compiling the code using g++.
I have installed boost libraries usingsudo apt-get install libboost-all-dev and boost folder is located in /usr/include directory, and whole scenario:
geek#ubuntu:~/workspace/HelloBoost/src$ g++ -I /usr/include/ -l boost_asio HelloBoost.cpp
/usr/bin/ld: cannot find -lboost_asio
collect2: ld returned 1 exit status
Thanks for your answers.

Did you read about using boost.asio ? Apparently, it looks like a headers only library, and you only need to link -lboost_system -lboost_regex -lopenssl

Related

C++ Boost: undefined reference to `boost::system::detail::system_category_ncx()`

I am trying to use Microsoft REST SDK to create a server listening to HTTP requests. My current computer is Ubuntu 18.04, the server code is pretty simple.
I have already installed the library by following the instructions on libboost official website and I have checked files in /usr/local/lib/, there are libboost_random.so.1.68.0 so I presume that I have libboost library.
However when I try to compile the code with the following command:
g++ server.cpp -o server -lpthread -lcpprest -lcrypto -lssl -lboost_system
It shows:
/tmp/ccKWADuo.o: In function `boost::system::system_category()':
server.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x7): undefined reference to `boost::system::detail::system_category_instance'
/tmp/ccKWADuo.o: In function `boost::system::generic_category()':
server.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7): undefined reference to `boost::system::detail::generic_category_instance'
collect2: error: ld returned 1 exit status
Thanks for any advices.
Boost consists of several independent libraries. Your linker error suggests you are missing libboost_system.so. You just mentioned libboost_random.so to be in your /usr/local/lib but is there also the required libboost_system.so?
[Problem solved] Reinstalling library libboost 1.68.0 helps. Error occurs when building from source, fixing those building errors will solve the problem.

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.

undefined reference to `shm_open' already with -L /lib -lrt -lpthread

I just want to use the boost library to create a shared memory on an ARM system. It work fine if you want to compile it only under ubuntu. However, when I want to cross compile it with TI's CCSv6 and angstrom toolchain, it keep pushing errors.
Because I do not know how to write a makefile for cross compile, I think using TI their own IDE might be a good choice to avoid further problems.
Here is my code and print out of build console.
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
int main()
{
shared_memory_object shdmem{open_or_create, "Boost1", read_write};
shdmem.truncate(1024);
mapped_region region{shdmem, read_write};
}
g++ -std=c++0x -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -L /lib -lrt -lpthread -fPIC
The IDE called Code Composer Studio has cross compile settings as below:
Prefix: arm-angstrom-linux-gnueabi-
Path: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/armv5te-angstrom-linux-gnueabi
Build Console:
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference toshm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference toshm_open'
collect2: ld returned 1 exit status
make: *** [test] Error 1
undefined reference to shm_open' means it cannot find -lrt for ARM.
In your build command line you need to specify include and library paths to ARM built libraries, not to Ubuntu ones. So -I/usr/include and -L /lib is wrong.
Also you need boost built for ARM, although if you just want to use interprocess library then boost headers should be enough. But you need to copy them into different location because including them from /usr/include includes also other headers specific to Ubuntu.
You can use the cross compiler IDE you mentioned or arm g++ cross compiler which you can install by:
sudo apt-get install g++-arm-linux-gnueabihf. Some headers and libraries for ARM will be installed too.

How to Link lapack and BLAS library to C++ code

The above are linear algebra libraries. i am using armadillo which is like a c++ wrapper/framework for linking to more basic linear algebra libraries in fortran.
I can compile the example.cpp easily by having a #include "armadillo" using gcc -c option with the -I flag.
This generates example.o which is now supposed to be linked to liblapack_LINUX.a and libBLAS_linux.a Statically
Locations of the two:
liblapack_LINUX.a :-/home/nimish/HTMLProjects/WP2/lib/lapack/
libBLAS_linux.a :-/home/nimish/HTMLProjects/WP2/lib/blas
I issue the following command:
gcc -o example.o -L../lib/blas -lblas_LINUX -L../lib/lapack -llapack_LINUX
OR with absolute paths
nimish#ubuntu:~$ gcc -o example.o -L/home/nimish/HTMLProjects/WP2/lib/blas -lblas_LINUX -L/home/nimish/HTMLProjects/WP2/lib/lapack -llapack_LINUX
to get the error:
/usr/bin/ld: cannot find -lblas_LINUX
collect2: ld returned 1 exit status
However the libraries do exist --
nimish#ubuntu:~$ find /home/nimish/HTMLProjects/WP2 -name liblapack_LINUX.a
/home/nimish/HTMLProjects/WP2/lib/lapack/liblapack_LINUX.a
nimish#ubuntu:~$ find /home/nimish/HTMLProjects/WP2 -name libblas_LINUX.a
/home/nimish/HTMLProjects/WP2/lib/BLAS/libblas_LINUX.a
What am i doing wrong? I am somewhat new to this linking libraries business as well as gcc.
first you need to ensure you have BLAS installed using(+lapack)
sudo apt-get install libblas-dev liblapack-dev
then you can link using -lblas after your program files. or you can use a make file.
from my side I prefer to use OpenBlas instead, you can use the following in a makefile.
www.openblas.net, get the tar.gz
copy it in your directory
extract it : tar -zxvf OpenBLAS-0.2.20.tar.gz
compile it : cd OpenBLAS-0.2.20
make
When it is done you should have the file libopenblas.a, the openblas library
BLASLIB = OpenBLAS/libopenblas.a -lpthread

linker can't find the lib, but g++ can

I use Debian Jessie on amd64. I can't build the program, which uses Boost threads. Makefile http://pastebin.com/83h8MeUP returns http://pastebin.com/qffPLnCi . ld can't find -lboost_thread-mt (But g++ can). How can I make ld to see this lib?