C/C++: how to figure out link flags? - c++

How do I reliably figure out link flags for libraries? I always end up googling/digging manuals.
Is there a way to list libraries available for linking, with names and/or descriptions?
edit: Linux system, GNU build chain, classics.

On most Linux systems, you can use pkg-config to list out the compiler options for a given library. For example:
g++ example.cpp $(pkg-config --cflags --libs libpng)
becomes
g++ example.cpp -I/usr/include/libpng12 -lpng12
Or an example with slightly more complicated output:
$ pkg-config --cflags --libs gthread
-D_REENTRANT -I/usr/include/glib-1.2 -I/usr/lib64/glib/include -lgthread -lpthread -lglib

Related

Permantly provide path to the headers and linker flags in OpenCV Linux

I have OpenCV 4.5.1 installed in Linux. But each time I restart I have to separely provide path to the headers and linker flags. and then compile ?
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/parallels/opencv4.5-custom/lib/pkgconfig
pkg-config --cflags --libs opencv4
and then
g++ -Wall -std=c++11 -o main main.cpp $(pkg-config --cflags --libs opencv4)
I just want to run the g++ comand each time

compiling c++ projects with dlib library on linux

I want to use dlib library for my c++ projects in linux. I have installed it successfully and able to compile and run the .cpp samples files given under the dlib. I have compiled the sample files through the
"g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp" given in [http://dlib.net/compile.html][1]
but I am unable to use the same command to run .cpp files which are the outside dlib-18.18/examples directory
Can someone help me out with compiling c++ file (with dlib library usage) from any user directory in linux?
I came to know that we have to include the path of dlib installation folder while trying to compile it from any other user directory. So I tried the command
" g++ -O3 -I/home/praneeth/computervision/.. ../dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv"
but it gave me the result:
g++: error: ../dlib/all/source.cpp: No such file or directory
Note: dlib-18.18 folder is present in my /home/praneeth/computervision/
I don't know how correct is this but it got compiled when I ran the command like: g++ -O3 -I/home/praneeth/computervision/dlib-18.18 /home/praneeth/computervision/dlib-18.18/dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv any comments on this regarding why this works?
g++ -Wl,-V -std=c++11 -o3 -I/home/user/dlib-19.6 /home/user/dlib-19.6/dlib/all/source.cpp -lpthread -lX11 -o Test Test.cpp pkg-config opencv --cflags --libs

Linking OpenCV libraries with Eclipse Ubuntu [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
OpenCV on ubuntu 11.10
I am having a very hard time trying to install OpenCV on Ubuntu. I believe that I have already installed OpenCV; however, I am trying to compile one of the samples program kalman.cpp with Eclipse, and I am not able to compile it. My problem I believe is linking with the libraries.
I have seen a lot of tutorial, and I do not understand why after installing opencv in my computer, I get the following output after using the command pkg-confi opencv --libs:
manuel#manuel:~$ sudo pkg-config opencv --libs
/usr/local/lib/libopencv_calib3d.so
/usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so
/usr/local/lib/libopencv_features2d.so
/usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so
/usr/local/lib/libopencv_highgui.so
/usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so
/usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so
/usr/local/lib/libopencv_objdetect.so
/usr/local/lib/libopencv_photo.so
/usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so
/usr/local/lib/libopencv_video.so
/usr/local/lib/libopencv_videostab.so
Every tutorial out there the libraries appear as:
-L/where/you/have/installed/opencv/lib -lcxcore -lcv -lhighgui -lcvaux
This is really annoying because Eclipse cannot find the library as libopencv_contrib.so. It is waiting for something as -lopencv_contrib
I really appreciate the help. Please let me know what I am doing wrong.
I have never used OpenCV with Eclipse. I basically compile it using gcc or g++ (depending on c or C++) file.
for C file,
$ gcc -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.c .c` opencvtest.c `pkg-config --libs opencv`
for C++ file,
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`
For more information, see http://jayrambhia.wordpress.com/2012/05/08/beginning-opencv/
If you are comfortable with this, I don't think you would need to use Eclipse.
Hope this helps.
To link to a library you need to specify the path to the directory where it is located using the -L /path/to/libraries flag.
You also need the specific libraries you want using -l my_library.
Usually you also need to specify the necessary include paths using -I /path/to/headers
pkg-config can be used as a helper to do this, as it returns the exact parameters you need in order to use a library.
You should rather use it like this:
echo `pkg-config opencv --cflags --libs`
resp.
g++ my_first_opencv_app.cc `pkg-config opencv --cflags --libs`
which on my system evaluates to
g++ my_first_opencv_app.cc -I/usr/include/opencv -lml -lcvaux -lhighgui -lcv -lcxcore
To get it to work with eclipse, you probably need to specify the include path (/usr/local/include/opencv ?), the library path (/usr/local/lib ?) and the libraries you need via some GUI element somewhere in the project settings. You probably shouldn't need pkgconfig then.

GCC linker error while compiling a static library

I want to create a static library of all my files so that i could give my mylib.a file to others to execute on their system. I use opencv library in my code. I used the following command to compile my code.
g++ index.cpp -o display1 -Wl,-Bdynamic pkg-config --cflags --libs opencv -lglut -lGL -lGLU -Wl,-Bstatic mylib.a
But it is giving the following error.
/usr/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status
I believe the Kerrek SB is right in the comment. The command should be
g++ index.cpp -o display1 mylib.a $(pkg-config --cflags --libs opencv) -lglut -lGL -lGLU
Explanation:
The -Wl,-Bdynamic and -Wl,-Bstatic flags are useless. The linker automatically picks static or dynamic library depending on what it finds. If you give it path to a library (as you do with mylib.a) it can't choose and will link the library you provided. If you give it an -lX flag, it will look for libX.so or libX.a and link whichever it finds, but most Linux installations won't have static variants of system libraries, so there is nothing to choose from either.
It's worse, the -Wl,-Bdynamic and -Wl,-Bstatic are wrong. -Wl,-Bstatic prohibits linking of shared libraries. That has the side-effect of selecting static libgcc, which implicitly comes last on the linker command line. And you don't seem to have that installed. Most Linux systems don't.
Each object must be listed on the command-line before the libraries it refers to. I would expect mylib.a contains functions that need opencv or opengl, so it must be listed before those -l flags.

Unable to compile basic GLIB program after GLIB install

I can't seem to compile this basic program using glib.h...
#include glib.h
#include stdio.h
int main ()
{
return ((glib_major_version) || (glib_minor_version) || (glib_micro_version)); ;
return 0;
}
glib.h is located in /usr/local/include/glib-2.0
So I compiled with
$ gcc -v -c -mcpu=v9 -I/usr/local/include/glib-2.0 testme2.c
Now I get missing glibconfig.h. But it is in /usr/local/lib/glib-2.0/include/glibconfig.h
Strangely glibconfig.h is the only file in /usr/local/lib/glib-2.0/include directory and more strangely it is not in /usr/local/include/glib-2.0 directory
Here are some more error messages...
from /usr/local/include/glib-2.0/glib.h:32,
from testme.c:40:
:34:24: glibconfig.h: No such file or directory
Here is an extract of /usr/local/include/glib-2.0/glib/gtypes.h
ifndef __G_TYPES_H__
define __G_TYPES_H__
include glibconfig.h
include glib/gmacros.h
G_BEGIN_DECLS
typedef char gchar;
typedef short gshort;
The question is how is GCC supposed to find glibconfig.h?
Glib installs a glib-2.0.pc file that describes all the options necessary to compile and link.
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
g++ -c `pkg-config --cflags glib-2.0` testme2.c
g++ -o testme2 testme.o `pkg-config --libs glib-2.0`
Note the use of pkg-config within backquotes.
$ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lglib-2.0
It is advisable to use pkg-config instead of manual configuration if .pc files for desired libraries exist and fall back to manual configuration if you have specific needs or no configuration for the library you are going to use exists. As you can see, pkg-config tells the compiler to put both glib-2.0 and glib-2.0/include directories into the search path as the root header searches in the global path.
You can infer pkg-config output into your compilation command via
gcc `pkg-config ...` ...
.pc files are usually installed in /usr/include/pkgconfig
There should be a program in the glib distribution called glib-config. If you run it with the --cflags argument, it will list all the gcc flags necessary. For example on my system:
$ glib-config --cflags
-I/usr/include/glib-1.2 -I/usr/lib/glib/include
As you can see, both directories are specified as include directories. There is also a --libs flags, which you can pass to your linker, so all the correct libs are linked, and the linker search path is correctly specified.