Trouble linking to gobject-introspection library - c

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!

Related

libSDL2_gfx-1.0.so.0: cannot open shared object file: No such file or directory

Creating an SDL2 application on Ubuntu 20.04 which uses the visual shape overlays provided by SDL2_gfx. Have both SDL2 and SDL2_gfx installed, I believe properly (./configure, make, sudo make install... all successful), compilation is success, but program execution is not.
Error message:
libSDL2_gfx-1.0.so.0: cannot open shared object file: No such file or directory
**Useful commands/results/information: **
user#debian$ pkg-config --cflags sdl2
-D_reentrant -I/usr/local/include -I/usr/local/include/SDL2
user#debian$ pkg-config --cflags SDL2_gfx
-D_reentrant -I/usr/local/include/SDL2 -I/usr/local/include -I/usr/local/include/SDL2
user#debian$ pkg-config --libs sdl2
-L/usr/local/lib -Wl, -rpath,/usr/local/lib -Wl, --enable-new-dtags -lSDL2
user#debian$ pkg-config --libs SDL2_gfx
-L/usr/local/lib -lSDL2_gfx -Wl, -rpath,/usr/local/lib -Wl, --enable-new-dtags -lSDL2
Compile argument: gcc sdlTest.c -o sdlTesting -I/usr/local/include -I/usr/local/include/SDL2
-L/usr/local/lib -L/usr/local/lib -lSDL2 -lSDL2_gfx
Also the compile argument:
gcc sdlTest.c -o sdlTesting -lSDL2 -lSDL2_gfx
...somehow works as well for compilation. Build error remains the same:
Tried the standard compilation using linker commands -lSDL2 -lSDL2_gfx, expected to have a normal execution using ./sdlTesting

SDL 2 C Compiler Flags

Whenever I run the following, I get undefined references to all the SDL-related functions used in my program:
cc -lSDL2 -lGL *.o
I believe this is caused by the lack of -l linker flags.
GCC arguments are positional, put the link flags after your o files:
gcc *.o -lSDL2 -lGL
Also, if you're on a proper full Linux system I'd recommend using pkg-config to pull compiler/linker flags:
gcc -c main.c `pkg-config sdl2 --cflags`
gcc main.o `pkg-config sdl2 --libs`

gcc cannot find -lglfw3

I'm on a linux system (arch linux specifically) and I'm trying to compile the introduction project from the official glfw page but I cannot seem to get gcc to compile it. If anyone doesn't know what I'm talking about it's this.
Also this is how I'm trying to compile it:
gcc -Iinclude test.c -o test -lglfw3 -lm -lGL -lGLU
and it gives me the following errors:
/usr/bin/ld: cannot find -lglfw3
collect2: error: ld returned 1 exit status
I completely forgot about this question until I got a notification for it. For me the solution was to not use -lglfw3 but rather use -lglfw
If you've installed pkg-config, and glfw3.pc is in the search path, try:
gcc -Iinclude test.c -o test `pkg-config --libs glfw3` -lm -lGL -lGLU
If you only have the static build, use: pkg-config --static --libs glfw3, which will add the dependencies that libglfw3.a requires.
Find libglfw3.a or libglfw3.so on your system
Mention that path to gcc using -L
gcc -Iinclude test.c -o test -L/dir/where/glfw3/resides -lglfw3 -lm -lGL -lGLU

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.

using glib library in c program

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.

Resources