如何使用自动工具设置包含路径 [英] how to set include paths with autotools

查看:110
本文介绍了如何使用自动工具设置包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用autoconf& automake,而我正在努力正确设置*CPPFLAGS中的包含路径.我已经阅读了大约3个小时的文档,但还无法弄清楚.我不是在寻找hack,而是在寻找正确的方法.这是我的难题.

I'm working on a C++ project that uses autoconf & automake, and I'm struggling to correctly set up the include paths in *CPPFLAGS. I've read about 3 hours worth of documents, and I can't figure it out yet. I'm not looking for a hack, but for the correct way to do this. Here is my conundrum.

如我所见,包含路径有3种完全不同的来源:

As I see it, there are 3 completely different sources for include paths:

  1. 必须与我的软件包一起安装的外部库,这些库由configure --with-XXX=<PATH>配置.
  2. 在我的程序包中,即使file.h是程序包的一部分,某些源文件也使用#include <file.h>,因此要编译它们,我必须正确设置包含路径. (请注意,这不是编辑所有这些文件的选项.)
  3. 异想天开的标准(或非标准)指定必须允许用户指定自己的(额外)包含路径.也就是说,我根本不应该设置CPPFLAGS.
  1. External libraries that must be installed along with my package, which are configured by configure --with-XXX=<PATH>.
  2. Within my package, some source files use #include <file.h> even when file.h is part of the package, so to compile them, I must set the include path correctly. (Note, it's not an option to edit all these files.)
  3. Whimsical (or not) standards specify the user must be allowed to specify their own (extra) include paths. That is, I shouldn't be setting CPPFLAGS at all.

在我当前的设置中:

  • 类型1路径由AC_SUBST(CPPFLAGS, "$CPPFLAGS -I<path>")设置在configure.ac内部.
  • 类型2路径由test_CPPFLAGS = -I<path>设置在Makefile.am内部.
  • 无法设置类型3.更确切地说,如果用户在运行make之前设置了CPPFLAGS,则它将覆盖Type 1设置,从而导致编译失败.当然,用户可以尝试使用CXXFLAGS代替,但是该用户具有不同的用法(请记住,我要求的是执行此操作的正确方法,而不是hack).
  • Type 1 paths are set inside configure.ac by AC_SUBST(CPPFLAGS, "$CPPFLAGS -I<path>").
  • Type 2 paths are set inside Makefile.am by test_CPPFLAGS = -I<path>.
  • Type 3 cannot be set. More exactly, if the user sets CPPFLAGS before running make, this overrides Type 1 settings, causing compilation to fail. Of course, the user could try to use CXXFLAGS instead, but that one has a different use (remember, I'm asking for the correct way to do this, not a hack).

我试图通过在configure.ac中使用AM_CPPFLAGS设置类型1路径来解决此问题. (供参考:如果您设置了AM_CPPFLAGS而不是CPPFLAGS,但是您仍然需要运行某些检查,例如AC_CHECK_HEADERS,则需要临时设置CPPFLAGS,然后将其还原以使检查生效;这是此处.)这将释放类型3路径的CPPFLAGS,但不幸的是,编译失败,因为如果没有专门的<target>_CPPFLAGS,由configure生成的Makefile -s将仅使用AM_CPPFLAGS.因此,如果test_CPPFLAGS存在类型2路径,则编译test将失败,因为它没有获得类型1路径.

I tried to fix this by setting Type 1 paths using AM_CPPFLAGS inside configure.ac. (For reference: if you set AM_CPPFLAGS instead of CPPFLAGS, but you still need to run some checks such as AC_CHECK_HEADERS, you need to temporarily set CPPFLAGS and then revert it for the checks to work; this is explained here.) This frees up CPPFLAGS for Type 3 paths, but unfortunately the compilation fails because the Makefile-s that gets produced by configure will only use AM_CPPFLAGS if no specialized <target>_CPPFLAGS exists. So, if test_CPPFLAGS exists with a Type 2 path, compiling test will fail because it doesn't get the Type 1 path.

一种解决方法是在Makefile.am内部指定始终使用AM_CPPFLAGS.但这是按书"吗?我可以以全局方式执行此操作,还是必须编辑每个target_CPPFLAGS?还有另一种正确"的解决方案吗?

A fix would be to specify inside Makefile.am to always use AM_CPPFLAGS. But is this "by the book"? Can I do this in a global way, or do I have to edit every single target_CPPFLAGS? Is there another "correct" solution?

推荐答案

我知道很难从自动工具手册中直接获得答案.在此处

I know it's difficult to get a straight answer from the autotools manuals. There are a couple of good start-to-finish tutorials here and here.

autoconf中没有特定于软件包的*CPPFLAGS的标准变量.可以使用CPPFLAGS=...调用configure,并且automake会将CPPFLAGS添加到相关的makefile规则中-在Makefile.in文件中搜索CPPFLAGS以获取示例.因此,我建议您不要将此变量用于其他任何用途.

There isn't a standard variable for package-specific *CPPFLAGS in autoconf. configure can be invoked with CPPFLAGS=..., and automake will add this CPPFLAGS to the relevant makefile rules - search for CPPFLAGS in a Makefile.in file for examples. For that reason, I suggest that you not use this variable for anything else.

Makefile.am中的标志添加到AM_CPPFLAGS变量(所有预处理器调用的默认设置),或使用target_CPPFLAGS覆盖各个预处理器标志.在第3方库的示例中,最好使用这样的名称:FOO_CPPFLAGS来保存预处理器选项,例如,

Add flags in Makefile.am to the AM_CPPFLAGS variable (the default for all preprocessor calls) or override individual preprocessor flags with target_CPPFLAGS. In the example of a 3rd party library, it's best to use a name like: FOO_CPPFLAGS to hold preprocessor options, e.g.,

FOO_CPPFLAGS="-I${FOO_DIR}/include -DFOO_BAR=1"
...
AC_SUBST(FOO_CPPFLAGS)

以及Makefile.am:

AM_CPPFLAGS = -I$(top_srcdir) $(FOO_CPPFLAGS)
# or:
target_CPPFLAGS = -I$(top_srcdir) $(FOO_CPPFLAGS)

top_srcdir变量由configure定义-我用它来说明第二种情况.假设您在顶级目录下的另一个目录other中具有file.h. -I$(top_srcdir)允许您将其包含为<other/file.h>.另外,-I$(top_srcdir)/other允许您将其包含为<file.h>.

The top_srcdir variable is defined by configure - I use it to illustrate the 2nd case. Let's say you have file.h in another directory other, under the top-level directory. -I$(top_srcdir) allows you to include it as <other/file.h>. Alternatively, -I$(top_srcdir)/other would allow you to include it as <file.h>.

另一个有用的预设变量srcdir-当前目录. -I$(srcdir)默认添加到AM_CPPFLAGS .因此,如果file.h在当前目录中,则可以将其包含在<file.h>甚至"file.h"中.如果other是兄弟"目录,则-I$(srcdir)/..将允许您包含<other/file.h>,而-I$(srcdir)/../other将允许<file.h>.

Another useful preset variable is srcdir - the current directory. -I$(srcdir) is added to AM_CPPFLAGS by default. So if file.h is in the current directory you can include it with <file.h> or even "file.h". If other was a 'sibling' directory, -I$(srcdir)/.. would allow you to include <other/file.h>, and -I$(srcdir)/../other would allow <file.h>.

我还要补充一点,有些软件包会安装pkg-config .pc文件.如果设置了pkg-config的安装来搜索正确的目录,您可能会发现PKG_CHECK_MODULES宏非常有用.

I'd also add that some packages install a pkg-config .pc file. Provided the installation of pkg-config is set up to search the right directories, you might find the PKG_CHECK_MODULES macro very useful.

这篇关于如何使用自动工具设置包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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