I've downloaded SDL2.0 like this:
~/tmp>wget http://www.libsdl.org/tmp/release/SDL2-2.0.0.tar.gz
Then I extracted it, went into the directory, and did ./configure && make && make install. Everything went fine. Now I've made a very simple sample (see far below). My problem is when I try to compile it doesn't work, So I copied SDL2.dll into the current directory and tried all combinations I know without success:
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2
All errors are either:
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: cannot find -lSDL2.dll
collect2: error: ld returned 1 exit status
Or:
933012#F9ZW00118371 ~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xe): undefined reference to `SDL_Init'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x42): undefined reference to `SDL_CreateWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x50): undefined reference to `SDL_GetWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x67): undefined reference to `SDL_RWFromFile'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x77): undefined reference to `SDL_LoadBMP_RW'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x9c): undefined reference to `SDL_UpperBlit'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xa7): undefined reference to `SDL_UpdateWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xb2): undefined reference to `SDL_FreeSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xda): undefined reference to `SDL_PollEvent'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xef): undefined reference to `SDL_DestroyWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xf4): undefined reference to `SDL_Quit'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o: bad reloc address 0x20 in section `.eh_frame'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
How would you do?
Here's my source code I try to compile:
#include <SDL.h>
int main(int argc, char *argv[]) {
int running;
SDL_Window *window;
SDL_Surface *windowsurface;
SDL_Surface *image;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
592, 460, SDL_WINDOW_SHOWN);
windowsurface = SDL_GetWindowSurface(window);
image = SDL_LoadBMP("exampleimage.bmp");
SDL_BlitSurface(image, NULL, windowsurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_FreeSurface(image);
running = 1;
while (running) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = 0;
break;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Not sure if this will work for cygwin, but on my Mac using pkg-config I was able to find out the compiler flags:
gcc example.c $(pkg-config --cflags --libs sdl2)
you should link with this flags: -Lwhere/sdldll/is -lSDL2, and -Iwhere/sdlincludes/are
-L flag is where the binaries(dll and lib) are, and -l is the filename of the dll(or lib)
-I flag is where the include files are located(.h)
gcc -o program.exe main.cpp -LSDL2/bin -lSDL2 -ISDL2/include
Related
I am novice in C programming. So I learned different process of compilation(preproccessing, compiling, linking). My program is
#include <stdio.h>
#define testDefinition(x) printf(#x " is equal to %lf\n",x)
int main(void)
{
testDefinition(3.15);
return 0;
}
It is simple program which doesn't have any sense,but problem is when I use gcc -o test test.c it works fine, but when I do that
gcc -E test.c -o test.i
gcc -C test.i -o test.o
gcc test.o -o test
I get error
usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
I am using Ubuntu 20.04 and GCC compiler.
test.o is already the executable, you did not pass -c.
$ gcc -E test.c -o test.i
$ gcc -C test.i -o test.o
$ ./test.o
3.15 is equal ....
Because of it, test.o is an ELF file and gcc treats it as shared library (I think). Because there are no source files passed in gcc test.o -o test there is no main, so it's undefined.
I guess, you wanted to do gcc -C -c test.i -o test.o to create an object file.
I have a github repo representing my exercise folder. Upon running make all the compiler throws error messages saying (Ubuntu):
cc -g -O2 -Wall -Wextra -Isrc -DNDEBUG -fPIC -c -o src/libex29.o src/libex29.c
src/libex29.c: In function ‘fail_on_purpose’:
src/libex29.c:36:33: warning: unused parameter ‘msg’ [-Wunused-parameter]
int fail_on_purpose(const char* msg)
^~~
ar rcs build/libex29.a src/libex29.o
ranlib build/libex29.a
cc -shared -o build/libex29.so src/libex29.o
cc -g -Wall -Wextra -Isrc test/ex29_test.c -o test/ex29_test
/tmp/cc7dbqDt.o: In function `main':
/home/givi/Desktop/lcthw_dir/29/test/ex29_test.c:21: undefined reference to `dlopen'
/home/givi/Desktop/lcthw_dir/29/test/ex29_test.c:22: undefined reference to `dlerror'
/home/givi/Desktop/lcthw_dir/29/test/ex29_test.c:25: undefined reference to `dlsym'
/home/givi/Desktop/lcthw_dir/29/test/ex29_test.c:26: undefined reference to `dlerror'
/home/givi/Desktop/lcthw_dir/29/test/ex29_test.c:33: undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'test/ex29_test' failed
make: *** [test/ex29_test] Error 1
i spent quite a lot trying to figure out how to fix undefined references. dlfcn.h is included, everything seems to be ok. Am i missing something? Please help
You must add following option when linking code using dlopen(3) :
-ldl
Here is a demo for Ubuntu 18:
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
$ cat dl.c
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
void *r;
r = dlopen("", RTLD_LAZY);
if (r != NULL)
printf("r is not null \n");
return 0;
}
$ gcc -o dl -Wall -pedantic -std=c11 dl.c -ldl
$ echo $?
0
Here is a very simple Makefile(note position of -ldl) and related make commands:
$ cat Makefile
CFLAGS=-g -O2 -Wall -Wextra -Isrc -DNDEBUG $(OPTFLAGS) $(OPTLIBS)
dl.o :dl.c
$(CC) -c $< $(CFLAGS)
dl :dl.o
$(CC) -o $# $^ $(CFLAGS) -ldl
clean :
rm -f dl dl.o
$ make clean
rm -f dl dl.o
$ make dl
cc -c dl.c -g -O2 -Wall -Wextra -Isrc -DNDEBUG
dl.c: In function ‘main’:
dl.c:5:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc, char **argv)
^~~~
dl.c:5:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
int main(int argc, char **argv)
^~~~
cc -o dl dl.o -g -O2 -Wall -Wextra -Isrc -DNDEBUG -ldl
$ ./dl
r is not null
Usually the references you're missing can be resolved by adding linker flag -ldl.
You didn't mention which operating system you're using.
In case you're on Windows you'll need this library: https://github.com/dlfcn-win32/dlfcn-win32
I'm trying to build a simple example using libcheck but it fails to build while using the flags reported by pkg-config.
My file: tests/test.c
#include <stdlib.h>
#include <check.h>
START_TEST(zero)
{
int z = 0;
ck_assert_int_eq(0, z);
}
END_TEST
Suite* suite_zero(void)
{
Suite* s;
TCase* tc;
s = suite_create("suite_zero");
tc = tcase_create("zero");
tcase_add_test(tc, zero);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
SRunner* sr;
Suite* s_zero = suite_zero();
sr = srunner_create(s_zero);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
My system:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
$ pkg-config --version
0.29.1
When I try to build my test using the flags and libs reported by pkg-config:
$ pkg-config --cflags --libs check
-pthread -lcheck_pic -pthread -lrt -lm -lsubunit
$ gcc -pthread -lcheck_pic -pthread -lrt -lm -lsubunit -g tests/test.c -o tests/zero
/tmp/ccRV2kLw.o: In function `zero':
Code/tests/test.c:4: undefined reference to `tcase_fn_start'
/Code/tests/test.c:7: undefined reference to `_mark_point'
/Code/tests/test.c:7: undefined reference to `_ck_assert_failed'
/tmp/ccRV2kLw.o: In function `suite_zero':
/Code/tests/test.c:15: undefined reference to `suite_create'
/Code/tests/test.c:16: undefined reference to `tcase_create'
/Code/tests/test.c:17: undefined reference to `_tcase_add_test'
/Code/tests/test.c:19: undefined reference to `suite_add_tcase'
/tmp/ccRV2kLw.o: In function `main':
/Code/tests/test.c:30: undefined reference to `srunner_create'
/Code/tests/test.c:32: undefined reference to `srunner_run_all'
/Code/tests/test.c:33: undefined reference to `srunner_ntests_failed'
/Code/tests/test.c:34: undefined reference to `srunner_free'
collect2: error: ld returned 1 exit status
Libcheck was installed by apt and the library and headers are in the typical locations, /usr/lib and /usr/include respectively. Yet it appears it can't be found. I'm a bit stumped as why. Any ideas?
Thanks.
Try adding the flags reported by pkg-config after the compilation command as this answer and the documentation for pkg-config suggests. The compiler needs to know what file it's trying to link the library to beforehand so that the flags can be applied appropriately.
gcc -g test.c -o test -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
or more succinctly
gcc -g test.c -o test `pkg-config --cflags --libs check`
I am trying to learn the gobject system, so I read some of the documentation on the gnome site and made a simple gobject parented to GObject. I don't wan't to keep running the gcc job every time I want to compile, and I also wanted to learn a build system at the same time. I tried autotools, which I got working to the point where pkg-config searches for gobject-2.0.
To compile my project from the command line, I do: gcc *.c $(pkg-config --cflags --libs gobject-2.0) (I had to take out the extra ticks for formatting). Anyway <--- this command works.
However: gcc $(pkg-config --cflags --libs gobject-2.0) *.c, which SHOULD BE the SAME COMMAND, returns this:
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
/tmp/ccxO7wkX.o: In function `main':
main.c:(.text+0x1a): undefined reference to `g_type_init'
main.c:(.text+0x27): undefined reference to `g_type_create_instance'
/tmp/ccPu2beU.o: In function `a_get_type':
myobject.c:(.text+0x57): undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
Yes, it is the EXACT SAME COMMAND WITH ANOTHER ORDER. I have no clue what's going wrong. Here is how autotools/automake does the build:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make: *** [GObjectTest] Error 1
Is this some quirky gcc compile thing? I tried with clang too no avail, which makes me think that this is a linker problem? I really have no idea, and was hoping that someone out there will. This is pretty frustrating.
Here is the "offending code":
GType a_get_type (void)
{
if (type == 0) {
GTypeInfo info = {
sizeof(AClass),
NULL,
NULL,
(GClassInitFunc) a_class_init,
NULL,
NULL,
sizeof(A),
0,
(GInstanceInitFunc) NULL
};
type = g_type_register_static (G_TYPE_OBJECT,
"AType",
&info, 0);
}
return type;
}
Edit: Here is the output of my autotools build, the make part anyway:
$ make
make all-recursive
make[1]: Entering directory `/home/aj/Desktop/GObjectTest'
Making all in src
make[2]: Entering directory `/home/aj/Desktop/GObjectTest/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT myobject.o -MD -MP -MF .deps/myobject.Tpo -c -o myobject.o myobject.c
mv -f .deps/myobject.Tpo .deps/myobject.Po
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make[2]: *** [GObjectTest] Error 1
make[2]: Leaving directory `/home/aj/Desktop/GObjectTest/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/aj/Desktop/GObjectTest'
make: *** [all] Error 2
Here is the interesting part: replace gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o with gcc -o GObjectTest main.o myobject.o -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 and it works.
Here's the makefile:
makefile.win
CC=gcc
CFLAGS=-Wall -ID:\dev\include -LD:\dev\lib -LD:\dev\bin
LIBS=-l mingw32 -l SDLmain -l SDL
TARGET=-mwindows
EXECUTABLE=main.exe
all:
$(CC) $(CFLAGS) $(LIBS) $(TARGET) main.c -o $(EXECUTABLE)
clean:
rm *o
(libSDL and libSDLmain are in D:\dev\lib. SDL.dll is in D:\dev\bin.)
and here's the code
#include <SDL/SDL.h>
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
printf("Could not initialize!");
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen) printf("Could not load video!");
int done = 0;
SDL_Event event;
while(!done)
{
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
done = 1;
}
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
printf("Exited cleanly");
return 0;
}
I build it with this command:
mingw32-make -f makefile.win
and mingw32-make translates the makefile into:
gcc -Wall -ID:\dev\include -LD:\dev\lib -LD:\dev\bin -l mingw32 -l SDLmain -l SD
L -mwindows main.c -o main.exe
which is alright.
But then I get all thes charming errors:
main.c:(.text+0x42): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x7c): undefined reference to `SDL_PollEvent'
main.c:(.text+0x8b): undefined reference to `SDL_Flip'
main.c:(.text+0x9c): undefined reference to `SDL_FreeSurface'
collect2: ld returned 1 exit status
mingw32-make: *** [all] Error 1
So, since I'm linking with mingw32, SDL and SDLmain. And I am adding the directory to the SDL headers. Why am I getting the errors?
You should place the library flags as last:
gcc -o main.exe main.c -lSDL -lSDLmain -lmingw32