C语言 添加编译器选项而不编辑 Makefile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3602927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:21:03  来源:igfitidea点击:

Add compiler option without editing Makefile

cmakefile

提问by Gnufabio

I should compile a program written in C through a Makefile. I should insert into the Makefile, some option, for instance: -O2, -march=i686. How can I insert this option in the Makefile without writing into it?

我应该通过 Makefile 编译一个用 C 编写的程序。我应该在 Makefile 中插入一些选项,例如:-O2, -march=i686. 如何在不写入的情况下在 Makefile 中插入此选项?

回答by dirkgently

You should use a macro like CFLAGS. Check out GNU GCC documentation.

您应该使用像CFLAGS这样的宏。查看 GNU GCC文档

Something like this should work:

这样的事情应该工作:

CFLAGS := $(CFLAGS) -O2 -march=i686

Or, if you prefer not to modify the makefile use:

或者,如果您不想修改 makefile,请使用:

make CFLAGS='-O2 -march=i686' 

The other options will be picked up automatically though. See overriding variables.

其他选项将自动选择。请参阅覆盖变量