Ubuntu 11.10: GCC/G++ won't link libraries

戏子无情 提交于 2019-12-01 01:32:33

问题


I went to compile a project of mine, which uses SDL, SDL_ttf, OpenAL, and GTK. All of which are outputting errors like the following:

TxtFunc.cpp:(.text+0x61): undefined reference to `TTF_OpenFont'
TxtFunc.cpp:(.text+0x8c): undefined reference to `TTF_RenderText_Solid'
TxtFunc.cpp:(.text+0xf6): undefined reference to `SDL_UpperBlit'
TxtFunc.cpp:(.text+0x108): undefined reference to `TTF_CloseFont'
TxtFunc.cpp:(.text+0x114): undefined reference to `SDL_FreeSurface'

for every library call. I am compiling with the following link options:

sdl-config --libs pkg-config gtk+-2.0 --libs pkg-config openal --libs -lalut -lSDL_ttf

I have all of these packages installed, and there are no "file not found" errors. Just a lot of undefined references... It didn't make sense, so I wrote up a quick test application:

#include "SDL/SDL.h"

int main()
{
    SDL_Init(SDL_INIT_VIDEO);
    return 0;
}

and compile like so:

g++ `sdl-config --libs --cflags` -o sdl-test ./sdl-test.cpp

I have even tried linking directly to "/usr/lib/libSDL-1.2.so.0" or "/usr/lib/libSDL.a" instead

all of these options end up with the same output:

/tmp/cc05XT8U.o: In function `main':
sdl-test.cpp:(.text+0xa): undefined reference to `SDL_Init'
collect2: ld returned 1 exit status

Anybody have any ideas?


回答1:


Generally you need to have your -l options after the files that use the symbols on the command line. Perhaps try moving the sdl-config --libs --cflags to the end of the command? i.e. for your test program:

g++ -o sdl-test ./sdl-test.cpp `sdl-config --libs --cflags`



回答2:


Gah, which pillock got the idea to change the compiler to rely now on the order of options in the command line?

Well,that fixed my problem too, just moved ($CFLAGS) after ($OBJS) in my Makefile and all my linking problems with unknown references to SDL libs are gone >.<




回答3:


In case you use SDL_ttf, you need to

g++ main.cpp -o sdl-test `sdl-config --libs --cflags` -lSDL_ttf


来源:https://stackoverflow.com/questions/8498899/ubuntu-11-10-gcc-g-wont-link-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!