Looking for a simple example

9 posts / 0 new
Last post
walkero
walkero's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2009-05-03 16:54
Looking for a simple example

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:

  1. #include <SDL2/SDL.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
  8. fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
  9. return EXIT_FAILURE;
  10. }
  11.  
  12. SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 620, 387, SDL_WINDOW_SHOWN);
  13. if (win == NULL) {
  14. fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
  15. return EXIT_FAILURE;
  16. }
  17.  
  18. SDL_Renderer *ren = SDL_CreateRenderer(win, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  19. if (ren == NULL) {
  20. fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
  21. if (win != NULL) {
  22. SDL_DestroyWindow(win);
  23. }
  24. SDL_Quit();
  25. return EXIT_FAILURE;
  26. }
  27.  
  28. SDL_Surface *bmp = SDL_LoadBMP("../img/grumpy-cat.bmp");
  29. if (bmp == NULL) {
  30. fprintf(stderr, "SDL_LoadBMP Error: %s\n", SDL_GetError());
  31. if (ren != NULL) {
  32. SDL_DestroyRenderer(ren);
  33. }
  34. if (win != NULL) {
  35. SDL_DestroyWindow(win);
  36. }
  37. SDL_Quit();
  38. return EXIT_FAILURE;
  39. }
  40.  
  41. SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
  42. if (tex == NULL) {
  43. fprintf(stderr, "SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError());
  44. if (bmp != NULL) {
  45. SDL_FreeSurface(bmp);
  46. }
  47. if (ren != NULL) {
  48. SDL_DestroyRenderer(ren);
  49. }
  50. if (win != NULL) {
  51. SDL_DestroyWindow(win);
  52. }
  53. SDL_Quit();
  54. return EXIT_FAILURE;
  55. }
  56. SDL_FreeSurface(bmp);
  57.  
  58. for (int i=0; i < 20; i++) {
  59. SDL_RenderClear(ren);
  60. SDL_RenderCopy(ren, tex, NULL, NULL);
  61. SDL_RenderPresent(ren);
  62. SDL_Delay(100);
  63. }
  64.  
  65. SDL_DestroyTexture(tex);
  66. SDL_DestroyRenderer(ren);
  67. SDL_DestroyWindow(win);
  68. SDL_Quit();
  69.  
  70. return EXIT_SUCCESS;
  71. }

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

walkero
walkero's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2009-05-03 16:54
Re: Looking for a simple example

I wonder were actually it is looking for -lSDL2.

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.

salass00
salass00's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-02-03 11:27
Re: Looking for a simple example

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.

jabirulo
jabirulo's picture
Offline
Last seen: 2 months 2 weeks ago
Joined: 2013-05-30 00:53
Re: Looking for a simple example

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
hypex
hypex's picture
Offline
Last seen: 14 hours 54 sec ago
Joined: 2011-09-09 16:20
Re: Looking for a simple example

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.

walkero
walkero's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2009-05-03 16:54
Re: Looking for a simple example

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:

  1. Using built-in specs.
  2. COLLECT_GCC=ppc-amigaos-gcc
  3. COLLECT_LTO_WRAPPER=/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/lto-wrapper
  4. Target: ppc-amigaos
  5. Configured with: /work/gcc/repo/configure --with-bugurl=https://github.com/sba1/adtools/issues --with-pkgversion='adtools build 8.4.0' --target=ppc-amigaos --prefix=/opt/ppc-amigaos --enable-languages=c,c++ --enable-haifa --enable-sjlj-exceptions --disable-libstdcxx-pch --disable-tls --enable-threads=amigaos --enable-lto
  6. Thread model: amigaos
  7. gcc version 8.4.0 (adtools build 8.4.0)
  8. COLLECT_GCC_OPTIONS='-I' '/opt/sdk/ppc-amigaos/Include/include_h' '-I' '/opt/sdk/SDL2/include' '-L/opt/sdk/SDL2/lib' '-Wall' '-gstabs' '-use-dynld' '-athread=native' '-o' 'sdl_example' '-v'
  9. /opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/cc1 -quiet -v -I /opt/sdk/ppc-amigaos/Include/include_h -I /opt/sdk/SDL2/include -mcrt=newlib -idirafter /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/include -idirafter /opt/ppc-amigaos/ppc-amigaos/SDK/local/newlib/include -idirafter /opt/ppc-amigaos/ppc-amigaos/SDK/include/include_h -idirafter /opt/ppc-amigaos/ppc-amigaos/SDK/include/netinclude -idirafter /opt/ppc-amigaos/ppc-amigaos/SDK/local/common/include sdl_example.c -quiet -dumpbase sdl_example.c -auxbase sdl_example -gstabs -Wall -version -o /tmp/ccI1hkGT.s
  10. GNU C17 (adtools build 8.4.0) version 8.4.0 (ppc-amigaos)
  11. compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version none
  12. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
  13. ignoring nonexistent directory "/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/sys-include"
  14. ignoring nonexistent directory "/opt/ppc-amigaos/ppc-amigaos/SDK/local/newlib/include"
  15. ignoring nonexistent directory "/opt/ppc-amigaos/ppc-amigaos/SDK/local/common/include"
  16. ignoring nonexistent directory "/opt/sdk/ppc-amigaos/Include/include_h"
  17. #include "..." search starts here:
  18. #include <...> search starts here:
  19. /opt/sdk/SDL2/include
  20. /opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/include
  21. /opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/include-fixed
  22. /opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/include
  23. /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/include
  24. /opt/ppc-amigaos/ppc-amigaos/SDK/include/include_h
  25. /opt/ppc-amigaos/ppc-amigaos/SDK/include/netinclude
  26. End of search list.
  27. GNU C17 (adtools build 8.4.0) version 8.4.0 (ppc-amigaos)
  28. compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version none
  29. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
  30. Compiler executable checksum: 577b1ea35dc54e20a6b3c7d50c83a311
  31. COLLECT_GCC_OPTIONS='-I' '/opt/sdk/ppc-amigaos/Include/include_h' '-I' '/opt/sdk/SDL2/include' '-L/opt/sdk/SDL2/lib' '-Wall' '-gstabs' '-use-dynld' '-athread=native' '-o' 'sdl_example' '-v'
  32. /opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/bin/as -mppc -mbig -o /tmp/ccYUO8qV.o /tmp/ccI1hkGT.s
  33. COMPILER_PATH=/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/bin/
  34. LIBRARY_PATH=/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/lib/
  35. COLLECT_GCC_OPTIONS='-I' '/opt/sdk/ppc-amigaos/Include/include_h' '-I' '/opt/sdk/SDL2/include' '-L/opt/sdk/SDL2/lib' '-Wall' '-gstabs' '-use-dynld' '-athread=native' '-o' 'sdl_example' '-v'
  36. /opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/collect2 -plugin /opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/liblto_plugin.so -plugin-opt=/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccV1xNYT.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc --eh-frame-hdr --defsym __amigaos4__=1 -q -d -V -Qy /opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/gthr-amigaos-native.o -dy -L/opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/newlib/lib -L/opt/ppc-amigaos/ppc-amigaos/SDK/local/newlib/lib -L/opt/ppc-amigaos/ppc-amigaos/SDK/local/common/lib -o sdl_example -use-dynld /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib/crtbegin.o -L/opt/sdk/SDL2/lib -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0 -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/lib -lSDL2 /tmp/ccYUO8qV.o -lgcc --start-group -lc --end-group -lgcc /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib/crtend.o
  37. GNU ld (GNU Binutils) 2.23.2
  38. Supported emulations:
  39. amigaos
  40. elf32ppc
  41. /tmp/ccYUO8qV.o: In function `main':
  42. sdl_example.c:8: undefined reference to `SDL_Init'
  43. sdl_example.c:9: undefined reference to `SDL_GetError'
  44. sdl_example.c:13: undefined reference to `SDL_CreateWindow'
  45. sdl_example.c:15: undefined reference to `SDL_GetError'
  46. sdl_example.c:19: undefined reference to `SDL_CreateRenderer'
  47. sdl_example.c:21: undefined reference to `SDL_GetError'
  48. sdl_example.c:23: undefined reference to `SDL_DestroyWindow'
  49. sdl_example.c:25: undefined reference to `SDL_Quit'
  50. sdl_example.c:29: undefined reference to `SDL_RWFromFile'
  51. sdl_example.c:29: undefined reference to `SDL_LoadBMP_RW'
  52. sdl_example.c:31: undefined reference to `SDL_GetError'
  53. sdl_example.c:33: undefined reference to `SDL_DestroyRenderer'
  54. sdl_example.c:36: undefined reference to `SDL_DestroyWindow'
  55. sdl_example.c:38: undefined reference to `SDL_Quit'
  56. sdl_example.c:42: undefined reference to `SDL_CreateTextureFromSurface'
  57. sdl_example.c:44: undefined reference to `SDL_GetError'
  58. sdl_example.c:46: undefined reference to `SDL_FreeSurface'
  59. sdl_example.c:49: undefined reference to `SDL_DestroyRenderer'
  60. sdl_example.c:52: undefined reference to `SDL_DestroyWindow'
  61. sdl_example.c:54: undefined reference to `SDL_Quit'
  62. sdl_example.c:57: undefined reference to `SDL_FreeSurface'
  63. sdl_example.c:60: undefined reference to `SDL_RenderClear'
  64. sdl_example.c:61: undefined reference to `SDL_RenderCopy'
  65. sdl_example.c:62: undefined reference to `SDL_RenderPresent'
  66. sdl_example.c:63: undefined reference to `SDL_Delay'
  67. sdl_example.c:66: undefined reference to `SDL_DestroyTexture'
  68. sdl_example.c:67: undefined reference to `SDL_DestroyRenderer'
  69. sdl_example.c:68: undefined reference to `SDL_DestroyWindow'
  70. sdl_example.c:69: undefined reference to `SDL_Quit'
  71. collect2: error: ld returned 1 exit status

@jabirulo
unfortunately -lpthread didn't work, because there is such file under newlib/lib folder

kas1e
kas1e's picture
Offline
Last seen: 20 hours 38 min ago
Joined: 2010-11-30 15:30
Re: Looking for a simple example

@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)

salass00
salass00's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-02-03 11:27
Re: Looking for a simple example

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

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.

walkero
walkero's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2009-05-03 16:54
Re: Looking for a simple example

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

Log in or register to post comments