在试图静态链接我的基于SDL2的程序时,所有函数都出现未定义引用错误

问题描述 投票:3回答:1

我想静态地链接我的基于C++ SDL2的程序,但它没有像预期的那样工作。

我已经把所有的头文件,我的程序和 libSDL2main.a 文件在一个文件夹中。然后我试着编译程序。

g++ -c Static_linking.cpp -o static.o 完美的工作

g++ static.o libSDL2main.a -o static 但这一行给我带来很多链接错误。

/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:当我用动态链接程序时,一切都正常。g++ Static_linking.cpp -lSDL2 -o static

新发现

我错了,其实有一个文件叫 libSDL2.a 我不知道为什么我搜索它的时候在Files上没有显示出来)。

虽然当我尝试 g++ static.o libSDL2main.a libSDL2.a -o static 它给了我一个可怕的大错误信息,我在这里只显示开头。

/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'
c++ sdl-2 static-linking
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.