首页 > 解决方案 > 尝试静态链接我的基于 SDL2 的程序时对所有函数的未定义引用错误

问题描述

我想静态链接我的基于 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(我不知道为什么当我搜索它时它没有出现在文件中)。

虽然当我尝试时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++sdlsdl-2static-linking

解决方案


推荐阅读