Chinaunix

标题: 请教:如何去掉make给的gcc 加上的-O2 选项 [打印本页]

作者: ljoolj    时间: 2006-03-24 11:26
标题: 请教:如何去掉make给的gcc 加上的-O2 选项
如何去掉make给的gcc 加上的-O2 选项 ,这个问法有点菜哈,
不过我真的不太懂Makefile语法哈
我要gdb程序不希望有-O2 优化,我也不是自己写的gcc命令行
而是执行的别人写的Makefile,但在Makefile中没有对-O2直接定义什么,怎么去掉呢?
各位高手理解我的意思了吧,给点建议吧。谢了先!
作者: congli    时间: 2006-03-24 11:30
/etc/make.conf有什么内容?
作者: ljoolj    时间: 2006-03-24 11:38
cat /etc/make.conf
cat: /etc/make.conf: No such file or directory

我的系统是netBsd里面没发现/etc/make.conf
但在/usr/share/mk/sys.mk中有点O2的踪迹但我看不太懂不知道这么修改。
CC?=            cc
.if ${MACHINE_ARCH} == "alpha" || \
    ${MACHINE_ARCH} == "arm" || \
    ${MACHINE_ARCH} == "x86_64" || \
    ${MACHINE_ARCH} == "armeb" || \
    ${MACHINE_ARCH} == "hppa" || \
    ${MACHINE_ARCH} == "i386" || \
    ${MACHINE_ARCH} == "m68k" || \
    ${MACHINE_ARCH} == "mipsel" || ${MACHINE_ARCH} == "mipseb" || \
    ${MACHINE_ARCH} == "ns32k" || \
    ${MACHINE_ARCH} == "powerpc" || \
    ${MACHINE_ARCH} == "sh5el" || ${MACHINE_ARCH} == "sh5eb" || \
    ${MACHINE_ARCH} == "sparc" || \
    ${MACHINE_ARCH} == "sparc64" || \
    ${MACHINE_ARCH} == "vax"
DBG?=   -O2
.else
DBG?=   -O
.endif
作者: congli    时间: 2006-03-24 11:41
倒,netbsd应该是/etc/mk.conf
作者: ljoolj    时间: 2006-03-24 11:44
cat /etc/mk.conf
cat: /etc/mk.conf: No such file or directory
也没有
作者: gvim    时间: 2006-03-24 12:05
自己建立一个
man mk.conf
参考 COPTS
作者: ljoolj    时间: 2006-03-24 12:26
我立了一个
/etc/mk.conf

COPTS+=-g3

cc 中-g3的选项是加上了
但-O2选项还是没有去掉啊.
作者: gvim    时间: 2006-03-24 12:36
你要搞开发吗?这个,我个人的建议是不妨多学点基础的,估计你现在的水平硬着头皮上会很困难。
恩,话很难听,听不听在你了

COPTS+=-O0 关闭任何优化
作者: ljoolj    时间: 2006-03-24 13:01
恩,主要是有人带,问公司里面的人有些费劲,就问问大家了。
作者: ljoolj    时间: 2006-03-24 14:04
明白了,gcc 里面 -O2 -O0 可以同时存在
最后一个有效是这个意思吧。
那可以直接在Makefile里面改,不用在/etc/mk.conf改,这样改影响面太大了。
作者: 雨丝风片    时间: 2006-03-24 15:16
原帖由 ljoolj 于 2006-3-24 14:04 发表
明白了,gcc 里面 -O2 -O0 可以同时存在
最后一个有效是这个意思吧。
那可以直接在Makefile里面改,不用在/etc/mk.conf改,这样改影响面太大了。


刚才看了一下讲awk的书以及我们前段时间翻译的《FreeBSD系统编程》中的make部分,找出了另外一个挺有意思的解决办法:

在你自己的Makefile文件的开头加上这样两句话:

  1. TMP_CFLAGS = $(CFLAGS)
  2. CFLAGS != awk 'BEGIN {"echo $(TMP_CFLAGS)" | getline ; sub("-O2", "") ; print $0}'
复制代码


这样一来,不管之前给CFLAGS赋了什么值,通过这两句话,就能把其中的-O2选项给去掉。同理,只需替换第二行中的"-O2",即可去除或修改之前赋给CFLAGS的任何选项。

这样你也不必去动系统makefile里的东西了。
作者: congli    时间: 2006-03-24 15:22
呵~awk还可以这样用.真有趣!
作者: 雨丝风片    时间: 2006-03-24 15:46
原帖由 congli 于 2006-3-24 15:22 发表
呵~awk还可以这样用.真有趣!


俺也是现学现用,纯属为了练习awk,
作者: congli    时间: 2006-03-24 15:53
原帖由 雨丝风片 于 2006-3-24 15:46 发表


俺也是现学现用,纯属为了练习awk,

家里那本<sed与awk>还闲放着,没时间看.
不过这几天用awk解决了几个小问题.感觉挺不错!
作者: ljoolj    时间: 2006-03-24 16:14
原帖由 雨丝风片 于 2006-3-24 15:16 发表


刚才看了一下讲awk的书以及我们前段时间翻译的《FreeBSD系统编程》中的make部分,找出了另外一个挺有意思的解决办法:

在你自己的Makefile文件的开头加上这样两句话:

[code]TMP_CFLAGS = $(CFLAGS)
CFLAGS != awk 'BEGIN {"echo $(TMP_CFLAGS)" | getline ; sub("-O2", "") ; print $0}'


刚才测试过了,虽然多敲几个字,不过感觉巧妙一些哈:)
作者: 雨丝风片    时间: 2006-03-24 16:33
原帖由 ljoolj 于 2006-3-24 16:14 发表
刚才测试过了,虽然多敲几个字,不过感觉巧妙一些哈:)


跟我们写代码一样,一种“普适”的解决方案看上去一般都要复杂一些,

不过扩展方法也很简单:
sub("被替换选项", "替换后的内容" )
作者: gvim    时间: 2006-03-24 16:41
pmake 有这个功能阿。在man make : variable modifiers那里
${CFLAGS:C/-O./-O0/:u}
不过不知道是不是只能pmake这样搞。
我记得gmake有替换函数。
作者: 雨丝风片    时间: 2006-03-24 17:00
原帖由 gvim 于 2006-3-24 16:41 发表
pmake 有这个功能阿。在man make : variable modifiers那里
${CFLAGS:C/-O./-O0/:u}
不过不知道是不是只能pmake这样搞。
我记得gmake有替换函数。


哈哈,不错!把该看的书都看遍了,就是没去man make,结果绕了个大弯子!
俺今天是对pmake刮目相看了!
作者: 雨丝风片    时间: 2006-03-24 17:04
gmake的“替换引用”也可以达到这个目的:

6.3.1 Substitution References
A substitution reference substitutes the value of a variable with alterations that you specify. It has the form `$(var:a=b)' (or `${var:a=b}') and its meaning is to take the value of the variable var, replace every a at the end of a word with b in that value, and substitute the resulting string.

When we say "at the end of a word", we mean that a must appear either followed by whitespace or at the end of the value in order to be replaced; other occurrences of a in the value are unaltered. For example:

  foo := a.o b.o c.o
bar := $(foo:.o=.c)


原来看过这里的,今天没有想起来,
回去再仔细看看pmake的man,免得以后又走弯路了,

[ 本帖最后由 雨丝风片 于 2006-3-24 17:07 编辑 ]
作者: gvim    时间: 2006-03-24 22:10
挖到这个东西,来源 http://www.home.unix-ag.org/bmeurer/NetBSD/tips.html

What to put in /etc/mk.conf
  1. In order to get a sane build environment and to build sane packages out of your environment, you should consider overriding some default values in your /etc/mk.conf. For example, if you are running NetBSD/alpha, you shouldn't use any optimizations to cc(1), because gcc is still buggy on Alpha. And in general you should think twice before setting the optimization level above 2, because this might cause several programs to segfault frequently or not run at all. Here are some lines from my mk.conf, which might help you (they will honor all default values but -O*):

  2. COMMONCFLAGS?=-O2 -pipe
  3. COPTS:=${COMMONCFLAGS} ${COPTS:C/-O[0-9]*//g}
  4. CFLAGS:=${COMMONCFLAGS} ${CFLAGS:C/-O[0-9]*//g}
  5. CXXFLAGS:=${COMMONCFLAGS} ${CXXFLAGS:C/-O[0-9]*//g}
  6.           When trying to fix bugs in packages, it is helpful to append -Wall -Werror to COMMONCFLAGS, but beware: This might break a lot of configure scripts (so, you have the chance to fix them too ;-). Another needful thing to have in your mk.conf is support for sudo instead of the default su(1), so you may need not to type the root password everytime you install a package as a user. Here are the lines from my mk.conf:

  7. .if exists(/usr/pkg/bin/sudo)
  8. SU_CMD=/usr/pkg/bin/sudo /bin/sh -c
  9. .endif
  10.           Another helpful thing to do, is to override the default MASTER_SITE with faster (local) mirrors. E.g. I have a local NetBSD mirror (thats the tatooine.kosmos.all line, so don't simply copy&paste to your mk.conf :-), from where pkgsrc should try to fetch the needed distfiles first and after that fails, it will try several other mirrors, and only if a distfile cannot be found there, it'll try to fetch it from the MASTER_SITEs specified for the package. Here are the lines from my mk.conf:

  11. MASTER_SITE_OVERRIDE+= \
  12. ftp://tatooine.kosmos.all/pub/NetBSD/packages/distfiles/ \
  13. ftp://ftp.de.netbsd.org/pub/NetBSD/packages/distfiles/ \
  14. ftp://ftp2.de.netbsd.org/pub/NetBSD/packages/distfiles/ \
  15. ftp://ftp.at.netbsd.org/pub/NetBSD/packages/distfiles/ \
  16. ftp://ftp.netbsd.org/pub/NetBSD/packages/distfiles/
复制代码

[ 本帖最后由 gvim 于 2006-3-24 22:17 编辑 ]
作者: antijp    时间: 2006-03-25 20:13
如果那些东西是include搞进来的,那么用命令行传递给make的变量的优先级会更高,直接改写就可以了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2