Undefined reference errors to all functions whe trying to statically link my SDL2-based program

Viewed 551

I want to link statically my C++ SDL2-based program, but it's not working as expected.

I already placed all headers files, my program and the libSDL2main.a file in a single folder. Then I tried to compile the program.

g++ -c Static_linking.cpp -o static.o works perfectly

g++ static.o libSDL2main.a -o static but this line gives me a lot of linking errors.

/usr/bin/ld: static.o: in function `main':
Static_linking.cpp:(.text+0x15): undefined reference to `SDL_Init'
/usr/bin/ld: Static_linking.cpp:(.text+0x3c): undefined reference to `SDL_CreateWindow'
/usr/bin/ld: Static_linking.cpp:(.text+0x56): undefined reference to `SDL_CreateRenderer'
/usr/bin/ld: Static_linking.cpp:(.text+0x7b): undefined reference to `SDL_SetRenderDrawColor'
/usr/bin/ld: Static_linking.cpp:(.text+0x87): undefined reference to `SDL_RenderClear'
/usr/bin/ld: Static_linking.cpp:(.text+0x93): undefined reference to `SDL_RenderPresent'
/usr/bin/ld: Static_linking.cpp:(.text+0x9d): undefined reference to `SDL_Delay'
/usr/bin/ld: Static_linking.cpp:(.text+0xa9): undefined reference to `SDL_DestroyWindow'
/usr/bin/ld: Static_linking.cpp:(.text+0xb5): undefined reference to `SDL_DestroyRenderer'
/usr/bin/ld: Static_linking.cpp:(.text+0xca): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

PS: Everything works when I link the program dynamically with: g++ Static_linking.cpp -lSDL2 -o static

New Discovery

I was wrong, in fact there is a file called libSDL2.a (I don't know why it didn't show up for me on Files when I searched for it).

Although when I tried g++ static.o libSDL2main.a libSDL2.a -o static it gives me a message error so scary and big that I will only show here the beggining.

/usr/bin/ld: libSDL2.a(SDL_alsa_audio.o): in function `ALSA_Init':
(.text+0x43): undefined reference to `snd_pcm_open'
/usr/bin/ld: (.text+0x51): undefined reference to `snd_pcm_close'
/usr/bin/ld: (.text+0x5f): undefined reference to `snd_pcm_writei'
/usr/bin/ld: (.text+0x6d): undefined reference to `snd_pcm_readi'
/usr/bin/ld: (.text+0x7b): undefined reference to `snd_pcm_recover'
/usr/bin/ld: (.text+0x89): undefined reference to `snd_strerror'
/usr/bin/ld: (.text+0x97): undefined reference to `snd_pcm_hw_params_copy'
/usr/bin/ld: (.text+0xa5): undefined reference to `snd_pcm_hw_params_any'
/usr/bin/ld: (.text+0xb3): undefined reference to `snd_pcm_hw_params_set_access'
/usr/bin/ld: (.text+0xc1): undefined reference to `snd_pcm_hw_params_set_format'
/usr/bin/ld: (.text+0xcf): undefined reference to `snd_pcm_hw_params_set_channels'
/usr/bin/ld: (.text+0xdd): undefined reference to `snd_pcm_hw_params_get_channels'
/usr/bin/ld: (.text+0xeb): undefined reference to `snd_pcm_hw_params_set_rate_near'
/usr/bin/ld: (.text+0xf9): undefined reference to `snd_pcm_hw_params_set_period_size_near'
0 Answers
Related