简体   繁体   English

如何在不编辑 Makefile 的情况下将 append 转换为 Makefile 中的 CXXFLAGS?

[英]How to append to CXXFLAGS in Makefile without editing Makefile?

I'm writing a build script for a Qt application.我正在为 Qt 应用程序编写构建脚本。 The script first calls qmake on the.pro file and then runs make CXXFLAGS=-DSWVERSION=xxxx .该脚本首先在 .pro 文件上调用qmake ,然后运行make CXXFLAGS=-DSWVERSION=xxxx

The problem with this is that it overwrites the CXXFLAGS already defined in the Makefile. Currently, the only method I know to solve this problem is to edit the Makefile and change this:这个问题是它覆盖了 Makefile 中已经定义的 CXXFLAGS。目前,我知道解决这个问题的唯一方法是编辑 Makefile 并更改它:

CXXFLAGS      = <flags>

To this:对此:

CXXFLAGS      += <flags>

I was hoping there would be a simpler solution.我希望有一个更简单的解决方案。 Is there any way I can simply append to CXXFLAGS from the command line WITHOUT rewriting the Makefile?有什么办法可以从命令行简单地将 append 转换为 CXXFLAGS 而无需重写 Makefile?

Alternatively, my real problem is defining SWVERSION at build-time (because it is dependent on the build timestamp).或者,我真正的问题是在构建时定义SWVERSION (因为它取决于构建时间戳)。 Do you know of a better way to define this at runtime instead of passing it to CXXFLAGS?您知道在运行时定义它而不是将其传递给 CXXFLAGS 的更好方法吗?

The change you suggest won't work anyway: a value set on the command line completely overrides the value set in the makefile, even if the value is appended to with += .您建议的更改无论如何都行不通:命令行上设置的值完全覆盖 makefile 中设置的值,即使该值附加了+=也是如此。 You could do this and it would work:你可以这样做并且它会起作用:

override CXXFLAGS += <flags>

However, the way it's normally done is that the makefile provides variables which are reserved specifically for users to override on the command line.但是,通常的做法是 makefile 提供专门保留的变量,供用户在命令行上覆盖。 For example in automake-generated makefiles, the common variables like CXXFLAGS , CFLAGS , etc. are reserved for users to set via the command line or environment, and the internal makefile flags are put into a different variable, like AM_CFLAGS .例如在 automake 生成的 makefile 中,诸如CXXFLAGSCFLAGS等公共变量保留供用户通过命令行或环境设置,而内部 makefile 标志被放入不同的变量中,如AM_CFLAGS Then the recipes to compile objects uses both variable, like $(CC) $(AM_CFLAGS) $(CFLAGS)...然后编译对象的方法使用两个变量,比如$(CC) $(AM_CFLAGS) $(CFLAGS)...

I don't know anything about qt so I can't say whether they have a similar set of reserved makefile variables but you might check the qt docs, or your generated makefile recipes, to see.我对 qt 一无所知,所以我不能说他们是否有一组类似的保留 makefile 变量,但您可以查看 qt 文档,或您生成的 makefile 食谱,看看。

For anyone reading this in the future, the solution I found was to call qmake <project file> DEFINES+="SWVERSION=xxxx" .对于以后阅读本文的任何人,我找到的解决方案是调用qmake <project file> DEFINES+="SWVERSION=xxxx" Hope someone finds this helpful.希望有人觉得这有帮助。

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

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