C++ and 3rd party libraries - c++

I'm rather new to programming and I need help with libraries. I'm having trouble understanding how to use the libraries. I simply don't know what to do after I download them. They usually have instructions but they're not easy to follow when you're a beginner I suppose.
After they're downloaded I'm faced with a bunch of stuff. Some people say I need to build them but I don't know how to do that. Also there's some "header only libraries" and I don't know what to do with those as well.
Example of what I mean, Boost & portaudio (or any other library), I don't know where to start or what to do.
Can someone help me, please!

First, see if there’s a distribution for your OS. On Ubuntu, installing the package through the software manager will install all the files you need for boost in the system directory, /usr/include/. This is by far the simplest solution.
If this is not an option, you need to build the library from source and install it in a directory of your own choice. This will usually be your home directory or /usr/local. Typically, the commands for this will be something like:
tar xzvf library-x.y.z.tgz
cd library-x.y.z
less README
./configure --help
./configure --prefix=/usr/local --other=options --as=appropriate
make
sudo -u bin -g bin make install
make clean
You might then need to add -I/usr/local/include and -L/usr/local/lib to your compiler command line.
For some libraries, you might need to look up the directories to include with a tool such as pkg-config. If you need to do this, the commands would be:
export CFLAGS=$CFLAGS `pkg-config --cflags library`
export LDFLAGS=$LDFLAGS `pkg-config --libs library`

Related

How to use FLANN installed from synaptic

I tried to compile FLANN with cmake, but the only result was a giant headache.
So I found here this solution through PCL repository and synaptic. The installation seems gone well, but now I don't know how to use the installed package.
Quoting FLANN's documentations :
An example of the compile command that must be used will look
something like this: g++ flann_example.cpp -I $FLANN_ROOT/include -o flann_example_cpp where $FLANN ROOT
is the library main directory.
But it's not clear to me where $FLANN_ROOT is.
The $FLANN_ROOT is a path where the library was installed. This is mostly relevant when you build and install manually (especially when installing to non-standard locations).
When installed by the packaging system (Synaptic - I guess Ubuntu?) the library headers will be most likely installed in '/usr/include' or '/usr/local/include'. Normally you do not have to use the -I then as those paths are examined by default.

Some doubts about libraries

Say, I want to write a program in C++ in Linux and I need some specific libraries. There are 2 ways of getting these libraries:
Using the command line--> apt-get install library
Downloading and extracting a .zip or .tar file from their website.
Now my questions are:
For the first method I have seen libraries being downloaded with apt-get install library and apt-get install library-dev. I know dev means development or developer, but what is the difference between installing the dev and not installing the dev? What does dev do, exactly?
For the second method, do I need to build the libraries using a compiler? Because I have seen tutorials doing it but the OS used was usually Windows, do I only have to build them on Windows and not Linux?
Also, say I can only use the 2nd method for a certain library and not the first one. After extracting, what am I supposed to do? Is there any default way of installing a library manually or is each library different?
Finally, when I use the first method where is the library installed to? Is it /usr/local/lib, /usr/lib or /usr/include? Because when I have to link to these libraries in the Linker's settings I only write their name, not the path so I assume there is already a default path for libraries to be in.
One last question: Is there any default way of installing and using libraries in general or does that depend on what I want to do, programming language, etc...?
The second method is very broad because it depends entirely on the how the project is designed including the build system used etc. Things get a little more conformant when you use a distribution's managed packages.
If you want to develop a program that uses the library you need the library-dev package that usually contains the C/C++/etc.. header files.
Many development package conform to a standard tool that helps your build system find the libraries header and binary files.
For example libcurl uses the pkg-config system so its compiler components can be found from the command line like this:
pkg-config libcurl --libs # print the library link flags
You can then add that to your Makefile (or whatever build system you use):
program:
g++ -o program program.cpp $(shell pkg-config libcurl --libs)
The $(shell pkg-config libcurl --libs) part adds the correct compiler flags to link with the library.
Not all dev packaged use pkg-config. Some come with their own tools (like mysql_config) while others let you guess and try to figure it all out for yourself (looking at you libclang).

Automatically collect and output all C++ dependencies

Problem
I need my code to compile on a machine that cannot leverage apt-get to install nonstandard C++ libraries but my C++ program has a single #include for an external library that I must use. That library includes many headers and other libraries, meaning that my makefile has many -l and -I to /usr/include/... and /usr/lib/... pointing to my apt-get installed libraries.
What I want
Rather than manually going through and grabbing all of these libraries so that I can include them in my project directory, I am hoping there is a command or flag for make or g++ that will dump all the libraries and headers into a directory for me.
Progress
This and this have helped because adding the -MMD flag to my g++ compile command will dump out a list of about 100 header file locations. But I would like g++ to go one step further and actually do the work of gathering them all together for me, if that feature exists.

How to build Leptonica library under Google pnacl-clang++

Does anyone know how to build Leptonica library under pnacl-clang++.
I can build the library using clang++ compiler.
cd leptonica-1.71
./configure CC="clang" CXX="clang++" --enable-shared
make
I don't know how to configure pnacl-clang++ to build Leptonica library.
Per Google documentation, This is how every file is compiled separately.
e.g.
nacl_sdk/pepper_<version>/toolchain/win_pnacl/bin/pnacl-clang++ \
hello_world.cc -Inacl_sdk/pepper_<version>/include -c \
-o hello_world.o -g -O0
But, I don't want to run pnacl-clang++ separately on every individual file in the library folder. I must configure it.
Thanks.
PNaCl expects everything to be build as static libraries, not shared, though that's being worked on. For most projects the effort of targeting PNaCl will involve creating a static library build, and setting CC/CXX to pnacl-clang/pnacl-clang++.
Open source projects are often already ported on naclports (and regression-tested), but it looks like leptonica isn't one of these. I suggest looking at other projects' diff to understand what goes into building for PNaCl, and contributing your port of leptonica.
There's a bit more documentation on building.
I hope this is useful in building any project, not just leptonica :-)

How to use SOCI C++ Database library?

I'm trying to implement soci in my program but I don't know how. I'm using C++ on Linux, on a project using netbeans. I have followed the steps in: http://soci.sourceforge.net/doc/structure.html to install it, and I tried to copy the files soci.h from /src/core and soci-mysql.h from /src/backends/mysql in my project but it gives a compilation error (these files include other soci files, but it's illogical to copy all files into the directory...). I have read the guide several time but I don't understand what I'm doing wrong. The examples only include these files.
Thanks.
Edit: I have given more information in a comment below the answer. I don't know what steps I have to follow to implement soci.
The relevant bit on that page is
When the configure script is run without parameters, the remaining part of the process will use /usr/local/include/soci as a default destination for SOCI header files and /usr/local/lib as a default destination for library files
Now /usr/local/include ought to be in your default include path (e.g. try something like gcc -c -v -x c++ /dev/null -o /dev/null to see the list your install uses) and so you can include these using
#include <soci/soci.h>
#include <soci/soci-mysql.h>
You then need to add the libraries to your link step. It looks like you'll have both static and shared versions of the libraries. You'll need to add -lsoci_core -lsoci_mysql to your link step; however if that doesn't work then you'll also need to specify /usr/local/lib as a search directory i.e. -L/usr/local/lib -lsoci_core -lsoci_mysql. (Again it's probably there already but you can see using gcc -print-search-dirs.) However, the issue then is that if you're using the shared version and /usr/local/lib isn't in your distributions library search path (see /etc/ld.so.conf and/or /etc/ld.so.conf.d/*) then it won't be able to find the shared library at runtime. You'll need to either hard-code in the path to the library with the linker switch -rpath or add /usr/local/lib to the system-wide search path as before or in your environment (variable LD_LIBRARY_PATH). I'm not sure what the best way to do this is - I'd suggest -rpath to avoid modifying the system in general, although if you're building a lot of libraries into /usr/local/lib it might make sense to add it.
I got the same doesn't load backend error on my C++ program when I execute session sql("mysql://db=...)
I found a solution (at least on my Ubuntu 11.04). Just do:
sudo -i ln -s /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so /usr/lib/libsoci_mysql.so
It seem that the SOCI library search for the file /usr/lib/libsoci_mysql.so that is not in the system, buy if you make a link to the library /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so that it's in the system it works (I think debian/ubuntu makes a file name change from the original name, but it have side effects because the SOCI library search inside for the original name).
I found the error using the bash environment variable LD_DEBUG=files and running my C++ binary.
Hope it helps.