QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12462|回复: 10

RPM 建包 (GCC) 优化选项

[复制链接]
发表于 2004-12-12 20:15:33 | 显示全部楼层 |阅读模式
gcc 的优化选项请看这里:
http://www.magiclinux.org/people/kde/web/gcc.html#lbAO


不同场合使用不同优化参数。但是任何优化都会使编译速度减慢,而且对性能的提升是有限的。推荐下面组合:
-O2 -g -pipe -fprefetch-loop-arrays

-O2 是最安全的优化参数(集合),推荐使用。

-Os 去掉了 -O2 中那些使体积增大的选项,故而体积最小,适合嵌入式程序。

-O3 使用 -O3 有时反倒使生成的代码执行速度减慢,而且编译速度奇慢。任何大于 -O3 的选项都是愚蠢的,因为那极有可能反而使代码执行速度减慢,甚至不稳定、不能启动。

-g 是使生成的 debuginfo 包为 gdb 调试程序提供额外支持。

-pipe 使得编译过程使用管道代替临时文件,从而加快编译过程。

-fprefetch-loop-arrays 对于使用巨大数组的程序可以加快代码执行速度,其他情况不推荐使用。适合数据库相关的大型软件、多媒体软件、数学软件等。该参数不包含于 -O1、-O2、-O3。

**使用其他优化参数首先应查询它是否已经包含进了 -O1、-O2、-O3,如果已经包含进了你使用的优化级别,再指定就是多余的。
**精确数值计算的软件绝对不要使用任何 -o 选项,而应该参考 GCC 说明中提到的数值计算优化选项,否则,不但降低速度,还会导致错误。有人曾经提到计算 PI 值的程序在不使用任何 -O 选项的时候最快。
------------------------------------------------------------------
对于 rpm 建包,可以修改这里的宏定义实现上述优化参数:
/usr/lib/rpm/macros

修改如下:
%optflags -O2 -g -pipe -fprefetch-loop-arrays

%_arch i686
#这里相当于 rpmbuild 的参数 --target=i686 指将来运行软件包时的环境,千万不要高于 i686,否则你的软件包就不能跨 intel 和 amd 平台使用了。
%_build_arch i686
#这里相当于 rpmbuild 的参数 --build=i686 指建包时的环境(你的机器),这可以比默认的 i386 快一些。 根据你的实际情况,可以使用 pentium2, pentium3, pentium4, athlon, athlon-tbird, athlon-4, athlon-xp, athlon-mp 等。

如果上面两处对 cpu 架构的指定无效(事实上在 rpm 的系统配置文件里,cpu 架构往往是由其他参数指定的),编出的还是 i386 包,你可以尝试这样修改(以 pentium3 为例):
%optflags -O2 -pipe  -mcpu=pentium3 -march=pentium3 -mtune=pentium3
这样优化出来的包不管名字是什么样的,其实质乃是针对 pentium3 优化过的!。

注意:
-fprefetch-loop-arrays 这里不是必需的,但它至少不会减慢代码执行速度。
-funroll-loops 会对已知次数的循环进行解环、展开,这可能加快代码执行速度,也可能减慢代码执行速度,所以是一个不可靠的优化参数。而且无论如何都会使生成的代码体积加大,有时非常庞大,故不推荐使用。
-fexpensive-optimizations 这个参数已经包含于 -O2,使用 -O2 的同时再指定这个参数就是多余的。
 楼主| 发表于 2004-12-12 20:22:26 | 显示全部楼层
Intel 386 and AMD x86-64 Options

These -m options are defined for the i386 and x86-64 family of computers:

-mcpu=cpu-type
Tune to cpu-type everything applicable about the generated code, except for the ABI and the set of available instructions. The choices for cpu-type are i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-4, athlon-xp, athlon-mp, winchip-c6, winchip2 and c3.
While picking a specific cpu-type will schedule things appropriately for that particular chip, the compiler will not generate any code that does not run on the i386 without the -march=cpu-type option being used. i586 is equivalent to pentium and i686 is equivalent to pentiumpro. k6 and athlon are the AMD chips as opposed to the Intel ones.

-march=cpu-type
Generate instructions for the machine type cpu-type. The choices for cpu-type are the same as for -mcpu. Moreover, specifying -march=cpu-type implies -mcpu=cpu-type.
回复

使用道具 举报

发表于 2005-1-16 10:06:29 | 显示全部楼层
请教一下,我如何在编译时禁止 “%optflags -O2 -g -pipe -fprefetch-loop-arrays ” 呢?

另外,在编译 gcc 时,是否要选择优化参数呢?
回复

使用道具 举报

发表于 2005-1-16 13:26:47 | 显示全部楼层
突然想到的问题:  Monkey's Audio Codes 解码器再编译的时候是不是最好不加上 -O选项?
回复

使用道具 举报

发表于 2005-1-16 13:33:26 | 显示全部楼层
最好不加
回复

使用道具 举报

 楼主| 发表于 2005-1-17 22:06:49 | 显示全部楼层
[quote:2601ea393e="bamfox"]请教一下,我如何在编译时禁止 “%optflags -O2 -g -pipe -fprefetch-loop-arrays ” 呢?
[/quote]
在 /usr/lib/rpm/macros 里 %optflags 行前面加上注释符 #
# %optflags -O2 -g -pipe -fprefetch-loop-arrays
回复

使用道具 举报

发表于 2005-1-17 22:31:56 | 显示全部楼层
我想知道,如何在 rpmbuild 时,以参数的形式(不用注释掉这种)禁用优化选项
回复

使用道具 举报

发表于 2005-1-17 22:32:14 | 显示全部楼层
这是我收集的,一起分享。 ^_^

1. gcc常用的使用方法
http://www.linuxfans.org/nuke/modules.php?name=Forums&file=viewtopic&t=54070

2. UNIX系统开发-gcc参数详解
http://www.linuxfans.org/nuke/modules.php?name=Forums&file=viewtopic&t=75367

3. 经验共享:在Redhat Linux上安装 GCC 编译器过程
http://www.linuxfans.org/nuke/modules.php?name=Forums&file=viewtopic&t=80824

4. GCC 中文手册——zt
http://www.linuxfans.org/nuke/modules.php?name=Forums&file=viewtopic&t=97290
回复

使用道具 举报

发表于 2005-7-19 16:38:05 | 显示全部楼层

Re: RPM 建包 (GCC) 优化选项

[quote:d1b77f4214="KDE"]%_arch i686
#这里相当于 rpmbuild 的参数 --target=i686 指将来运行软件包时的环境,千万不要高于 i686,否则你的软件包就不能跨 intel 和 amd 平台使用了。
%_build_arch i686
#这里相当于 rpmbuild 的参数 --build=i686 指建包时的环境(你的机器),这可以比默认的 i386 快一些。根据你的实际情况,可以使用 pentium2, pentium3, pentium4, athlon, athlon-tbird, athlon-4, athlon-xp, athlon-mp 等。 [/quote]

请教一下,我在magic 1.2上修改了macros,但是制作的rpm包还是i386的,最后是指定了--target=i686才成功的。而且,好像rpmbuild没有--build选项?
回复

使用道具 举报

 楼主| 发表于 2005-7-19 19:13:15 | 显示全部楼层
请教一下,我在magic 1.2上修改了macros,但是制作的rpm包还是i386的,最后是指定了--target=i686才成功的。而且,好像rpmbuild没有--build选项?

的确,新一点的 rpm 系统的确不再支持 --build 选项,默认使用测试到的真实机器。
回复

使用道具 举报

发表于 2009-9-14 12:41:10 | 显示全部楼层
-mcpu= 参数似乎 gcc 4.4 开始已经被 mtune 替代了?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 02:38 , Processed in 0.094841 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表