+ -
当前位置:首页 → 问答吧 → 转一篇文章 Safe flags to use for Gentoo比较详细

转一篇文章 Safe flags to use for Gentoo比较详细

时间:2005-08-10

来源:互联网

Safe flags to use for Gentoo

For a guide to setting your Cflags, See here CFLAGS

Since Freehackers.org's CFLAGS guide is down, we mirror it here.

Those flags are for gcc-3.x and alike (mostly gcc-3.0, gcc-3.1.1 and gcc-3.2), you may check which gcc you're using with the following command :

How to know your gcc version

# gcc --version

You can know which CPU you have with the following command : How to know your CPU type

# cat /proc/cpuinfo

This page is for those who don't want to experiment, want a stable system, but still optimized for their processor. Be careful, by using those flags, binaries from your system might not work on another one. So if you compiled with those flags optimized for p4, you can't send a tbz2 or share a hard disk with a friend who only has a pentium MMX.
[edit]
i386 (Intel)

do you really want to install gentoo on that ?

CHOST="i386-pc-linux-gnu"
CFLAGS="-march=i386 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=i386 -O3 -pipe -fomit-frame-pointer"

-O3 is actually more harmful than -O2 because -O3 will bloat the binaries, so it should be:

CHOST="i386-pc-linux-gnu"
CFLAGS="-march=i386 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=i386 -O2 -pipe -fomit-frame-pointer"

[edit]
i486 (Intel)

do you really want to install gentoo on that ?

CHOST="i486-pc-linux-gnu"
CFLAGS="-march=i486 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=i486 -O2 -pipe -fomit-frame-pointer"

[edit]
Pentium 1 (Intel)

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=pentium -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium -O2 -pipe -fomit-frame-pointer"

[edit]
Pentium MMX (Intel)

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=pentium-mmx -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium-mmx -O2 -pipe -fomit-frame-pointer"

[edit]
Pentium PRO (Intel)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentiumpro -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentiumpro -O2 -pipe -fomit-frame-pointer"


[edit]
Pentium II (Klamath), (Intel)

vendor_id : GenuineIntel
cpu family : 6
model : 3
model name : Pentium II (Klamath)
stepping : 3

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium2 -Os -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium2 -Os -pipe -fomit-frame-pointer"

[edit]
Pentium II (Deschutes), (Intel)

vendor_id : GenuineIntel
cpu family : 6
model : 5
model name : Pentium II (Deschutes)
stepping : 2

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium2 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium2 -O2 -pipe -fomit-frame-pointer"

[edit]
Celeron (Mendocino), aka Celeron1 (Intel)

vendor_id : GenuineIntel
cpu family : 6
model : 6
model name : Celeron (Mendocino)
stepping : 0

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium2 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium2 -O2 -pipe -fomit-frame-pointer"

[edit]
Pentium III (Intel)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium3 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium3 -O2 -pipe -fomit-frame-pointer"

[edit]
Celeron (Coppermine) aka Celeron2 (Intel)

vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium3 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium3 -O3 -pipe -fomit-frame-pointer"

[edit]
Celeron (Willamette) (Intel)

vendor_id : GenuineIntel cpu family : 15 model : 1 model name : Intel(R) Celeron(R) CPU 1.70GHz stepping : 3

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer"

[edit]
Pentium-M / Centrino (Intel)

vendor_id : GenuineIntel
cpu family : 6
model : 9
model name : Intel(R) Pentium(R) M processor XXXXMHz

For gcc < 3.4.3:

CHOST="i686-pc-linux-gnu"
CFLAGS="-O2 -march=pentium3 -pipe -mfpmath=sse"
CXXFLAGS="${CFLAGS}"

Comment: I also have working system but no benchmark in speed difference to the above with:

CHOST="i686-pc-linux-gnu"
CFLAGS="-O2 -march=pentium4 -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"

For gcc >= 3.4.3:

CHOST="i686-pc-linux-gnu"
CFLAGS="-O3 -march=pentium-m -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"

Indeed it seems more a question of belief than of real speed if you use optimization level O2 or O3. O3 works well for me, but people may prefer to stick with O2. Options concerning SSE and such things sometimes may cause very long compile times until errors occur.
[edit]
Pentium 4 (Intel)

vendor_id : GenuineIntel
cpu family : 15
model : 0 or 2
model name : Intel(R) Pentium(R) 4 CPU XXXXMHz

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer"

my system (running 24/7 as private mail/file server):

vendor_id : GenuineIntel
cpu family : 15
model : 1
model name : Intel(R) Pentium(R) 4 CPU 1.70GHz

CHOST="i686-pc-linux-gnu"
CFLAGS="-O2 -march=pentium4 -ffast-math -pipe -fomit-frame-pointer -mfpmath=sse,387 -msse2 -mmmx"
CXXFLAGS="${CFLAGS}"
LDFLAGS="-Wl,-O1"

[edit]
Xeon w/EM64T (Intel)

vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(R) CPU XXXXMHz

CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=nocona -O3 -fomit-frame-pointer -fforce-addr -ftracer -mmmx -msse3 -mfpmath=sse -pipe"
CXXFLAGS="${CFLAGS}"

You should be using gcc 3.4.x or higher.

If you have hyperthreading turned on, then each processor counts as two logical CPUs, so if you have two CPUs, then you should use MAKEOPTS="-j5" in your make.conf.

This is the 1st revision for this processor. Please add to it.
[edit]
Nehemiah (Via)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=i686 -msse -mmmx -mfpmath=sse -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=i686 -msse -mmmx -mfpmath=sse -O2 -pipe -fomit-frame-pointer"

note: it is also possible to use "-march=c3-2".

note: I had much better luck with -Os than with -O2. The cache on the nehemiah chips is really small, so making the executables small helps more than anything else.
[edit]
Eden C3/Ezra (Via)

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=i586 -m3dnow -Os -pipe -fomit-frame-pointer"
CXXFLAGS="-march=i586 -m3dnow -Os -pipe -fomit-frame-pointer"

quote : The ezra doesn't have any special instructions that you could optimize for, just consider it a K6-3...basically a P2 with 3DNow

Note: it is also possible to use "-march=c3".

Due to the small 64kb cache on the Via C3 CPU, '-Os' works better since it makes smaller binaries.
[edit]
Transmeta Efficeon

vendor_id : GenuineTMx86
cpu family : 15
model : 2
model name : Transmeta Efficeon(tm) Processor TM8000
stepping : 4

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=i686 -mtune=pentium3 -msse2 -O3 -mfpmath=sse -falign-functions=0 -falign-jumps=0 -falign-loops=0 -pipe"
CXXFLAGS="${CFLAGS}"

Note: The Efficeon processor will reorder and realign instructions on translating form x86 to VLIW (Very Large Instruction Word), so not aligning functions/jumps/loops will produce smaller executable without any effect on speed.
[edit]
K6 (AMD)

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=k6 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=k6 -O3 -pipe -fomit-frame-pointer"

[edit]
K6-2 (AMD)

vendor_id : AuthenticAMD cpu family : 5 model : 8

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=k6-2 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=k6-2 -O2 -pipe -fomit-frame-pointer"

[edit]
K6-3 (AMD)

CHOST="i586-pc-linux-gnu"
CFLAGS="-march=k6-3 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=k6-3 -O2 -pipe -fomit-frame-pointer"

[edit]
Duron (AMD)

From 600 to 900 MHz (these models have a tbird-alike core model)

product: AMD Duron(tm) Processor version: 6.3.1

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"

[edit]
Mobile Duron (AMD)

vendor_id : AuthenticAMD
cpu family : 6
model : 3
model name : AMD Duron(tm) Processor

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"

[edit]
Duron Morgan (AMD)

vendor_id : AuthenticAMD
cpu family : 6
model : 7
model name : AMD Duron(tm) Processor
stepping : 1

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer"

[edit]
Athlon (AMD)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon -O3 -pipe -fomit-frame-pointer"

[edit]
Athlon-tbird, aka K7 (AMD)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-tbird -O3 -pipe -fomit-frame-pointer"

[edit]
Athlon-tbird XP (AMD)

vendor_id : AuthenticAMD
cpu family : 6
model : 8
model name : AMD Athlon(tm)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer"

[edit]
Athlon 4(AMD)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-4 -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-4 -O3 -pipe -fomit-frame-pointer"

[edit]
Athlon XP (AMD)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer -m3dnow -msse -mmmx"
CXXFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer -m3dnow -msse -mmmx"

In case you didn't see the comment written under the Intel proc up top, the person who left it is right; -O3 often slows down a system more than it speeds it up if the user has low memory. Just to be on the "safe" side (owing to the title of this article) I would go with the -O2 optimization. :-)
[edit]
Athlon MP (AMD)

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=athlon-mp -O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-march=athlon-mp -O3 -pipe -fomit-frame-pointer"

[edit]
Opteron/Athlon64 (AMD)

CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=k8 -O2 -pipe"
CXXFLAGS="-march=k8 -O2 -pipe"

[edit]
603 (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fsigned-char"
CXXFLAGS="-O3 -pipe -fsigned-char"

[edit]
603e (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fsigned-char"
CXXFLAGS="-O3 -pipe -fsigned-char"

[edit]
604 (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fsigned-char"
CXXFLAGS="-O3 -pipe -fsigned-char"


[edit]
PowerPC 604e

Got 3 of these machines running (as router,ldap server, kernel builder)

cat /proc/cpuinfo
processor : 0
cpu : 604e
clock : 199MHz
revision : 2.2 (pvr 0009 0202)
bogomips : 397.31
machine : PReP IBM 43P-140 (Tiger1)
upgrade cpu : not present
scsi fuse : ok
simms : 0:128MiB 1:128MiB
l2 cache : sync burst, parity, 1MiB

cc -v
[...]
gcc version 3.4.1 20040803 (Gentoo Linux 3.4.1-r3, ssp-3.4-2, pie-8.7.6.5)

CFLAGS="-O3 -mcpu=604e -mtune=604e -mstring -mmultiple -mbig-endian -mpowerpc-gpopt -funroll-loops -fomit-frame-pointer -pipe
CHOST="powerpc-unknown-linux-gnu"
CXXFLAGS="${CFLAGS}"

works ok

[edit]
604e (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fno-strict-aliasing"
CXXFLAGS="-O3 -pipe -fno-strict-aliasing"

[edit]
750 aka G3 (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=750 -O3 -pipe -fno-strict-aliasing -mpowerpc-gfxopt"
CXXFLAGS="-mcpu=750 -O3 -pipe -fno-strict-aliasing -mpowerpc-gfxopt"

Note: do not use -march=
[edit]
7400, aka G4 (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=7400 -O2 -pipe -fsigned-char -maltivec -mabi=altivec -mpowerpc-gfxopt"
CXXFLAGS="-mcpu=7400 -O2 -pipe -fsigned-char -maltivec -mabi=altivec -mpowerpc-gfxopt"

Note: do not use -march=
Note: -O3 is unstable on G4
[edit]
7450, aka G4 second generation (PowerPC)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=7450 -O2 -pipe -fno-strict-aliasing -maltivec -mabi=altivec -mpowerpc-gfxopt"
CXXFLAGS="-mcpu=7450 -O2 -pipe -fno-strict-aliasing -maltivec -mabi=altivec -mpowerpc-gfxopt"

Note: do not use -fsigned-char
Note: -O3 is unstable on G4
Note: -mpowerpc-gfxopt may slow down certain applications
[edit]
970, aka G5 (PowerPC)

CHOST="powerpc64-unknown-linux-gnu"
CFLAGS="-O2 -mcpu=G5 -maltivec -mabi=altivec -fno-strict-aliasing -pipe"
CXXFLAGS="-O2 -mcpu=G5 -maltivec -mabi=altivec -fno-strict-aliasing -pipe"
LDFLAGS="-Wl,-O1"

[edit]
PowerPC (If you don't know which one)

CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-O2 -pipe -fno-strict-aliasing"
CXXFLAGS="-O2 -pipe -fno-strict-aliasing"

[edit]
Sparc

CHOST="sparc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-O3 -pipe -fomit-frame-pointer"

[edit]
Sparc 64

CHOST="sparc-unknown-linux-gnu"
CFLAGS="-O3 -pipe -fomit-frame-pointer"
CXXFLAGS="-O3 -pipe -fomit-frame-pointer"

[edit]
HPPA 1.1

CHOST="hppa1.1-unknown-linux-gnu"
CFLAGS="-O3 -pipe -mschedule=7100LC -march=1.1 -fomit-frame-pointer"
CXXFLAGS="-O2 -pipe -mschedule=7100LC -march=1.1 -fomit-frame-pointer"

[edit]
HPPA 2.0

CHOST="hppa2.0-unknown-linux-gnu"
CFLAGS="-O3 -pipe -mschedule=8000 -fomit-frame-pointer"
CXXFLAGS="-O2 -pipe -fomit-frame-pointer"

What is a CFLAG?

A CFLAG is a C compiler flag, usually associated with a GCC (GNU Compiler Collection) option. As well as being used to specify normal arguments like file output, CFLAGs can be used to customize and optimize applications when you build them from source. This is an important feature when using Gentoo Linux because the majority of packages are built from source code.

CFLAGs are commonly used to specify the architecture of your computer, as well as the CPU you are using and any other special options you would like to enable or disable. This information is important to GCC because it tells it exactly how to customize the assembly instructions it creates from the application's source code. If you're the impatient type and wish to get started immediately, you can check out the Safe CFLAGs Guide.
[edit]
Selecting the best cflags for your system

Technically GCC could run without any CFLAGs, since they are extra options, but the whole premise behind Gentoo is to custom compile packages for your particular system, which almost always gives you somewhat improved speed.
[edit]
Basic (safe) optimizations

There are several basic optimizations considered safe for general use. The first is -O (letter, not number)
[edit]
-O

Will enable basic optimizations. These do not greatly impact compile speed, and will greatly increase the speed of your system, but not as much as other CFLAGS.
[edit]
-O2

Is the next level of optimization. It turns on all optimizations that don't largely increase the size of the binary or interfere with debugging. -O2 is even better than -O, and just as safe. If you want a fast system that "just works", -O2 is probably good for you.
[edit]
-O3

Will enable some more optimizations. However, these should not be used on a system with low memory or drive space. The end binary output will probably be larger, and may not be any faster. In fact, it may slow down the program since larger binaries takes longer to load from the hard drive into the memory and the hard drive is a bottleneck even on fast computers. It will also cause larger memory usage and possibly cache misses on CPUs with a low amount of cache (celeron for ex.).
[edit]
-Os

This is exactly like -O2, except that it disables some flags that cause increased binary size and also tunes some optimizations for size rather than speed. This will probably decrease load times for large applications such as Mozilla, and will result in a slightly smaller amount of RAM usage during the program's runtime and faster startup. Of course, disk size of the binary is reduced. If you have an old system, or one strapped for space in either RAM or disk, choose this flag.

Note that only one of the above flags may be chosen. If you choose more than one, the last one specified will have the effect.
[edit]
-fomit-frame-pointer

This flag is very good if you are concerned mainly with execution times. However, binary size may increase slightly on x86.
[edit]
-march=<your_arch>

The next flag you want to consider using, '-march' requires you to know what your system's architecture is. Basically, you just need to know what your CPU is, and what the exact GCC name for it is. This flag takes the following form:

-march=pentium4
Of course you want to replace pentium4 with whatever CPU you're actually using.

Here's an _incomplete_ list of valid x86 architectures you may use with the march flag:

i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium-m, pentium4, k6, k6-2, k6-3, k8, athlon, athlon-tbird, athlon-4, athlon-xp, athlon-mp, winchip-c6, winchip2, c3

Note: If you have problems compiling some applications, try changing the '-march' to a '-mcpu'. -mcpu also supports a wider range of architectures, so if you're using a non-x86 system, you may want to take a look at it.

Also: if for any reason you decide to specify both -march and -mcpu/-mtune at once, be sure to specify -march last. Just as with the -O options, the last one specified will have the effect.

GCC documentation about -march and -mcpu/-mtune (http://gcc.gnu.org/onlinedocs/gcc/i3...4-Options.html)
[edit]
-pipe

The final and most common cflag is the '-pipe' option. The pipe option tells GCC not to create temporary files when compiling, and instead pipes data directly to the next function. Be aware that using -pipe flag can fill your whole ram and render your system unusable during compilation (although it can save you some compilation time). Enable at your own risk if you have an exceedingly small amount of ram (Works fine at 24 megabytes).
[edit]
What about all the other flags?

GCC has well over a hundred individual optimization flags and it would be insane to try and describe them all. Most of the important stuff however is covered when you utilize -Os. If you're feeling really ambitious, you can go look up every single GCC 3.4 optimization option here (http://gcc.gnu.org/onlinedocs/gcc-3....ze%20Options/).

There is a grossly incomplete CFLAGS matrix covering people's experiences with some of the less tested CFLAGS.

I'll probably add more to this document at a later time. Have fun!

作者: fei   发布时间: 2005-08-10

这种帖子已经看过N多了,现在的感觉是-------------------------------->>>稳定远比速度重要

作者: meteoren   发布时间: 2005-08-10

强烈建议Morgan Duron的CFLAGS使用-O2,要不慢死你……

作者: Zer4tul   发布时间: 2005-08-11

常用。顶先。自己找。

作者: fei   发布时间: 2005-10-26

-O, -O2, -O3, -Os
all implies
-fomit-frame-pointer

check 'man gcc'

作者: zhllg   发布时间: 2005-10-26

good!有空翻译一把。

作者: EricNeon   发布时间: 2005-10-26

没必要翻译吧,这个31。都是很简单的句子和词汇啊。

作者: Zer4tul   发布时间: 2005-10-27