使用autoconf/automake,我该如何指定包含文件路径?

6
假设我想让生成的makefile将某些特定头文件路径传递给g++。 我需要在configure.ac或Makefile.am中添加什么来指定这一点?(注意-我不想在./configure时通过CPPFLAGS传递它。我希望在那一步之前就将这些路径集成进去)
编辑: 具体而言,我想包含 /usr/include/freetype 和 /mypath/include。我放AC_CHECK_HEADERS([freetype/config/ftheader.h]),它通过了,但似乎没有被添加到传递给g++ 的 -I 中。 我还尝试添加 CPPFLAGS=-I.:/usr/include/freetype:/mypath/include,但它会出问题并且会把-I写两遍,第一个是-I. 并且会忽略第二个 -I。

2
这看起来不是正确的操作。./configure 是找出编译系统上的事物位置的步骤,因此也是允许编译用户告诉它头文件路径的非默认位置的步骤。 - reinierpost
1
CPPFLAGS不是一个以冒号分隔的列表。你需要CPPFLAGS='-I/usr/include/freetype -I/mypath/include'。 - William Pursell
2个回答

11

我同意这是对问题最直接的实际答案。只需在您的Makefile.am中添加一行,您就可以为构建目标指定自定义包含路径。 - Tchakabam

9
将路径硬编码到软件包文件中绝对是错误的做法。如果您选择这样做,那么您需要意识到您正在违反使用autotools构建软件包的基本规则。如果在软件包文件中指定了/mypath/include,那么您正在为旨在在所有计算机上运行的软件包指定特定于您计算机的内容;显然这是错误的。看起来您想要的是让您的软件包(在您的计算机上构建时)在/mypath中查找头文件。可以轻松实现此目标,而不会破坏您的软件包。至少有3种方法可以做到:
  1. Use a config.site file. In /usr/local/share/config.site (create this file if necessary), add the line:

    CPPFLAGS="$CPPFLAGS -I/mypath/include"
    

    Now any package using an autoconf generated configure script with the default prefix (/usr/local) will append -I/mypath/include to CPPFLAGS and the headers in /mypath/include will be found.

  2. If you want the assignment to be made for all builds (not just those to be installed in /usr/local), you can use this:

    Put the same line specifying CPPFLAGS in $HOME/config.site, and set CONFIG_SITE=$HOME/config.site in the environment of your default shell. Now, whenever you run an autoconf generated configure script, the assignments from $HOME/config.site will be made.

  3. Simply specify CPPFLAGS in the environment of your default shell.

所有这些解决方案相比于修改构建文件,具有两个主要优点。 首先,它们将适用于所有由autoconf生成的软件包(只要它们遵循规则,并且不会在构建文件中分配用户变量,如CPPFLAGS)。 其次,它们不会将您的机器特定信息放入一个应该在所有计算机上运行的软件包中。

听起来很不错,但是... configure 生成的 Makefile 产生了一个编译语句,它没有引用环境变量 CFLAGS 或 CPPFLAGS。 - Cheeso
如果该软件包使用 automake 进行构建,那么默认规则应该在适当的地方包含 $(CFLAGS)$(CPPFLAGS)。如果没有这样做,那么软件包维护人员就严重滥用了 automake。也许你正在查看的软件包使用手写的 Makefile.in - William Pursell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接