GCC/G++: How to find -march=... setting for the current platform

Method 1: Explicitly find the -march setting

To determine what -march=... setting is appropriate for your current platform when using GCC or G++, you can use the following command:

g++ -march=native -Q --help=target | grep march= | head -n1 | cut -f3-

Example output

znver3

In this example, the output indicates that the architecture is znver3, which corresponds to AMD’s Zen 3 architecture.

Method 2: Use -march=native

If you are compiling on the same machine type where the code will run, you can use the -march=native option. This tells the compiler to automatically detect the architecture of the host machine and optimize the code accordingly.

Example command:

g++ -march=native -o my_program my_program.cpp