c++ - 尝试静态链接我的基于 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'
解决方案
推荐阅读
- html - 如何减小 CSS 卡片显示中图像的大小
- r - 提高 R 中 data.table 中两列的粘贴速度(可重现)
- php - php 计算器 - Switch 语句问题
- spring-boot - Togglz 不从 application.yml 获取 Spring-Boot 配置
- webpack - webpack 开发服务器未构建和重新加载 index.html
- java - Spring Boot 电子邮件发送抛出 SocketTimeoutException: Read timed out
- node.js - 如何使用 Bale bot 向群组发送消息?
- angular - ngFor 内部的数据绑定
- jquery - 使用 jQuery 删除元素后,AngularJS 观察者仍然触发?
- python - 使用 spyder (python) 打开一个网站并根据时间刷新它