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

2020-03-17 13:19发布

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?

3条回答
来,给爷笑一个
2楼-- · 2020-03-17 13:46

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楼-- · 2020-03-17 13:57

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`
查看更多
何必那么认真
4楼-- · 2020-03-17 14:06

In case you use SDL_ttf, you need to

g++ main.cpp -o sdl-test `sdl-config --libs --cflags` -lSDL_ttf
查看更多
登录 后发表回答