Linking SDL in a C program - c

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.

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

gcc glib symbol lookup error

I am currently programming a little C-programm that uses GLib-2.0 for its data structures.
I am compiling with the following command:
gcc -g -O3 -std=gnu99 -fPIC -shared -I/usr/local/java/jdk1.8.0_20/include \
-I/usr/local/java/jdk1.8.0_20/include/linux \
`pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` \
-o runtimesamplingagent.so runtimesamplingagent.c
This works without problems.
However, when I run the program, I get following error:
symbol lookup error: ./runtimesamplingagent.so: undefined symbol: g_node_new
What's the cause of this?
I am pretty new to C but I would guess it's some kind of linking issue.
Any hints what could be the cause of it?
I am working on a Ubuntu 14.04 LTS 64bit machine with libglib-2.0-dev installed.
You have already included the linking option in the build command; the problem is that it is in the wrong place.
Your command looks like this:
gcc -g -O3 -std=gnu99 -fPIC -shared \
-I/usr/local/java/jdk1.8.0_20/include \
-I/usr/local/java/jdk1.8.0_20/include/linux \
`pkg-config --cflags glib-2.0` \
`pkg-config --libs glib-2.0` \ <<<
-o runtimesamplingagent.so \
runtimesamplingagent.c
In that line marked with <<<, you have already specified the options needed to link GLib. The only problem is that it is in the wrong place. You should put this at the very end, after the .c file.
So your command should look like this instead:
gcc -g -O3 -std=gnu99 -fPIC -shared \
-I/usr/local/java/jdk1.8.0_20/include \
-I/usr/local/java/jdk1.8.0_20/include/linux \
`pkg-config --cflags glib-2.0` \
-o runtimesamplingagent.so \
runtimesamplingagent.c \
`pkg-config --libs glib-2.0`
appending
-lglib-2.0
to the end of the build command, fixed it.

How to use pkg-config in Make

I want to compile the simplest GTK program.
I can compile it using the command line:
gcc $(pkg-config --cflags --libs gtk+-3.0) main.c -o main.o
However, if I use Make it doesnt work:
CFLAGS=-g -Wall -Wextra $(pkg-config --cflags)
LDFLAGS=$(pkg-config --libs gtk+-3.0)
CC=gcc
SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))
all: $(EXECUTABLES)
It tells me this:
gcc -g -Wall -Wextra -c -o main.o main.c
main.c:1:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1
Where do I stick $(pkg-config --cflags --libs gtk+-3.0) in the Makefile to make it compile?
Thanks very much in advance for your kind help.
There are two issues.
First, your CFLAGS line is wrong: you forgot to say gtk+-3.0 in the pkg-config part, so pkg-config will spit out an error instead:
CFLAGS=-g -Wall -Wextra $(pkg-config --cflags gtk+-3.0)
Second, and more important, $(...) is intercepted by make itself for variable substitution. In fact, you've seen this already:
SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))
all: $(EXECUTABLES)
is all done by make.
There are two things you can do.
First, you can use `...` instead, which does the same thing ($(...) is newer shell syntax).
CFLAGS=-g -Wall -Wextra `pkg-config --cflags gtk+-3.0`
LDFLAGS=`pkg-config --libs gtk+-3.0`
Second, since you seem to be using GNU make, you can use the shell substitution command, which was shown in the answer Basile Starynkevitch linked above:
CFLAGS=-g -Wall -Wextra $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS=$(shell pkg-config --libs gtk+-3.0)

Resources