当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Undefined reference to SDL2 functions in linked library (Code::Blocks)

问题描述:

I have created a static library that uses SDL2 and I'm having trouble using it in another project. All SDL2 functions referenced in the library are throwing undefined reference errors. I can use SDL2 in unit tests for the static library project so it must be set up right... right? I am linking with -lSDL2 etc the same as on other projects (including the unit test) where it's working. Shouldn't this be enough?

My linker settings look like this:

Linker settings in Code::Blocks

Here's the build log:

 -------------- 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))

Any help is appreciated. I've been stuck on this for a while :(

网友答案:

I managed to fix the problem. It seems that it was the linking order that was causing the issue. sdl_frame.a needed to be linked before SDL2 etc. By default, Code::blocks does the "Other linker options" before "Link libraries", so the order was wrong.

Old order:

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

Working order:

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

This is how it now looks in the linker settings:

Hopefully someone will find this useful. If anyone knows of a better fix, I'd be very interested.

分享给朋友:
您可能感兴趣的文章:
随机阅读: