I am programming in C using SDL2 and I am getting this error:
`main': main.c:(.text+0x2d0): undefined reference to 'IMG_Load'`
I have these includes:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
For reference I am using Arch Linux and using GCC with this command to compile:
gcc main.c `sdl2-config --libs -lSDL2 -lSDL2_image` -o game
I've looked all over for solutions but none of them seem to rectify the issue.
This:
gcc main.c `sdl2-config --libs -lSDL2 -lSDL2_image` -o game
should be:
gcc main.c `sdl2-config --libs` -lSDL2 -lSDL2_image -o game
In other words, only sdl2-config --libs is the subcommand.
Related
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`
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.
I'm pretty new to C programming, and am trying to work through the exercises in '21st Century C' second edition. I'm stuck on page 202, Example 9-7, unicode.c. This example starts with:
#include <glib.h>
#include <locale.h> //setlocale
#include "string_utilities.h"
#include "stopif.h"
//Frees instring for you--we can't use it for anything else.
char *localstring_to_utf8(char *instring){
GError *e=NULL;
setlocale(LC_ALL, ""); //get the OS's locale.
char *out = g_locale_to_utf8(instring, -1, NULL, NULL, &e);
free(instring); //done with the original
Stopif(!out, return NULL, "Trouble converting from your locale to UTF-8.");
Stopif(!g_utf8_validate(out, -1, NULL), free(out); return NULL,
"Trouble: I couldn't convert your file to a valid UTF-8 string.");
return out;
}
When I try to compile it with:
c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
I get errors such as:
$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
/tmp/ccBDQFiH.o: In function `localstring_to_utf8':
/home/kevin/21st_Century_C/ch09/unicode.c:29: undefined reference to `g_locale_to_utf8'
/home/kevin/21st_Century_C/ch09/unicode.c:32: undefined reference to `g_utf8_validate'
/tmp/ccBDQFiH.o: In function `main':
/home/kevin/21st_Century_C/ch09/unicode.c:48: undefined reference to `g_utf8_strlen'
This seems to indicate that the Glib library is not found, but the compiler didn't complain about this, and the Glib libraries and include files are right where I specified on the command line. I've installed the libglib2.0-dev package in addition to the libglib2.0 package (all installed with 'sudo apt-get ..'). 'pkg-config' seems to find glib-2.0 just fine.
This is all on a Ubuntu 14.04.2 system.
I can't figure out how to correct this error, and don't understand why it can't find the specific Glib functions, if it finds the glib include and lib files.
The order of things in the command line matter. In general it should be something like:
gcc [options] [source files] [object files] [-L stuff] [-lstuff] [-o outputfile]
So give this a whirl instead:
gcc -g -Wall -O3 -std=gnu11 `pkg-config --cflags glib-2.0` \
unicode.c string_utilities.o `pkg-config --libs glib-2.0` \
-o unicode
This is also covered in the Compiling GLib Applications section of the GLib Reference Manual:
$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hello
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!