what is the significance of pkg-config in gcc? - c

While building a dbus-example, I found that we need to add a pkg-config in gcc. For example:
gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
What is the significance of pkg-config --cflags --libs dbus-1? what is pkg-config here? what is cflags extra here? what is --libshere?

gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
will run the pkg-config command, and pass its output as parameters to gcc.
The purpose of pkg-config is to make linking against libraries much easier, as different operating systems and distributions require different compilation flags (aka CFLAGS), library inclusion paths and libraries to link to. pkg-config uses configuration files (defined by the libraries) to generate the above information for compilers, and allows us to not worry about what operating system or distribution the compilation takes place on.
--cflags means the pkg-config should give the compilation flags for the listed packages.
--libs means the pkg-config should give the linking information for the listed packages.
and dbus-1 is the name of the package.

gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
comprises these parts:
executing the pkg-config --cflags --libs dbus-1 note `` run the command in between.
run gcc with the flags 1. returns and an input file <file_name> output object file .

Related

How do you use PGO + LTO optimization with GCC

I've been researching a lot lately on how PGO and LTO can significantly optimize a programs speed (some say around 20%). I currently just program in C and build GUI's with GTK+ in Windows (compiling everything through GCC) and only use -O2 for optimization.
I've been reading to compile with the flags
gcc -Wall -g -mwindows -O3 -fprofile-generate -flto example.c -o example.exe `pkg-config --cflags --libs gtk+-3.0`
a .gcno and a .gcda file is created, then you run the compiled .exe a bunch of times then just re-compile the same .c file again but swap the "-fprofile-generate" with "-fprofile-use"? (while making sure all these files stay in the same folder/directory, and use a different name for the .exe compiled the second time)
gcc -Wall -g -mwindows -O3 -fprofile-use -flto example.c -o program.exe `pkg-config --cflags --libs gtk+-3.0`
Or do you make use of the .gcno/.gcda file somehow? Also read things about using a "benchmark".
Question:
What are the steps to use it in GCC? (Step by step guide would help :)

C & Mac: trouble finding a header using pkg-config

I'm trying to use in one of my projects. I'm working on a Mac, have gotten MacPorts with pkg-config and glib-2.0 packages.
When I try to make a file containing an include to above path, I get the following error (line above it is for clarity that it does actually give me the right dirs):
$ pkg-config --cflags --libs glib-2.0
-I/opt/local/lib/glib-2.0/include -I/opt/local/include -L/opt/local/lib -lglib-2.0 -lintl
$ make
gcc hash-glib.c -c `pkg-config --cflags --libs glib-2.0` -std=c99 -Wall -Wextra -pedantic -O2
hash-glib.c:2:23: error: glib/glib.h: No such file or directory
$
Presumably, you've run:
ls -l /opt/local/lib/glib-2.0/include/glib/glib.h \
/opt/local/include/glib/glib.h
to demonstrate that the header #include "glib/glib.h" actually is present in one of the locations where you've been told by pkg-config that it could be found. If it isn't there, then pkg-config is misleading you, and the compiler is telling you that you've been hoodwinked.
Since the compiler will have done its utmost to find the header, it is a reasonable bet that the file isn't in either of those locations. You are then left with detective work: where is the glib.h header installed?
find /opt/local -type f -name glib.h
If that tells you where it is, you can then work out what pkg-config should be saying. If that fails to find it, widen the search area. If you still can't find it, maybe it isn't installed yet? Or you only installed the glib runtime, not the development package.

glib.h and gtk.h not found

Hi everyone I have a program with the following includes:
gtk/gtk.h
glib.h
I have used the commands:
sudo apt-get install libgtk2.0-dev glib
sudo apt-get install glade
But I am still getting the error that glib was not found and gtk/gtk.h was not found. It's the first time I am using gtk and I have no idea how it works or how to install it.
The command you're supposed to use (in more recent releases of linux/gtk) is pkg-config, not gtk-config. gtk-config is intended for pre 2.0 gtk development.
Consider the file you're compiling is called foo.c, to compile it under gtk-2.0, you would use, from the command line the command:
gcc `pkg-config --cflags glib-2.0 gtk+-2.0` foo.c -o foo `pkg-config --libs glib-2.0 gtk+-2.0`
This should compile, and give you a file foo, that can be executed.
but really, use a makefile, as this stuff is a pain to keep typing. I would write out a sample makefile, but there are rules that need to be followed in the formatting of them that makes it difficult to type in the editor window.
# Sample Makefile
CFLAGS := $(shell pkg-config --cflags glib-2.0 gtk+-2.0)
LDFLAGS := $(shell pkg-config --libs glib-2.0 gtk+-2.0)
foo: foo.c
<TAB HERE NOT SPACES>$(CC) $(CFLAGS) $< -o $# $(LDFLAGS)
This defines a simple rule saying to make foo, it depends on foo.c, so of foo.c is newer than foo, it will be rebuilt. Where I write 'TAB HERE NOT SPACES' it must be a tab character, and cannot be a set of space characters.
type "locate glib.h" to determine file's location (assuming a contemporary linux distribution - your post doesn't provide much information).
Then ensure the path to glib.h is properly specified in your Makefile. (You do have a Makefile, don't you?) Perform the same steps for gtk.h.
Please read the official documentation. It explains how to compile GTK applications.
Basically to compile a hello.c file to generate a hello program, you'll type:
gcc `pkg-config --cflags --libs gtk+-2.0` hello.c -o hello

how to compile a program with gtkmozembed.h

i have written a program under ubuntu, in which i include gtkmozembed.h. I am facing a problem in compiling the program.Below is the simplest form of a program which uses gtkmozembed.
#include <gtk/gtk.h>
#include <stdio.h>
#include <gtkmozembed.h>
int main(){
GtkWidget *mozEmbed;
mozEmbed = gtk_moz_embed_new();
return 0;
}
Eventhough, the above program is doing nothing, compiling that program is a lot for me...
I am trying to comile the above program like below
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
and it is giving the following error...
error: gtkmozembed.h: No such file or directory
I can understand, something else has to be added to the above gcc line,so that the compiler can find the gtkmozembed.h, but not getting what is that, 'something'...Looking for someone's help..Thank you...
Install libxul-dev (sudo apt-get install libxul-dev) and include
#include <gtkmozembed.h>
in the main file(test.c) and compile with
gcc `pkg-config --cflags --libs gtk+-2.0 xulrunner-gtkmozembed` test.c -o test
Your problem is that gtkmozembed.h is not found in the standard include file lookup path (well, the error does tell you that pretty obviously). On my system it lives in $(include)/gtkmozembed/, so you have two options
Change the path of the included file in your source
#include <gtkmozembed/gtkmozembed.h>
or manually add the path to the lookup path
gcc `pkg-config --libs --cflags gtk+-2.0` -I/usr/include/gtkmozembed test.c -o test
You should go with option 1).
This will tell gcc where to find the include file, but as pointed out by Matthew this is not enough: you will most probably also need to add more information for linking and required additional includes. Thankfully gtk-mozembed comes with a pkg-config file, so you can get all the needed information like you did for gtk+-2.0 with
pkg-config --libs --cflags mozilla-gtkmozembed-embedding
or combined with the other call
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test
You should also (just for kicks) have a look at what pkg-config does. The part in "`" is just what is return by the shell when executing that command. On my machine:
$ pkg-config --libs --cflags mozilla-gtkmozembed-embedding
-DXPCOM_GLUE -fshort-wchar \
-I/usr/include/xulrunner-1.9.2 -L/usr/lib/xulrunner-devel-1.9.2/lib -lxpcomglue
(line breaks added by me). The -I parts just adds additional needed directories to the include file lookup path -- they were emitted because you called with --cflags. The entries with -lxpcomglue is due to calling with --libs and ask for linking against this library, i.e. libxpcomglue.so. It is located in /usr/lib/xulrunner-devel-1.9.2/lib. The rest are a define and a gcc flag needed for gtkmozembed.
Try this:
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test

Why can't I build a "hello world" for glib?

So here's the world's simplest glib program:
#include <glib.h>
I try to compile it with gcc test.c and I get:
test.c:1:18: error: glib.h: No such file or directory
So I make sure that I have the right packages:
# dpkg -l | grep libglib
ii libglib-perl 1:1.183-1 Perl interface to the GLib and GObject libra
ii libglib1.2-dev 1.2.10-19build1 The GLib library of C routines (development)
ii libglib1.2ldbl 1.2.10-19build1 The GLib library of C routines
ii libglib2.0-0 2.20.1-0ubuntu2 The GLib library of C routines
ii libglib2.0-cil 2.12.1-1ubuntu2 CLI binding for the GLib utility library 2.1
ii libglib2.0-data 2.18.2-0ubuntu2 Common files for GLib library
ii libglib2.0-dev 2.20.1-0ubuntu2 Development files for the GLib library
ii libglibmm-2.4-1c2a 2.18.1-1 C++ wrapper for the GLib toolkit (shared lib
Then I search for any "glib.h" anywhere under /usr/include. I get two, /usr/include/glib-1.2/glib.h and /usr/include/glib-2.0/glib.h. So I try:
$ gcc -I/usr/include/glib-2.0 -Wall test.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
from /usr/include/glib-2.0/glib.h:32,
from test.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory
(about 10,000 more errors snipped)
I don't seem to have a glibconfig.h anywhere on my computer.
What do I do now?
glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.
Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.
The canonical way to do what you are trying is
% gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Note the back-ticks, which tell the shell to run the pkg-config command "in-place".
> > The canonical way to do what you are trying is
> % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:
gcc -Wall -o test `pkg-config --cflags glib-2.0` test.c `pkg-config --libs glib-2.0`
It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.
As #chris said use pkg-config.
glibconfig.h is missing
it’s because this file is not in the /usr/include/glib-2.0, but in /usr/lib/glib-2.0. So you have to include also this /usr/lib path or copy the file to the /include/glib-2.0
I am using glib.h as well-
Do this to compile all your glib.h programs :)
gcc `pkg-config --cflags --libs glib-2.0` filename.c
Make sure to surround pkg-config --cflags --libs glib-2.0 with back-quotes
which you find under tilde (left most key on the querty keyboard).
Thank me later .. :P
apt-get build-dep is your friend -- nobody can remember all the packages you need to build something.

Resources