Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

autotools - Linker flags in wrong place

I'm trying to use Autotools to build my C program that needs to be linked against certain libraries. It only contains one C source file.

This is the Makefile.am associated with it:

bin_PROGRAMS=game
game_SOURCES=main.c
game_CFLAGS=`pkg-config --cflags libglfw`
game_LDFLAGS=`pkg-config --libs libglfw`

When I run make, it tries to compile it using this:

gcc `pkg-config --cflags libglfw` -g -O2 `pkg-config --libs libglfw`  -o game game-main.o

However this is wrong, as the library link flags must be at the end, or else it will give errors about undefined references. For example if I run this:

gcc `pkg-config --cflags libglfw` -g -O2   -o game game-main.o `pkg-config --libs libglfw`

It compiles fine.

How can I make it so the LDFLAGS primary is appended at the end rather than in the middle?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can start by not abusing LDFLAGS for libraries. LDFLAGS is for linker flags. Use foo_LDADD (for executables) or foobar_LIBADD (when producing a library) to list link libraries.

Also, running pkg-config inside Makefile.am is unnecessary and wasteful. Just use:

game_CFLAGS = ${libglfw_CFLAGS}
game_LDADD  = ${libglfw_LIBS}

libglfw_CFLAGS,LIBS is populated by this in configure.ac:

PKG_CHECK_MODULES([libglfw], [libglfw])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...