LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1455|回复: 7

gcc隐含参数问题

[复制链接]
发表于 2009-5-21 16:15:37 | 显示全部楼层 |阅读模式
本人今天写程序的时候发现了下面一个问题。

[souldump@localhost bin]$ cat test.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    func(1, 2);
}
[souldump@localhost bin]$ cat test2.c
#include <stdio.h>

int func(int a, unsigned int flags, int third)
{
    printf("in %s a is %d flags %d third %p \n", __FUNCTION__, a ,flags, third);
   
}
   
[souldump@localhost bin]$ gcc -o main test.c test2.c
[souldump@localhost bin]$ gdb main
GNU gdb Fedora (6.8-29.fc10)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(no debugging symbols found)
(gdb) disass main
Dump of assembler code for function main:
0x080483c4 <main+0>:    lea    0x4(%esp),%ecx
0x080483c8 <main+4>:    and    $0xfffffff0,%esp
0x080483cb <main+7>:    pushl  -0x4(%ecx)
0x080483ce <main+10>:    push   %ebp
0x080483cf <main+11>:    mov    %esp,%ebp
0x080483d1 <main+13>:    push   %ecx
0x080483d2 <main+14>:    sub    $0x14,%esp
0x080483d5 <main+17>:    movl   $0x2,0x4(%esp)
0x080483dd <main+25>:    movl   $0x1,(%esp)
0x080483e4 <main+32>:    call   0x80483f4 <func>
0x080483e9 <main+37>:    add    $0x14,%esp
0x080483ec <main+40>:    pop    %ecx
0x080483ed <main+41>:    pop    %ebp
0x080483ee <main+42>:    lea    -0x4(%ecx),%esp
0x080483f1 <main+45>:    ret   
End of assembler dump.
(gdb) disass func
Dump of assembler code for function func:
0x080483f4 <func+0>:    push   %ebp
0x080483f5 <func+1>:    mov    %esp,%ebp
0x080483f7 <func+3>:    sub    $0x18,%esp
0x080483fa <func+6>:    mov    0x10(%ebp),%eax
0x080483fd <func+9>:    mov    %eax,0x10(%esp)
0x08048401 <func+13>:    mov    0xc(%ebp),%eax
0x08048404 <func+16>:    mov    %eax,0xc(%esp)
0x08048408 <func+20>:    mov    0x8(%ebp),%eax
0x0804840b <func+23>:    mov    %eax,0x8(%esp)
0x0804840f <func+27>:    movl   $0x80484f4,0x4(%esp)
0x08048417 <func+35>:    movl   $0x80484fc,(%esp)
0x0804841e <func+42>:    call   0x80482f4 <printf@plt>
0x08048423 <func+47>:    leave  
0x08048424 <func+48>:    ret   
End of assembler dump.
(gdb) r
Starting program: /home/souldump/bin/main
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
in func a is 1 flags 2 third 0xbffff478

Program exited with code 051.
Missing separate debuginfos, use: debuginfo-install glibc-2.9-3.i686
(gdb)


我有一个3个参数的函数,但是我传两个参数照样能编译过,
我又试了下,传5个参数也没问题。
GCC把我搞了,高手帮忙解决下吧。
发表于 2009-5-21 18:31:28 | 显示全部楼层
所以才需要头文件, 才需要函数声明不是
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-5-26 09:04:32 | 显示全部楼层
你说的函数声明我知道,很多时候我是不想用,
而且现在我觉得这种行为有可取之处,让他实
现类似C++的一些功能。不过我不知道是不
是其他的编译器行为也一样,我只有gcc,要是
这样就好了。
回复 支持 反对

使用道具 举报

发表于 2009-5-26 10:58:11 | 显示全部楼层
Post by souldump;1991048
你说的函数声明我知道,很多时候我是不想用


难道LZ就是传言中的无招胜有招境界?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-5-26 17:28:15 | 显示全部楼层
有些时候,你忘记把所有的函数声明都放到了头文件里,或者你忘记包含了一个头文件,
而你又恰好忘记函数的参数个数,这种情况就发生了,我也希望GCC能帮我检查出这种
问题,但是不幸的,gcc做不到这点。

有些函数我希望不是static的(或者是临时决定为extern),但是在开发过程中我有不想
在头文件中声明让别人使用,因为以后可能忘记删除。这种事我偶尔会做。我认为这是个
方便之处,但是毕竟还是给我带来了麻烦。
回复 支持 反对

使用道具 举报

发表于 2009-5-26 19:16:55 | 显示全部楼层
越来越看不懂 souldump 兄的意图了. 不过我个人的习惯是使用 -Wall 的编译参数, 并保证所有的 warning 都被处理掉 (加上 -Werror 就可以把这些 warning 当作 error 处理). 这种情况下至少可以避免头文件中忘记/错误声明的情况.

至于这种不想公开声明同时又想提供可用性的函数, 确实好像没有什么办法保证. 可以当做是小动作的惩罚?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-5-27 13:20:48 | 显示全部楼层
这种情况就是在开发模块的时候,有些私有函数要测试的情况,如果你放头文件声明了,
那么可能忘记最后删除,而且你还不能将函数定义成static,所以我经常用这种方式,
那样即便我将来忘记把函数改成似有,别人也不知道我有这个函数。可惜我记性不太好,
总是忘记函数原型。。。。
回复 支持 反对

使用道具 举报

发表于 2009-6-13 02:52:54 | 显示全部楼层
精华啊   不过我已经看过了 哈哈 还是顶下  *^__^*                     
   
     
      
   
   
  
     
      
------------------------------------------------------------
张柏芝模型制作在地面上,不生乔木,只生野草,疤痕修复这是我的罪过。
回复 支持 反对

使用道具 举报

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

本版积分规则

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