Why the order of pkg-config make difference - c

Today I try to compile the hello world example from gstreamer tutorial
When I was compiling the code with command
gcc `pkg-config --cflags --libs gstreamer-0.10` basic-tutorial-1.c
I got the undefined reference error.
However, if I tried
gcc basic-tutorial-1.c `pkg-config --cflags --libs gstreamer-0.10`
It worked...
Can anyone give me some tips about the order of the pkg-config?
Thanks !

Related

Why does tcc not recognize "main" when I include the -pthreads flag?

I am trying to compile a c application with the gtk 3.0 library with tcc. The documentation says the command to run to compile is
gcc `pkg-config --cflags gtk+-3.0` -o [executable name] [source file] `pkg-config --libs gtk+-3.0`
I am trying to use tcc to compile, and it from what I can tell, the syntax should be the same. However, where gcc compiles it fine, when I use tcc, compilation fails with the error:
tcc: error: undefined symbol 'main'
I isolated the problem to the -pthread flag inserted by pkg-config --cflags gtk+-3.0 Thus, running a simple "Hello, World" c program and compiling with
tcc -pthread -o [executable name] [source file]
results in the same error. Am I compiling wrong, is it a compiler bug, or something else?
try -lpthread
tcc hello.c -lpthread -o hello

Linking SDL in a C program

I have recently become interested in using SDL after having learned some basics of C. I have installed SDL_image and SDL_mixer. They are located in /usr/local/include/SDL2. I realize that you must link against the header files however I am not sure how to do it. I am getting the error that SDL_mixer or SDL_image do not exist (depending on their line order in my source code). I have tried two different compilation commands and neither work here they are:
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
gcc filename.c -o test -I./usr/local/include/SDL2 -L./lib -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
If anyone has any ideas I would appreciate it! Thanks in advance!
you do not want that leading period
wrong
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
closer - not necessarily correct yet
gcc filename.c -o test -L/usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
any path with a leading period indicates to start from current dir and go relative instead of the intended absolute path
any system has the notion of a default library path which is fine if you are using a standard install ... so no need to do a
-I/include
... sometime a library has helpers to identify and auto populate these ...
sdl and sdl2 do have such a helper ... this will give you those settings
gcc -o test filename.c `pkg-config --cflags --libs sdl2`
notice those backticks ... another syntax style would be
gcc -o test filename.c $(pkg-config --cflags --libs sdl2)
you are free to issue that stand alone just to take a peek
pkg-config --cflags --libs sdl2
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
Now onto your sdl mixer ... well it has a
pkg-config --cflags --libs SDL2_mixer
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_mixer -lSDL2
you probably do not want to mix sdl with sdl2 so replace mention of
-lSDL_mixer -lSDL_image
with
-lSDL2_mixer -lSDL2_image
as per
pkg-config --cflags --libs SDL2_image
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_image -lSDL2
so bundling these together
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2) $(pkg-config --cflags --libs SDL2_mixer) $(pkg-config --cflags --libs SDL2_image)
or more simply combined to
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2 SDL2_mixer SDL2_image )
this can be stripped down to simply the following ... yet above syntax is more robust to changes
gcc -o test filename.c -D_REENTRANT -I/usr/include/SDL2 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_image
You can use sdl2-config to supply the appropriate flags to gcc:
gcc filename.c -o test `sdl2-config --cflags --libs`
sdl2-config --cflags produces a list of options that should be passed to the compiler, and sdl2-config --libs produces a list of libraries that should be linked to.

Signal in Glade GTK+3 do not work

I am trying to learn how to program a GTK application. I created my first application and here is the callback function:
void click_button2 (GtkToggleButton *tbutton, gpointer data)
{
gtk_main_quit ();
}
And here is how my GTK project looks: http://i.imgur.com/FR58rhT.png
After compiling with: gcc -Wall -g -o testGlade test.c pkg-config --cflags --libs gtk+-3.0 gmodule-2.0
The cal back does not seem to work. Nothing happens when I click the button.
Please add your source code of the test.c for further help.
Try compiling with -rdynamic option.
gcc -Wall -g -o testGlade test.c pkg-config --cflags --libs gtk+-3.0 gmodule-2.0 -rdynamic

what is the significance of pkg-config in gcc?

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 .

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

Resources