Obtaining current GCC architecture
Asked Answered
A

5

48

How can I find out what the -march default argument is for the current architecture if I don't supply any?

Armendariz answered 30/7, 2012 at 18:58 Comment(0)
S
85

gcc -dumpmachine gives you the target triplet, e.g. x86_64-unknown-linux-gnu

If gcc -v shows GCC was configured with a --with-arch option (or --with-arch-32 and/or --with-arch-64) then that's what will be the default.

Without a --with-arch option (and if there isn't a custom specs file in use) then the arch used will be the default for the target.

For x86, up to and including GCC 4.4, the default for 32-bit was -march=i386 and for 64-bit was -march=x86-64 (note the hyphen instead of underscore.)

For x86 with GCC 4.5 and later versions the default arch is inferred from the target triplet i.e. configuring for i586-pc-linux-gnu means the default is -march=i586, configuring for core2-pc-linux-gnu means the default is -march=core2.

Some other platforms also infer the default arch from the target triplet (and have done since before GCC 4.4) so e.g. ultrasparc2-sun-solaris2.10 implies -march=ultrasparc2.

Stringboard answered 31/7, 2012 at 11:56 Comment(8)
The gcc -dumpmachine options is perfect. Another example of how a two second search on stackoverflow avoids another read of the gcc man page!Sabrasabre
I have read the manual, and enjoyed it. What I meant was that you can search stackoverflow with more human search terms rather than going through the man page. For example, I have used gcc -dumpmachine before but couldn't remember the argument name. However I can't do a easy search in the man page for the correct argument sometimes. Maybe I should change "avoids another read of" to "avoids another search through" ;)Sabrasabre
Ah yes, fair enough, that's certainly easier than remembering the exact term to search for in the manual, because if you could remember it you wouldn't need to look it up!Stringboard
The only problem is that MinGW says just mingw32 and I don't see any option to find whether it means i386 or i486 or i686 or what (unlike cygwin gcc for mingw, which says i686-pc-mingw).Horrify
The specs file can also modify what particular tuning flags are used. So beware of that. Effectively the trick with echo | gcc -v -E - 2>&1 | grep cc1 shown in another answer will reveal what the specs file is using.Felodese
Doesn't the gcc -dumpmachine just give you the target by which your gcc was built for? I'm not sure it gives you the exact architecture of your machine.Excavate
@annoying_squid I never claimed it tells you that. It tells you the target GCC as built for, which can determine the default -march value, which is what the question is about. The question is not about the exact architecture of the machine, it's about the default settings for a particular build of GCC, which depends on the target GCC was built for, and other configure options ... as the answer explains.Stringboard
fyi there is also gcc -dumpversionDigestible
E
19

I found this gem on the GCC mailing list that prints the default -march and mtune parameters:

$ echo | gcc -v -E - 2>&1 | grep cc1
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.0/cc1 -E -quiet -v - -mtune=generic -march=x86-64

Basically, you are compiling an empty file from stdin and while doing so, you print the commands.

Expecting answered 25/6, 2014 at 20:0 Comment(1)
Makes sense to point out that -m32 and -m64 can be used on the gcc invocation to ask for either 32-bit or 64-bit (default) targets.Felodese
N
15

You can use gcc -Q --help=<type> to list the current option values of the given <type>. Thus:

$ gcc -Q --help=target | grep march
  -march=                           x86-64
$ gcc -m32 -Q --help=target | grep march
  -march=                           i686
$ i686-w64-mingw32-gcc -Q --help=target | grep march
  -march=                           pentiumpro

Edit: Actually, this option is not as generally useful as it appears, because target-specific defaults are not reflected in the output.

Ninette answered 2/1, 2015 at 23:25 Comment(1)
This is very helpful for GCC 6 and later, where it does print target-specific defaults.Punctuation
B
6

Running the command

gcc -v

will show something like this:

COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/src/gcc-4.7.1/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --enable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
Thread model: posix
gcc versie 4.7.1 (GCC) 

The Target: line is what you want. You should be able to deduce enough information from this.

Bearskin answered 30/7, 2012 at 20:14 Comment(2)
What part of "x86_64-unknown-linux-gnu" can you use with -march?Cellarage
The first part: x86_64. But that is a general one. A full list can be found int the GCC manual: gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/….Bearskin
C
3

I think that the answer is there isn't any equivalent. Either you don't specify -march= and the compiler uses the architecture's minimal instruction set, or you do specify -march= and it uses features of whichever cpu model you ask for. So there is no way to write a -march= option that is equivalent to omitting the -march option.

Maybe if you explain what motivated the question we could improve on this answer.

Cellarage answered 30/7, 2012 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.