Search code examples
gccg++command-line-argumentspreprocessor-directive

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


I use GCC for C and G++ for C++. I have trouble supplying each cmdline options everywhere.

Say I can easily replace -DHAVE_CONFIG_H with

#define HAVE_CONFIG_H

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

I prefer at least -O2 -std=xxx -L -I.

Edit: I'm having this problem because when I upload my code to an online judger, it would always compile it plainly (gcc xxx.c -o xxx -lm) and won't turn on any optimization flags for me. I can't modify their command line so I want it in my source code.


Solution

  • I have trouble supplying each cmdline options everywhere.

    Why? Are you using some 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 (probably something like adding a line such as CXXFLAGS += -DHAVE_CONFIG_H -O2 -std=c++11)

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

    You mean replaceable by preprocessor directives. Read documentation of cpp

    Actually, most compiler options are not replaceable by preprocessor directives; notably

    • -std=

    • -L and -l and any other options for the linker

    • -I and similar options for include directories

    However, -O2 and some other optimization flags are settable by function attributes e.g. __attribute__((optimize("O2")) (see this) and by function specific option pragmas e.g. #pragma GCC optimize "-O2". Notice that this is specific to GCC.

    I recommend spending a few hours reading the documentation of GCC.

    You might also perhaps change your spec file, but I don't recommend doing that.