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`
Related
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
Im using windows 10 with wsl ubuntu 18.04 Im trying to run the code from here :
https://www.ccs.neu.edu/home/skotthe/classes/cs5600/fall/2015/labs/intro-check-lab-code.tgz
I installed gcc, makefile, and check in the ubuntu terminal. But when I di $ make it says:
gcc money.o check_money.o -lcheck -lm -lpthread -lrt -lgcov -coverage -o check_money_tests
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libcheck.a(check_log.o): In function `subunit_lfun':
(.text+0x5f4): undefined reference to `subunit_test_start'
(.text+0x6bf): undefined reference to `subunit_test_fail'
(.text+0x6d4): undefined reference to `subunit_test_pass'
(.text+0x6ef): undefined reference to `subunit_test_error'
collect2: error: ld returned 1 exit status
Makefile:23: recipe for target 'check_money_tests' failed
make: *** [check_money_tests] Error 1
so I open the check_money.c it says that check.h cannot be found. What did I miss here?
Simple version: you should also link with -lsubunit flag, i.e.:
TST_LIBS = -lcheck -lm -lpthread -lrt -lsubunit
Better yet, all the flags should be taken from check's package configuration, as the requirements depend on how the check library was built (and that's what pkg-config has been invented for). So instead of hardcoding your options, you could improve your Makefile like this:
CFLAGS = -c -Wall $(shell pkg-config --cflags check)
TST_LIBS = $(shell pkg-config --libs check)
Result:
$ make
gcc -c -Wall -pthread -fprofile-arcs -ftest-coverage src/*.c
gcc -c -Wall -pthread -fprofile-arcs -ftest-coverage tests/*.c
gcc money.o check_money.o -lcheck_pic -pthread -lrt -lm -lsubunit -lgcov -coverage -o check_money_tests
...
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
I have built OpenSSL from source (an intentionally old version; built with ./config && make && make test) and would prefer to use what I have built without doing make install to link against my program.
The command that's failing is:
gcc -Wall -Wextra -Werror -static -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto
-Iopenssl/openssl-0.9.8k/include -o myApp source1.o source2.o common.o`
And I receive a series of errors similar to:
common.c:(.text+0x1ea): undefined reference to `SSL_write'
This makes me think there's something funky with my OpenSSL. If I omit -Lopenssl/openssl-0.9.8k/ from my command, the error changes to being unable to:
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
Am I compiling OpenSSL incorrectly? Or how should I best resolve this?
Silly "Linux-isms" strike again! Apparently, I need to change my command such that the -L and -l stuff is at the end like (despite what man gcc seems to indicate):
gcc -Wall -Wextra -Werror -static -o myApp source1.o source2.o common.o -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto -Iopenssl/openssl-0.9.8k/include
Why don't you want to use make install? It can copy generated binaries in the directory you want if you previously passed it to ./configure --prefix $HOME/target_library_install_directory
If you used this trick with every library you build and install, you could then add the target directory to the LIBRARY_PATH environment variable and avoid using -L option.
If you use Autotools, or you are building an Autools project like cURL, then you should be able to use pkg-config. The idea is the Autotools package will read OpenSSL's package configuration and things will "just work" for you.
The OpenSSL package configuration library name is openssl.
You would use it like so in a makefile based project.
%.o: %.c
$(CC) -o $# -c `pkg-config --cflags openssl` $^
target: foo.o bar.o baz.o
$(CC) -o $# `pkg-config --libs openssl` $^
Also see How to use pkg-config in Make and How to use pkg-config to link a library statically.
Another approach is to use pkg-config to preserve compatibility. This is an example of makefile when needs to link OpenSSL.
CC = gcc
CFLAGS = \
-I. \
-D_GNU_SOURCE=1
LDFLAGS = `pkg-config --libs inih`
LDFLAGS += `pkg-config --libs libcurl`
LDFLAGS += `pkg-config --libs liburiparser`
LDFLAGS += `pkg-config --libs openssl`
# Executable
foo: foo.o
$(CC) -o $# $^ $(LDFLAGS)
foo.o: foo.c
$(CC) -c $(CFLAGS) $< -o $#
I am working on a C project for Linux-environment (compiled with gcc). I am using two libraries:
SDL image (source is stored at the directory SDL_img).
SDL TTF (source is stored at the directory SDL_ttf).
My CFLAGS variable:
CFLAGS = -std=c99 -pedantic-errors -Wall -g -lm `sdl-config --cflags` -ISDL_img -ISDL_ttf
As you can see, I am including those two library-directories.
My gcc command include the following:
`sdl-config --libs` -lSDLmain -lSDL -lSDL_img -lSDL_ttf
Finally, in my project I have the following includes:
#include "SDL_ttf.h"
#include "SDL_image.h"
For some reason, I get errors of the type:
undefined reference to 'IMG_Load'
Why?
EDIT:
all: Chess.o Commons.o Console.o Controls.o Coords.o File.o GameState.o GUI.o Keyboard.o List.o Minimax.o Move.o Piece.o SettingsState.o Slots.o Square.o Str.o
gcc $^ -lm -std=c99 -pedantic-errors -g -o Chess `sdl-config --libs` -lSDLmain -lSDL -lSDL_img -lSDL_ttf
Just cd to those src/SDL_xxx dirs and do make to build those libraries and then set the correct -L and -I. It failed on linking so the -I is "good" but your path to your libraries is incorect
basically linker is now searching its paths to find libSDL_img and libSDL_ttf then it also searches /usr/lib/x86_64-linux-gnu and it still can not find it, that is why you are getting undefined referecne.
To fix this: first build those SDL_xxx and then search where are those libraries and pass somethig like this along with other to gcc:
-Lsrc/SDL_img/lib -lSDL_img -Lsrc/SDL_ttf/lib -lSDL_ttf