I'm trying to compile a C program, with these headers: http://pastebin.com/SppCXb0U , on Ubuntu. At first I didn't have any luck at all, but after reading about pkg-config I crafted this line:
gcc `pkg-config --cflags --libs dbus-1` `pkg-config --cflags --libs glib-2.0` signals-tutorial.c
However, it still doesn't work and gives me this error:
/tmp/cc3BkbdA.o: In function `filter_example':
signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main'
/tmp/cc3BkbdA.o: In function `proxy_example':
signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init'
signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get'
signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name'
signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal'
signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal'
collect2: ld returned 1 exit status
I'm not sure what to do from here.
==================================
A nice explanation - thank you. However, I can't get it working. Running your command above (with an added ) yield the following result
gcc `pkg-config --cflags dbus-1` \
> `pkg-config --cflags glib-2.0` \
> signals-tutorial.c \
> `pkg-config --libs dbus-1` \
> `pkg-config --libs glib-2.0`
/tmp/ccjN0QMh.o: In function `filter_example':
signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main'
/tmp/ccjN0QMh.o: In function `proxy_example':
signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init'
signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get'
signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name'
signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal'
signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal'
collect2: ld returned 1 exit status
Your problem isn't with the header files, your problem is with the libraries; complaints about "undefined references" generally come from the linker. You need to put the library configuration options after the source file:
gcc `pkg-config --cflags dbus-glib-1` \
`pkg-config --cflags dbus-1` \
`pkg-config --cflags glib-2.0` \
signals-tutorial.c \
`pkg-config --libs dbus-glib-1` \
`pkg-config --libs dbus-1` \
`pkg-config --libs glib-2.0`
The --libs option will produce a series of -l flags for the compiler, the compiler will pass those to the linker. The linker will resolve symbols from left to right starting with the object file (or, close enough in this case, the C source file) so all the library -l switches need to follow your source file.
Related
everyone.
I'm working on ubuntu, developing this application using the glib resources, I wrote a makefile like this:
INCLUDES= -I ./headers
FLAGS= -g
PKGCONFIGCFLAGS= `pkg-config --cflags qmi-glib` `pkg-config --cflags gio-2.0` `pkg-config --cflags gobject-2.0` `pkg-config --cflags glib-2.0` `pkg-config --cflags gmodule-no-export-2.0` `pkg-config --cflags gmodule-2.0`
PKGCONFIGLIBS= `pkg-config --libs qmi-glib` `pkg-config --libs gio-2.0` `pkg-config --libs glib-2.0` `pkg-config --libs gobject-2.0` `pkg-config --libs gmodule-no-export-2.0` `pkg-config --libs gmodule-2.0`
CC=gcc
OBJECTS=./build/qmi_test.o
all: ${OBJECTS}
$(CC) ${FLAGS} ${INCLUDES} ./sources/main.c ${OBJECTS} -o ./bin/main
./build/qmi_test.o:./sources/qmi_test.c
$(CC) ${FLAGS} ${INCLUDES} ${PKGCONFIGCFLAGS} ./sources/qmi_test.c ${PKGCONFIGLIBS} -lm -o ./build/qmi_test.o
As you may notice, I inserted every glib related resource I found because I keep receiving this in return:
/usr/bin/ld: ./build/qmi_test.o: in function `g_autoptr_cleanup_generic_gfree':
/usr/include/glib-2.0/glib/glib-autocleanups.h:28: undefined reference to `g_free'
/usr/bin/ld: ./build/qmi_test.o: in function `qmi_test1':
/home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:24: undefined reference to `g_file_new_for_path'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:25: undefined reference to `g_main_loop_new'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:26: undefined reference to `g_cancellable_new'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:29: undefined reference to `g_main_loop_run'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:31: undefined reference to `g_object_unref'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:32: undefined reference to `g_main_loop_unref'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:33: undefined reference to `g_object_unref'
/usr/bin/ld: ./build/qmi_test.o: in function `Device_Create_Start':
/home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:42: undefined reference to `g_file_get_path'
/usr/bin/ld: /home/myUser/Documents/quectel_eg25-g/./sources/qmi_test.c:45: undefined reference to `qmi_device_new'
collect2: error: ld returned 1 exit status
make: *** [Makefile:13: all] Error 1
I read in that the order of ellements when calling gcc does matter. But I'm trying to follow the order of elements as shown here:
glib compiling
Any help or recommendation is welcome.
You need to add the library flags (PKGCONFIGLIBS) to the link step and not the compile step:
all: ${OBJECTS}
$(CC) ${FLAGS} ${INCLUDES} -lm ./sources/main.c ${OBJECTS} ${PKGCONFIGLIBS} -lm -o ./bin/main
./build/qmi_test.o:./sources/qmi_test.c
$(CC) ${FLAGS} ${INCLUDES} ${PKGCONFIGCFLAGS} ./sources/qmi_test.c -o ./build/qmi_test.o
I'm following an X11 programming tutorial from here. The page gives 2 commands that can be used to compile an XCB-based program:
gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`
and
gcc -Wall prog.c -lxcb
Now, I've tried both. The first one says gcc: error: unrecognized command-line option ‘--cflags’. Apparently this is a shell related problem (As I've seen here). So I tried bash. And this gives a different error:
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlib':
example.c:(.text+0xd6): undefined reference to `XInternAtom'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlibProperly':
example.c:(.text+0x163): undefined reference to `XInternAtoms'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `main':
example.c:(.text+0x4b1): undefined reference to `XOpenDisplay'
/usr/bin/ld: example.c:(.text+0x559): undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status
This is the same as what I get with gcc -Wall prog.c -lxcb. So I guess bash fixed the problem but there are 2. And in Atom, when you hover over a function, it shows you which header it is from. But in this one I didnt't get anything.
Thanks in advance
Looks like over the years the libraries got splitted and the function declarations were moved to a different library. And now xcb depends on X11 or something like that or maybe pkg-config --libs xcb should output -lX11, no idea. Anyway, the following works:
gcc -Wall 1.c -lxcb -lX11
or the following works too:
gcc -Wall 1.c $(pkg-config --cflags --libs xcb) $(pkg-config --cflags --libs x11)
Would be nice to ping the maintainer of that introduction to let him know he should update the page.
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 !
I want to use hash tables in my c program.
I code:
...
#include <glib.h>
void main(int argc, char **argv)
{
GHashTable *g_hash_table;
...
g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
...
}
Then I compile:
$ gcc -I/usr/include/glib-2.0
-I/usr/lib/i386-linux-gnu/glib-2.0/include
-lglib-2.0 -o test test.c
or the same command:
$ gcc `pkg-config --cflags --libs glib-2.0` -o test test.c
but anyway the result is:
test.c: underfined reference to `g_int_equal`
test.c: underfined reference to `g_int_hash`
test.c: underfined reference to `g_hash_table_new`
collect2: ld returned 1 exit status
Why I cant compile my program? I do wrong include of glib library?
You need to specify libraries in the command line after the source and object files that use them:
gcc test.c `pkg-config --cflags --libs glib-2.0` -o test
From this pdf at IBM developper works, it's better to use the pkg-config if you have a standard install of glib with this command :
$ gcc `pkg-config --cflags --libs glib-2.0` -o ex-compile ex-compile.c
Your include look right and the way you are using it to. Not sure the ' will change anything but you might want to check the PDF, it contains a lot of examples and explainations.
I have the file blah.c containing:
#include <gobject-introspection-1.0/girepository.h>
int main(int argc,char **argv) {
GIRepository *gir = g_irepository_get_default();
return 0;
}
This, of course, is simpler than the file I wish to compile, but has the same problem. I compile with
gcc `pkg-config --cflags --libs gobject-introspection-1.0` blah.c
and get the linking error:
/tmp/cck88oj4.o: In function `main':
blah.c:(.text+0x10): undefined reference to `g_irepository_get_default'
collect2: ld returned 1 exit status
The pkg-config command returns
-pthread -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/gobject-introspection-1.0 -Wl,--export-dynamic -pthread -lgirepository-1.0 -lgobject-2.0 -lgmodule-2.0 -lffi -lgthread-2.0 -lrt -lglib-2.0
and the files libgirepository-1.0.a and libgirepository-1.0.so are both present in /usr/lib.
What is causing this linking error?
The reason for the linking error could be because the linker is linking with -Wl, as-needed option by default. When this option is used, the libraries are not linked until symbol in the library is encountered. In your current case as the source file is added in the end, none of the symbols in the libraries are encountered, thus linking is not done. You could try:
gcc blah.c `pkg-config --cflags --libs gobject-introspection-1.0`
or
gcc -Wl,-no-as-needed `pkg-config --cflags --libs gobject-introspection-1.0` blah.c
Hope this helps!