I'm compiling my C++ app using GCC 4.3. Instead of manually selecting the optimization flags I'm using -march=native, which in theory should add all optimization flags applicable to the hardware I'm compiling on. But how can I check which flags is it actually using?
6 Answers
You can use the -Q --help=target options:
gcc -march=native -Q --help=target ...
The -v option may also be of use.
You can see the documentation on the --help option here.
-
15I'm going to suggest that this is suboptimal. The output of --help=target doesn't display CPU cache information, of which the methods both elias and 42n4 below have listed. Specifically, on gcc 4.9.2 on a Phenom, the output includes these:
--param l1-cache-size=64 --param l1-cache-line-size=64 --param l2-cache-size=512Commented Jan 29, 2015 at 0:22 -
@DanielSantos: on my system it does display those parameters with the
-voption, albeit as part of thecc1command line...– thkalaCommented Jan 30, 2015 at 10:20 -
not perfect. on gcc version 5.4.0 (Buildroot 2017.05-rc2-00016-gc7eaf50-dirty) it will cause the error in return: Assembler messages: Error: unknown architecture native Error: unrecognized option -march=native. So, loose the
-march=nativeand it will work everywhere just following:gcc -Q --help=target. Commented Jun 6, 2017 at 14:00 -
1@Oleg - That sounds like a bug in GCC 5. The issue is not present in GCC 7.– jwwCommented Oct 27, 2017 at 19:50
To see command-line flags, use:
gcc -march=native -E -v - </dev/null 2>&1 | grep cc1
If you want to see the compiler/precompiler defines set by certain parameters, do this:
echo | gcc -dM -E - -march=native
-
5This answer deserves as many upvotes as the accepted one for, in particular, listing what
nativereally equates to. Commented Feb 4, 2017 at 18:02 -
7so if i'd like to cross-native-compile, i should feed both the compiler both the defines AND the arguments? or is the arguments sufficient? Commented Dec 21, 2017 at 2:18
-
It should be (-### is similar to -v):
echo | gcc -### -E - -march=native
To show the "real" native flags for gcc.
You can make them appear more "clearly" with a command:
gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )//g'
and you can get rid of flags with -mno-* with:
gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )|( -mno-[^\ ]+)//g'
If you want to find out how to set-up a non-native cross compile, I found this useful:
On the target machine,
% gcc -march=native -Q --help=target | grep march
-march= core-avx-i
Then use this on the build machine:
% gcc -march=core-avx-i ...
-
-
@BaptisteWicht are there flags that -march=native will include that -march=core-avx-i would not, in this case, or which flags? Thanks! Commented Jun 28, 2016 at 20:08
-
3@rogerdpack On this computer (sandybridge), march=sandybridge does not enable AVX (don't know why) while march=native does. Another important difference is that cache sizes are only extracted with march=native Commented Jun 29, 2016 at 7:03
-
2@BaptisteWicht that's odd seems to work here (I guess):
echo | gcc-6 -dM -E - -march=sandybridge | grep AVX #define __AVX__ 1but cache sizes do seem absent. Commented Jul 14, 2016 at 0:37
I'm going to throw my two cents into this question and suggest a slightly more verbose extension of elias's answer. As of gcc 4.6, running of gcc -march=native -v -E - < /dev/null emits an increasing amount of spam in the form of superfluous -mno-* flags. The following will strip these:
gcc -march=native -v -E - < /dev/null 2>&1 | grep cc1 | perl -pe 's/ -mno-\S+//g; s/^.* - //g;'
However, I have only verified the correctness of this on two different CPUs (an Intel Core2 and AMD Phenom), so I suggest also running the following script to be sure that all of these -mno-* flags can be safely stripped.
2021 EDIT: There are indeed machines where -march=native uses a particular -march value, but must disable some implied ISAs (Instruction Set Architecture) with -mno-*.
#!/bin/bash
gcc_cmd="gcc"
# Optionally supply path to gcc as first argument
if (($#)); then
gcc_cmd="$1"
fi
with_mno=$(
"${gcc_cmd}" -march=native -mtune=native -v -E - < /dev/null 2>&1 |
grep cc1 |
perl -pe 's/^.* - //g;'
)
without_mno=$(echo "${with_mno}" | perl -pe 's/ -mno-\S+//g;')
"${gcc_cmd}" ${with_mno} -dM -E - < /dev/null > /tmp/gcctest.a.$$
"${gcc_cmd}" ${without_mno} -dM -E - < /dev/null > /tmp/gcctest.b.$$
if diff -u /tmp/gcctest.{a,b}.$$; then
echo "Safe to strip -mno-* options."
else
echo
echo "WARNING! Some -mno-* options are needed!"
exit 1
fi
rm /tmp/gcctest.{a,b}.$$
I haven't found a difference between gcc -march=native -v -E - < /dev/null and gcc -march=native -### -E - < /dev/null other than some parameters being quoted -- and parameters that contain no special characters, so I'm not sure under what circumstances this makes any real difference.
Finally, note that --march=native was introduced in gcc 4.2, prior to which it is just an unrecognized argument.
-
-
gcc version 5.4.0 (Buildroot 2017.05-rc2-00016-gc7eaf50-dirty) returns: Error: unknown architecture `native' Commented Jun 6, 2017 at 13:35
-
Oleg: What arch are you using? It could be that "native" is only supported on some architectures. Commented Jun 7, 2017 at 14:19
There is resolve-march-native for exactly this: It parses GCC output and combines the output from multiple calls to GCC into the eventual answer. For example:
# resolve-march-native --vertical
-march=sandybridge
-maes
--param=l1-cache-line-size=64
--param=l1-cache-size=32
--param=l2-cache-size=3072
There is also distccflags but it seems discontinued.
PS: I am the author of resolve-march-native.