Unable to compile basic GLIB program after GLIB install - c++

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.

Related

Can't link GLFW library with it's header file on Ubuntu 18.04

I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:
g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o
My current c++ file is:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit())
return -1;
}
I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:
g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw
driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1
I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?
Have you tried swapping the linker arguments around? That is, compile with
g++ -o output driver.o -lglfw
The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.
All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig
The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h. I suppose this is a wrong path to glfw3.h. compilation was successful because of the system default include path -I/usr/include.
Do not tune compiler flags manually, let pkg-config do
it for you.
A typical compile and link command-line when using the static version of the GLFW library may look like this:
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`
If you are using the shared version of the GLFW library, simply omit the --static flag.
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`

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

C++ program with gtk+

I want to call gtk+ routines from a C++ program test.cpp, and he encountered a problem while building my program.
I am getting the following error:
g++ test.cpp -o test.x
test.cpp:1:22: fatal error: gtk/gtk.h: No such file or directory
# include <gtk/gtk.h>
Somehow I need to link to gtk+. What will be the correct compiler declarations get my program working.
To compile a program using gtkmm, you need to use the following command:
g++ program.cc -o program `pkg-config --cflags --libs gtkmm-3.0`
The pkg-config --cflags --libs gtkmm-3.0 part provides the include paths and the linker options.
Source: The Flaming Manual, which you should Read.
I'm not sure about gtk in particular but this is a simple file not found issue.
You need to be sure that your system includes search path contains a folder gtk in it which in turn has a file called gtk.h.
Please read about the GCC search paths.
This is also likely a duplicated question so it will probably be closed off soon by the community.

libxml++/libxml++.h: No such file or directory

I am using ubuntu 12.4 g++ i'm working on libxml++ library I have included this library in my program but I am getting an error saying
libxml++/libxml++.h: No such file or directory
i also tried compiling using g++ main.cc -o main pkg-config --cflags --libs libxml++-2.6 but it is not working. I have installed the latest libraries using sudo apt-get install libxml++.
While compiling for libxml we have to use these options. pkg-config --cflags --libs libxml++-2.6 this should be in single quotation mark / Backticks ("`", grave accents), .
So the command should be like this.
$ g++ main.cc -o program `pkg-config --cflags --libs libxml++-2.6`
or go through this once. http://developer.gnome.org/libxml++/stable/

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.