c++ - 未定义对链接库中 SDL2 函数的引用 (Code::Blocks)

标签 c++ codeblocks static-linking sdl-2

我创建了一个使用 SDL2 的静态库,但在另一个项目中使用它时遇到问题。库中引用的所有 SDL2 函数都抛出 undefined reference 错误。我可以在静态库项目的单元测试中使用 SDL2,因此必须正确设置它……对吗?我正在与 -lSDL2 等链接,就像它正在工作的其他项目(包括单元测试)一样。这还不够吗?

我的链接器设置如下所示: Linker settings in Code::Blocks

这是构建日志:

    -------------- Build: Debug in sdl_frame_sample (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -std=c++11 -Wextra -Wall -I../../sdl-frame/sdl_frame/include -I../../sdl-frame/sdl_frame/include/box2d -Iinclude -I/home/yeti/Programs/gtest-1.7.0/include -c /home/yeti/Documents/codes/sdl_frame_samples/sdl_frame_sample/main.cpp -o obj/Debug/main.o
g++ -Wall -fexceptions -g -std=c++11 -Wextra -Wall -I../../sdl-frame/sdl_frame/include -I../../sdl-frame/sdl_frame/include/box2d -Iinclude -I/home/yeti/Programs/gtest-1.7.0/include -c /home/yeti/Documents/codes/sdl_frame_samples/sdl_frame_sample/src/sample_game_t.cpp -o obj/Debug/src/sample_game_t.o
g++ -Wall -fexceptions -g -std=c++11 -Wextra -Wall -I../../sdl-frame/sdl_frame/include -I../../sdl-frame/sdl_frame/include/box2d -Iinclude -I/home/yeti/Programs/gtest-1.7.0/include -c /home/yeti/Documents/codes/sdl_frame_samples/sdl_frame_sample/src/sample_world_t.cpp -o obj/Debug/src/sample_world_t.o
g++ -L../sdl-frame/sdl_frame/bin/Debug -L/home/yeti/Programs/gtest-1.7.0 -o bin/Debug/sdl_frame_sample obj/Debug/main.o obj/Debug/src/sample_game_t.o obj/Debug/src/sample_world_t.o  -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf  ../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a /home/yeti/Programs/gtest-1.7.0/libgtest.a
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_t.o): In function `game_t::~game_t()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:31: undefined reference to `SDL_Quit'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:32: undefined reference to `TTF_Quit'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_t.o): In function `game_t::init()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:40: undefined reference to `SDL_Init'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:42: undefined reference to `SDL_GetError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:48: undefined reference to `TTF_Init'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:50: undefined reference to `SDL_GetError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_t.o): In function `game_t::start()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:87: undefined reference to `SDL_LogError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:93: undefined reference to `SDL_LogError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:121: undefined reference to `SDL_LogError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_t.o): In function `game_t::add_world(game_world_t*)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:155: undefined reference to `SDL_LogError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_t.o): In function `game_t::add_hud(hud_t*)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_t.cpp:166: undefined reference to `SDL_LogError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_timer_t.o): In function `game_timer_t::start()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_timer_t.cpp:23: undefined reference to `SDL_GetTicks'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_timer_t.o): In function `game_timer_t::resume()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_timer_t.cpp:45: undefined reference to `SDL_GetTicks'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_timer_t.o): In function `game_timer_t::pause()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_timer_t.cpp:58: undefined reference to `SDL_GetTicks'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_timer_t.o): In function `game_timer_t::get_ticks()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_timer_t.cpp:77: undefined reference to `SDL_GetTicks'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_window_t.o): In function `game_window_t::init(std::string, int, int, unsigned int)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:34: undefined reference to `SDL_CreateWindow'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:37: undefined reference to `SDL_GetError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:42: undefined reference to `SDL_CreateRenderer'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:45: undefined reference to `SDL_GetError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:46: undefined reference to `SDL_DestroyWindow'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_window_t.o): In function `game_window_t::render()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:62: undefined reference to `SDL_SetRenderDrawColor'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:63: undefined reference to `SDL_RenderClear'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:87: undefined reference to `SDL_RenderPresent'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_window_t.o): In function `game_window_t::set_position(int, int)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:143: undefined reference to `SDL_SetWindowPosition'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_window_t.o): In function `game_window_t::free()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:148: undefined reference to `SDL_DestroyRenderer'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/game_window_t.cpp:150: undefined reference to `SDL_DestroyWindow'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_world_t.o): In function `game_world_t::update_game_objects(unsigned int)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/world/game_world_t.cpp:93: undefined reference to `SDL_LogError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(world_image_t.o): In function `world_image_t::init(std::string)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/world/world_image_t.cpp:23: undefined reference to `SDL_LogError'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(asset_manager_t.o): In function `asset_manager_t::~asset_manager_t()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:16: undefined reference to `TTF_CloseFont'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(asset_manager_t.o): In function `asset_manager_t::load_text_texture(std::string, std::string, int, SDL_Color)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:42: undefined reference to `TTF_OpenFont'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(asset_manager_t.o): In function `asset_manager_t::load_texture_from_surface(SDL_Surface*, SDL_Renderer*)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:143: undefined reference to `SDL_GetError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:148: undefined reference to `SDL_CreateTextureFromSurface'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:151: undefined reference to `SDL_GetError'
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:155: undefined reference to `SDL_FreeSurface'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(asset_manager_t.o): In function `asset_manager_t::load_texture_from_text(_TTF_Font*, std::string, SDL_Color)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:164: undefined reference to `TTF_RenderText_Solid'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(asset_manager_t.o): In function `asset_manager_t::load_texture_from_image(std::string)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/asset_manager_t.cpp:177: undefined reference to `IMG_Load'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_texture_t.o): In function `game_texture_t::render(float, float, float, float)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/game_texture_t.cpp:27: undefined reference to `SDL_RenderCopy'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_texture_t.o): In function `game_texture_t::render(SDL_Rect*, SDL_Rect*)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/game_texture_t.cpp:37: undefined reference to `SDL_RenderCopy'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_texture_t.o): In function `game_texture_t::render(SDL_Rect*, double, SDL_Point*, SDL_Rect*)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/game_texture_t.cpp:58: undefined reference to `SDL_RenderCopyEx'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(game_texture_t.o): In function `game_texture_t::get_texture_dimensions(texture_dimensions_t&)':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/game_texture_t.cpp:68: undefined reference to `SDL_QueryTexture'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(tracked_SDL_Texture.o): In function `tracked_SDL_Texture::~tracked_SDL_Texture()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/asset_management/tracked_SDL_Texture.cpp:10: undefined reference to `SDL_DestroyTexture'
../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a(controller_t.o): In function `controller_t::refresh()':
/home/yeti/Documents/codes/sdl-frame/sdl_frame/src/controller_t.cpp:20: undefined reference to `SDL_PollEvent'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
42 error(s), 0 warning(s) (0 minute(s), 0 second(s))

感谢任何帮助。我已经坚持了一段时间:(

最佳答案

我设法解决了这个问题。似乎是链接顺序导致了问题。 sdl_frame.a 需要在 SDL2 等之前链接。默认情况下,Code::blocks 在“链接库”之前执行“其他链接器选项”,因此顺序错误。

旧顺序:

g++ -L../sdl-frame/sdl_frame/bin/Debug -L/home/yeti/Programs/gtest-1.7.0 -o bin/Debug/sdl_frame_sample obj/Debug/main.o obj/Debug/src/sample_game_t.o obj/Debug/src/sample_world_t.o  -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf  ../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a /home/yeti/Programs/gtest-1.7.0/libgtest.a

工作顺序:

g++ -L../sdl-frame/sdl_frame/bin/Debug -L/home/yeti/Programs/gtest-1.7.0 -o bin/Debug/sdl_frame_sample obj/Debug/main.o obj/Debug/src/sample_game_t.o obj/Debug/src/sample_world_t.o  ../../sdl-frame/sdl_frame/bin/Debug/libsdl_frame.a -lSDL2 -lSDL2main -lSDL2_ttf -lSDL2_image  /home/yeti/Programs/gtest-1.7.0/libgtest.a

这是它现在在链接器设置中的样子:

enter image description here

希望有人会觉得这很有用。如果有人知道更好的修复方法,我会非常感兴趣。

关于c++ - 未定义对链接库中 SDL2 函数的引用 (Code::Blocks),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192991/

相关文章:

c++ - 如何在构建我的 clang 插件时绕过依赖构建?

c++ - 在事先不知道参数个数的情况下调用函数

c++将基类转换为派生类困惑

c++ - 在静态链接应用程序中使用函数 gethostbyname 的浮点异常

c++ - Visual Studio 2008 IDE - 静态链接 C Dll 库

c++ - 在 OpenGL 中绘制物体的轨迹

c++ - 井字游戏

C 未知类型名称 'DATA'

c++ - C++ [Codeblocks/Visual Studio] 中没有这样的目录或文件

go - standard_init_linux.go :207: exec user process caused "no such file or directory" while trying to statically link c libs