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

时间:2015-10-18 00:15:19

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

我创建了一个使用SDL2的静态库,我在另一个项目中使用它时遇到了麻烦。库中引用的所有SDL2函数都抛出了未定义的引用错误。我可以在静态库项目的单元测试中使用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))

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

1 个答案:

答案 0 :(得分:2)

我设法解决了这个问题。似乎是导致问题的链接顺序。 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

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