I am trying to use a simple SDL2 example code that compiles for AmigaOS 4 based on latest 2.0.12 SDK, on a cross compiler.
I need that to test my cross compiler installation is fine, so to be sure that the setup is useful for everyone to use. This should be able to be compiled with gcc, v8.0.4 in my situation.
I have the following code:
#include <SDL2/SDL.h> #include <stdio.h> #include <stdlib.h> int main() { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError()); return EXIT_FAILURE; } SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 620, 387, SDL_WINDOW_SHOWN); if (win == NULL) { fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError()); return EXIT_FAILURE; } SDL_Renderer *ren = SDL_CreateRenderer(win, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == NULL) { fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError()); if (win != NULL) { SDL_DestroyWindow(win); } SDL_Quit(); return EXIT_FAILURE; } SDL_Surface *bmp = SDL_LoadBMP("../img/grumpy-cat.bmp"); if (bmp == NULL) { fprintf(stderr, "SDL_LoadBMP Error: %s\n", SDL_GetError()); if (ren != NULL) { SDL_DestroyRenderer(ren); } if (win != NULL) { SDL_DestroyWindow(win); } SDL_Quit(); return EXIT_FAILURE; } SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp); if (tex == NULL) { fprintf(stderr, "SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError()); if (bmp != NULL) { SDL_FreeSurface(bmp); } if (ren != NULL) { SDL_DestroyRenderer(ren); } if (win != NULL) { SDL_DestroyWindow(win); } SDL_Quit(); return EXIT_FAILURE; } SDL_FreeSurface(bmp); for (int i=0; i < 20; i++) { SDL_RenderClear(ren); SDL_RenderCopy(ren, tex, NULL, NULL); SDL_RenderPresent(ren); SDL_Delay(100); } SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return EXIT_SUCCESS; }
Trying to compile it with
ppc-amigaos-gcc -I/opt/sdk/ppc-amigaos/Include/include_h -I/opt/sdk/SDL2/include -use-dynld sdl_example.c -o sdl_example
Under the /opt/sdk/SDL2/include folder I have the SDL2 folder with all the includes
I get the following errors:
/tmp/ccgPBkQC.o: In function `main':
sdl_example.c:(.text+0x20): undefined reference to `SDL_Init'
sdl_example.c:(.text+0x3c): undefined reference to `SDL_GetError'
sdl_example.c:(.text+0x80): undefined reference to `SDL_CreateWindow'
sdl_example.c:(.text+0xa0): undefined reference to `SDL_GetError'
sdl_example.c:(.text+0xd4): undefined reference to `SDL_CreateRenderer'
sdl_example.c:(.text+0xf4): undefined reference to `SDL_GetError'
sdl_example.c:(.text+0x124): undefined reference to `SDL_DestroyWindow'
sdl_example.c:(.text+0x128): undefined reference to `SDL_Quit'
sdl_example.c:(.text+0x144): undefined reference to `SDL_RWFromFile'
sdl_example.c:(.text+0x154): undefined reference to `SDL_LoadBMP_RW'
sdl_example.c:(.text+0x174): undefined reference to `SDL_GetError'
sdl_example.c:(.text+0x1a4): undefined reference to `SDL_DestroyRenderer'
sdl_example.c:(.text+0x1b8): undefined reference to `SDL_DestroyWindow'
sdl_example.c:(.text+0x1bc): undefined reference to `SDL_Quit'
sdl_example.c:(.text+0x1d0): undefined reference to `SDL_CreateTextureFromSurface'
sdl_example.c:(.text+0x1f0): undefined reference to `SDL_GetError'
sdl_example.c:(.text+0x220): undefined reference to `SDL_FreeSurface'
sdl_example.c:(.text+0x234): undefined reference to `SDL_DestroyRenderer'
sdl_example.c:(.text+0x248): undefined reference to `SDL_DestroyWindow'
sdl_example.c:(.text+0x24c): undefined reference to `SDL_Quit'
sdl_example.c:(.text+0x25c): undefined reference to `SDL_FreeSurface'
sdl_example.c:(.text+0x270): undefined reference to `SDL_RenderClear'
sdl_example.c:(.text+0x284): undefined reference to `SDL_RenderCopy'
sdl_example.c:(.text+0x28c): undefined reference to `SDL_RenderPresent'
sdl_example.c:(.text+0x294): undefined reference to `SDL_Delay'
sdl_example.c:(.text+0x2b4): undefined reference to `SDL_DestroyTexture'
sdl_example.c:(.text+0x2bc): undefined reference to `SDL_DestroyRenderer'
sdl_example.c:(.text+0x2c4): undefined reference to `SDL_DestroyWindow'
sdl_example.c:(.text+0x2c8): undefined reference to `SDL_Quit'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_key_create'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_mutex_lock'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_active_p'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_setspecific'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_once'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_mutex_unlock'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_getspecific'
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/libgcc.so: undefined reference to `__gthread_mutex_init'
collect2: error: ld returned 1 exit status
If I compile it with, where I try to link with SDL2
ppc-amigaos-gcc -I/opt/sdk/ppc-amigaos/Include/include_h -I/opt/sdk/SDL2/include -use-dynld sdl_example.c -o sdl_example
I get the following error
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/bin/ld: cannot find -lSDL2
collect2: error: ld returned 1 exit status
I wonder were actually it is looking for -lSDL2.
Your help is much appreciated
For this found that I needed to move libSDL2.a at the newlib/lib folder. But then, when I try to compile I get the first errors mentioned before again.
Which errors are those? If it's the undefined references to __gthread_xxxx then you need to add -athread=single (or -athread=native if the code uses C++ threading) to the commandline used for linking.
Dunnot if it helps, but under AmigaOS4 I use to compile your test/example:
gcc test.c -o test -Wall -gstabs -use-dynld -lSDL2 -lpthread -athread=native
and compiled fine:
#test
SDL_LoadBMP Error: Couldn't open ../img/grumpy-cat.bmp
#list
test 186086 ----rwed Hoy 11:23:57
test.c 1831 ----rwed Hoy 11:23:54
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
You might wanna add "-lunix" as it uses Unix path names as well.
Is "-use-dynld" known to work on cross compilers?
Does it include a configure or CMake file you can build a makefile from?
I recall you once had to specify each link lib file fully in the right order. But things may be easier now. Don't know if cross compiling can find all the files.
First of all I would like to thank you all guys for your help
I tried to compile it with the following command
ppc-amigaos-gcc -I/opt/sdk/ppc-amigaos/Include/include_h -I/opt/sdk/SDL2/include -L/opt/sdk/SDL2/lib -Wall -gstabs -use-dynld -lSDL2 -athread=native sdl_example.c -o sdl_example -v
so to get some more info, if possible, and the full output is:
@jabirulo
unfortunately
-lpthread
didn't work, because there is such file under newlib/lib folder@walkero
The latest GCC needs pthread library in any case if you use -athread=native. That library comes with SDK. It just in a separate archive in SDK: pthread-53.11.lha , so you need to install that one.
In your last error output, you have all undef references to the SDL functions, which mean that libSDL2.a didn't take on the linking stage. Can't say right from the output what the problem, but I have few advises:
1). change the order of compiling flags and linking libs (linking libs at the end, after all, other ones).
2). don't use shared objects (at least for first tests) and compile everything statically. Once you are done with static builds and all works, you then can think if you need to switch to the usage of sobjest or not.
If it were me, the compiling line should look like this:
ppc-amigaos-gcc -gstabs -athread=native sdl_example.c -o sdl_example -lSDL2 -lpthread
(taking in account that includes all in right place)
When linking static libraries gcc will only link in those parts of the library that are needed to satisfy undefined references. Because you link SDL2 before sdl_example.c, which is the only part of your program that uses SDL functions, it means that nothing is linked from SDL2 and it is the same as if you hadn't used -lSDL2 in the first place.
As a general rule I recommend to always specify linker libraries at the end of the command-line, as kas1e does in his post above.
You guys are awesome. It worked, and I fixed the pthreads as kas1e proposed and this works as well.
Thank you salass00 for the good explanation as well, that helps me understand how things work. That doesn't that I won't come back with more questions in the future :D
Now this small amount of code compiles just fine, and I am quite sure that my cross development environment seems quite ready for usage from anyone likes. You can have a look if you want at
https://github.com/walkero-gr/odysseyOnDocker