简体   繁体   English

如何在同一个 autotools 项目中为两个程序提供不同的构建选项

[英]How to have different build options for two programs in the same autotools project

I have a simple autotools project with two programs: one and two .我有一个简单的 autotools 项目, one包含两个程序: onetwo

Only one of the program depends on a library ( math in this example), and I would like that the other program not to be linked with this library.只有一个程序依赖于一个库(在这个例子中是math ),我希望另一个程序不要与这个库链接。

Here are my files:这是我的文件:

configure.ac配置文件

AC_INIT([test], [0.1], [somebody@example.com])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_PROG_CC
AC_CHECK_LIB([m], [log])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT    

Makefile.am生成文件

bin_PROGRAMS = one two
one_SOURCES = one.c
two_SOURCES = two.c

one.c (headers removed) one.c (已删除标题)

int main(void)
{
    /* do depend on lib math*/
    printf("log[15] = %f\n", log(15));
    return 0;
}

two.c (headers removed) two.c (已删除标题)

int main(void)
{
    /* do NOT depend on lib math*/
    printf("15 = %d\n", 15);
    return 0;
}

When I build this当我建立这个

autoreconf --install
./configure
make

The programs are well build :这些程序构建良好:

# compilation 
gcc .... -c -o one.o one.c
# link
gcc .... -o one one.o -lm
# compilation 
gcc .... -c -o two.o two.c
# link THE PROBLEM IS HERE, I don't want `-lm` to be added here
gcc .... -o two two.o -lm     

Only one of the program depends on a library (math in this example), and I would like that the other program not to be linked with this library.只有一个程序依赖于一个库(在这个例子中是数学),我希望另一个程序不要与这个库链接。

It takes some care to build programs with different options in the same Autotools project.在同一个 Autotools 项目中构建具有不同选项的程序需要一些小心。 The usual approach is to create separate output variables for those bits that are not common to all projects, and use them in your Makefile.am to define appropriate per-target build variables.通常的方法是为那些并非所有项目通用的位创建单独的输出变量,并在您的Makefile.am使用它们来定义适当的每个目标构建变量。

In your example, it is the link options specifying the math library that are target-specific, so you need to capture them in their own variable.在您的示例中,链接选项指定特定于目标的数学库,因此您需要将它们捕获在它们自己的变量中。 The AC_CHECK_LIB and AC_SEARCH_LIBS macros both prepend the appropriate library link option to the LIBS output variable, which is one of the sources from which Automake draws global link options, so if you use these, you need to also do something to avoid the math library option from remaining in LIBS . AC_CHECK_LIBAC_SEARCH_LIBS宏都将适当的库链接选项添加到LIBS输出变量,这是 Automake 绘制全局链接选项的来源之一,因此如果您使用这些,您还需要做一些事情来避免数学库选项从留在LIBS Alternatively, you could devise some other mechanism for testing the math library options.或者,您可以设计一些其他机制来测试数学库选项。

One good trick would be to save the value of LIBS before running AC_CHECK_LIB , extract the math library options, if any, afterward, and then restore the original value of LIBS .一个很好的技巧是在运行AC_CHECK_LIB之前保存LIBS的值,然后提取数学库选项(如果有),然后恢复LIBS的原始值。 This is a relatively common Autoconf idiom.这是一个比较常见的 Autoconf 习惯用法。 For example,例如,

LIBS_save=$LIBS

AC_CHECK_LIB([m], [log])

LIBM_LDFLAGS=${LIBS%${LIBS_save}}
AC_SUBST([LIBM_LDFLAGS])

LIBS=$LIBS_save

Your Makefile.am might then look like so:你的Makefile.am可能看起来像这样:

bin_PROGRAMS = one two

one_SOURCES = one.c
one_LDADD = $(LIBM_LDFLAGS)

two_SOURCES = two.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM