是否可以使用哈希行而不是传递给 GCC 的每个命令行参数?

Is it possible to use hash lines instead of every single cmdline argument passed to GCC?

我对 C 使用 GCC,对 C++ 使用 G++。我无法在任何地方提供每个 cmdline 选项。

假设我可以轻松地将 -DHAVE_CONFIG_H 替换为

#define HAVE_CONFIG_H

是否所有 cmdline 参数都可以用 # 行替换?如果不是,哪些参数 可替换的?

我至少更喜欢 -O2 -std=xxx -L -I.

编辑:我遇到这个问题是因为当我将我的代码上传到在线判断器时,它总是会简单地编译它(gcc xxx.c -o xxx -lm)并且不会'不要为我打开任何优化标志。我无法修改他们的命令行,所以我想在我的源代码中使用它。

I have trouble supplying each cmdline options everywhere.

为什么?您是否正在使用一些 build automation tool like GNU make? I believe you should use it, then it is just a matter to add a few appropriate lines in your Makefile(可能是添加诸如 CXXFLAGS += -DHAVE_CONFIG_H -O2 -std=c++11 之类的行)

Are all cmdline arguments replaceable by # lines? If not, what args are replaceable?

你的意思是可以被预处理器指令替换。阅读 documentation of cpp

实际上,大多数编译器选项不能由预处理器指令替换;特别是

  • -std=

  • -L-l 以及 linker

  • 的任何其他选项
  • -I 和包含目录的类似选项

然而,-O2一些其他优化标志可由function attributes e.g. __attribute__((optimize("O2")) (see this) and by function specific option pragmas设置,例如 #pragma GCC optimize "-O2"。请注意,这是特定于 GCC.

我建议花几个小时阅读 documentation of GCC

您也可以更改 spec file,但我不建议您这样做。