When you're optimizing code for performance, especially in C or C++, using compiler options like -march=native
can significantly boost speed by enabling all the SIMD instruction sets and CPU-specific optimizations available on the machine. This option tells the compiler to auto-detect the processor's architecture and use every feature it supports - like AVX2, and other instruction-level enhancements. However, this automation can feel like a black box. This tutorial shows how to check gcc or g++ compiler options enabled by -march=native
.
Here's how we can find out which options -march=native
expands into:
gcc -march=native -v -E - < /dev/null 2>&1 | grep cc1
g++ -march=native -v -E -x c++ - < /dev/null 2>&1 | grep cc1plus
-v
- verbose output (shows the internal commands).-E
- run only the preprocessor stage (no compilation or linking).-
- the hyphen here tells the compiler to read from standard input (stdin) instead of a file, and it works in tandem with the following part of the command.< /dev/null
- this redirects an empty input stream to the compiler, so it processes no actual code - just a blank input.2>&1
- this redirects standard error (stderr) to standard output (stdout). By doing this, both error messages and regular output are combined into one stream.| grep cc1
and| grep cc1plus
- filters out lines containingcc1
orcc1plus
.-x c++
- this tells the compiler to treat the input as C++ source code, even though we're not providing an actual file. This is used because we're redirecting an empty stream, and the option ensures that the compiler knows it's dealing with C++ code.
Leave a Comment
Cancel reply