使用Automake更正GCC命令行顺序 [英] Correcting the GCC command line ordering using Automake

查看:94
本文介绍了使用Automake更正GCC命令行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Mac上可以正常编译的自动工具项目,但是在Linux(Ubuntu 12.04.1 LTS)下,传递给gcc的命令行使库相对于目标文件而言是乱序的.例如,autotools生成以下命令来编译我的代码,将名为test.c的单个文件转换为名为test的二进制文件:

I have an autotools project that compiles just fine on the Mac, but under Linux (Ubuntu 12.04.1 LTS) the command lines passed to gcc have the libraries out of order relative to the object files. For example, autotools generates the following command to compile my code, a single file named test.c into a binary named test:

gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lglib-2.0 -o test test-test.o

此命令行失败:

/home/user/glib-test/test.c:4: undefined reference to `g_malloc`
/home/user/glib-test/test.c:5: undefined reference to `g_free`

但是,如果我从命令行进行编译并进行切换,以使库引用位于目标文件之后,那么它就可以正常工作:

However, if I compile from the command line and switch it up so the library reference is after the object files it works just fine:

gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -o test test-test.o -lglib-2.0

挑战在于,我无法弄清楚如何强制Autotools以正确的顺序生成命令行.为了清楚起见,我在这里重现了简单的测试用例.首先是configure.ac:

The challenge is that I can't figure out how to force Autotools to generate the command line in the right order. For the sake of clarity, I've reproduced the simple test case here. First up is configure.ac:

dnl Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(glib-test, 1.0)

AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE()

AC_PROG_CC
AM_PROG_CC_C_O
PKG_CHECK_MODULES(GLIB, glib-2.0 > 2.0)
AC_CONFIG_FILES(Makefile)
AC_OUTPUT

接下来是简单的Makefile.am:

CFLAGS=-Wall
bin_PROGRAMS=test
test_CFLAGS=$(GLIB_CFLAGS)
test_LDFLAGS=$(GLIB_LIBS)
test_SOURCES=test.c

最后,此最小测试用例的源代码test.c:

Finally, the source code to this minimal test case, test.c:

#include <glib.h>

int main(int argc, char **argv) {
    gchar *foo = g_malloc(100);
    g_free(foo);
    return 0;
}

然后使用以下一系列命令来实现编译:

Compilation is then achieved using the following series of commands:

touch NEWS README AUTHORS ChangeLog
aclocal
autoconf
automake --add-missing
./configure
make

我应该很清楚,我理解为什么我的代码无法编译,我只是想知道如何获取automake并将库放在命令行的末尾,以便gcc能够正确执行和链接.应该注意的是,Mac OS X Lion上的gcc似乎没有此问题.

I should be clear, I understand why my code won't compile, I'm just wondering how to get automake to put the libraries at the end of the command line so gcc will execute and link properly. It should be noted that gcc on Mac OS X Lion doesn't seem to have this problem.

推荐答案

原来,解决方案是LDFLAGSLDADD之间的区别.简而言之,LDFLAGS是在命令行上的目标文件之前添加的,LDADD是其后添加的.因此,将Makefile.am更改为以下内容可以解决问题:

The solution turned out to be the difference between LDFLAGS and LDADD. In short LDFLAGS is added before the object files on the command line and LDADD is added afterwards. Thus, changing Makefile.am to the following solved the problem:

CFLAGS=-Wall
bin_PROGRAMS=test
test_CFLAGS=$(GLIB_CFLAGS)
test_LDADD=$(GLIB_LIBS)
test_SOURCES=test.c

仅需跟踪工作中的GCC开发人员即可解决.另外,我提供的这个示例相当糟糕,因为test在某些自动工具的上下文中具有定义的含义.

It only took tracking down a GCC developer at work to solve. Also, this example I provided is rather poor because test has a defined meaning in some contexts of autotools.

这篇关于使用Automake更正GCC命令行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆