Autotools:如何设置全局编译标志

发布于 2024-07-09 15:46:49 字数 320 浏览 8 评论 0原文

我有一个包含多个源目录的项目:

src/A
   /B
   /C

在每个源目录中,Makefile.am 包含

AM_CXXFLAGS = -fPIC -Wall -Wextra

如何避免在每个源文件夹中重复此操作?

我尝试修改 src/Makefile.am 和 configure.in,但没有成功。 我以为我可以使用 AC_PROG_CXX 全局设置编译标志,但找不到太多有关如何使用这些宏的文档(您有指向此类文档的指针吗?)。

提前致谢

I have a project with several sources directories :

src/A
   /B
   /C

In each, the Makefile.am contains

AM_CXXFLAGS = -fPIC -Wall -Wextra

How can avoid repeating this in each source folder ?

I tried to modifiy src/Makefile.am and the configure.in, but without success. I thought I could use AC_PROG_CXX to set the compilation flags globally but can't find much documentation on how to use those macro (do you have any pointer to such a documentation ?).

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-07-16 15:46:49

您可以执行以下操作:

(1) 一种解决方案是在所有 Makefile.am 上包含一个通用 makefile 片段:

include $(top_srcdir)/common.mk
...
bin_PROGRAMS = foo
foo_SOURCES = ...

在这种情况下,您将写入

AM_CXXFLAGS = -fpic -Wall -Wextra

common.mk将来,只需编辑此文件即可更轻松地向所有 Makefile.am 添加更多宏或规则。

(2) 另一种解决方案是在 configure.ac 中全局设置这些变量(名称 configure.in 早已被弃用),如下所示

...
AC_SUBST([AM_CXXFLAGS], [-fpic -Wall -Wextra])
...

:甚至不必在 Makefile.am 中说明任何内容,它们会自动继承此全局定义。 缺点是您无法轻松选择退出(使用第一个解决方案很容易决定不包含 common.mk),并且依赖关系对于第三方人员来说并不明确(当他们阅读 Makefile.am 他们没有提示标志可能来自哪里)。

(3) 第三种解决方案是按照 orsogufo 建议进行:覆盖 configure.ac 中的用户变量 CXXFLAGS。 我建议不要这样做,因为它破坏了 GNU 构建系统的功能之一:允许用户在 make 时覆盖此变量。 例如,您可能想

make CXXFLAGS='-O0 -ggdb' 

在调试一段代码时键入,这将覆盖 CXXFLAGS 的任何定义(但是
不是 AM_CXXFLAGS 中的那些)。 老实说,大多数项目都无法正确支持这一点,因为它们使用了 CXXFLAGS

最后,我应该提到 -fpic-Wall-Werror 不是可移植选项。
根据您的项目范围,您可能希望为这些添加配置检查(gnulib 最近获取了新的宏来测试警告标志,并且 libtool 可用于构建共享库) 。

You can do several things:

(1) One solution is to include a common makefile fragment on all your Makefile.ams:

include $(top_srcdir)/common.mk
...
bin_PROGRAMS = foo
foo_SOURCES = ...

in that case you would write

AM_CXXFLAGS = -fpic -Wall -Wextra

to common.mk and in the future it will be easier to add more macros or rules to all Makefile.ams by just editing this file.

(2) Another solution would be to set these variables globally in your configure.ac (the name configure.in has been deprecated long ago), as in :

...
AC_SUBST([AM_CXXFLAGS], [-fpic -Wall -Wextra])
...

Then you don't even have to say anything in your Makefile.ams, they automatically inherit this global definition. The drawback is that you can't opt-out easily (with the first solution it's easy to decide not to include common.mk) and the dependency is not really explicit to third-party people (when they read the Makefile.am they have no hint about where the flags may come from).

(3) A third solution would be to do as orsogufo suggested: overwriting the user variable CXXFLAGS in configure.ac. I would advise against it, because it defeats one of the features of the GNU Build System: users are allowed to override this variable at make-time. For instance you may want to type

make CXXFLAGS='-O0 -ggdb' 

when debugging a piece of code, and this will overwrite any definition of CXXFLAGS (but
not those in AM_CXXFLAGS). To be honest, most projects fails to support this correctly because they play tricks with CXXFLAGS.

Finally, I should mention that -fpic, -Wall, and -Werror are not portable options.
Depending on the scope of your project you may want to add configure check for these (gnulib recently acquired new macros to tests for warnings flags, and libtool can be used to build shared libraries).

謌踐踏愛綪 2024-07-16 15:46:49

在configure.ac中使用EXTRA_CFLAGS

例如,对于您的情况,它是这样的:

EXTRA_CFLAGS=-fPIC -Wall -Wextra

Use EXTRA_CFLAGS in configure.ac

For example for your case it would be this:

EXTRA_CFLAGS=-fPIC -Wall -Wextra

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文