gcc下的链接问题……

mstlq 2010-05-18 06:01:41
测试代码……
main.c

#include<stdio.h>
int main()
{
int d=erfc();/*请注意,这是一个未曾定义也未曾声明过的函数名*/
printf("%d",d);
return 0;
}




在gcc4.4.3和3.5.5下均顺利编译并且在连接过程中也不报错……
我能理解erfc会被编译器当作外部函数,从而可以通过编译,
我本以为在连接的时候会因为找不到函数的定义而link失败,没想到gcc居然允许它过了(VC是不行的)……

请各位帮忙解解惑,谢谢^_^


ps:此代码源自我接手的某个项目,原来的库里就有类似的代码,引发了一堆诡异行为,把我搞晕了+_+
...全文
422 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
fantasylyz 2012-04-27
  • 打赏
  • 举报
回复
我也遇到同样的问题
mstlq 2010-05-19
  • 打赏
  • 举报
回复
似乎大家的都会报错……
看来是我倒霉,人品不好了……

结帖,谢谢各位^_^
真的吗咚咚 2010-05-19
  • 打赏
  • 举报
回复
我用的是gcc版本3.2,在代码中多include一个文件<math.h>,
运行gcc -E main.c > preprocess.txt,
生成的preprocess.txt文件里可以看到,在系统库里面有double erfc (double);这个函数。
preprocess.txt文件内容部分展开:(...为省略)
...

extern double acosh (double);
extern double atanh (double);
extern double remainder (double, double);
extern double gamma (double);
extern double gamma_r (double, int *);
extern double lgamma (double);
extern double lgamma_r (double, int *);
extern double erf (double);
extern double erfc (double);
extern double y0 (double);
extern double y1 (double);
extern double yn (int, double);
extern double j0 (double);
extern double j1 (double);
extern double jn (int, double);



extern double hypot (double, double);

...

extern __attribute__((dllimport)) const enum __fdlibm_version __fdlib_version;
# 365 "/usr/include/math.h" 3

# 3 "test2.c" 2

int main()
{
int d = erfc();
printf("%d", d);
return 0;
}

阿磊2013 2010-05-19
  • 打赏
  • 举报
回复
学习了
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 mstlq 的回复:]

引用 3 楼 akirya 的回复:
gcc 4.4.1版
error: too few arguments to function 'erfc'


确认一下,请问不#include<math.h>也会报此错误吗?
等完此回复便结帖,谢谢大家^_^
[/Quote]
我加上也报错
x.c:6: error: too few arguments to function 'erfc'
pengzhixi 2010-05-19
  • 打赏
  • 举报
回复
DEV-CPP下就会提示未定义。加上math头文件后提示参数太少。
jialina198799 2010-05-19
  • 打赏
  • 举报
回复
就是这样子得我也遇到了同样的问题
localxiao 2010-05-19
  • 打赏
  • 举报
回复

D:\>gcc 2.c
2.c: In function 'main':
2.c:4: warning: incompatible implicit declaration of built-in function 'erfc'
2.c:4: error: too few arguments to function 'erfc'

D:\>gcc -v
Using built-in specs.
Target: mingw32
Configured with: ../gcc-4.4.0/configure --enable-languages=c,ada,c++,fortran,jav
a,objc,obj-c++ --disable-sjlj-exceptions --enable-shared --enable-libgcj --enabl
e-libgomp --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enab
le-version-specific-runtime-libs --prefix=/mingw --with-gmp=/mingw/src/gmp/root
--with-mpfr=/mingw/src/mpfr/root --build=mingw32
Thread model: win32
gcc version 4.4.0 (GCC)

这个这个。。。
louyong0571 2010-05-19
  • 打赏
  • 举报
回复
好多问题,好长的帖子
wbgxx 2010-05-19
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 sabflying 的回复:]

链接库里已经有了这个函数,而且头文件已经被项目的其他部分包含了
[/Quote]

但是他的头文件里却没有啊?怎么确定已经被项目的其他部分包含了呢
sabflying 2010-05-19
  • 打赏
  • 举报
回复
链接库里已经有了这个函数,而且头文件已经被项目的其他部分包含了
mstlq 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 pottichu 的回复:]
楼主是什么平台?
[/Quote]

win xp……
minGW
>_<
flyyyri 2010-05-18
  • 打赏
  • 举报
回复
ding
pottichu 2010-05-18
  • 打赏
  • 举报
回复
楼主是什么平台?
mstlq 2010-05-18
  • 打赏
  • 举报
回复
贴一贴我编译器的状况……

Configured with: ../../src/gcc-4.4.3/configure --prefix=/mingw --host=mingw32 --target=mingw32 --bui
ld=i686-pc-linux-gnu --enable-languages=c,c++ --enable-shared --enable-cxx-flags='-fno-function-sect
ions -fno-data-sections' --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enab
le-threads=win32 --enable-libgomp --disable-nls --disable-libstdcxx-pch --disable-win32-registry --d
isable-sjlj-exceptions --with-dwarf2 --with-gmp=/home/loaden/AUR/mingw32/gmp --with-mpfr=/home/loade
n/AUR/mingw32/mpfr --with-pkgversion='QP MinGW32' --with-bugurl=http://www.qpsoft.com/blog/guestbook
.php
Thread model: win32
十八道胡同 2010-05-18
  • 打赏
  • 举报
回复
code::blocks 8.02
|
|=== 10Questions, Debug ===|
E:\My project\10Questions\main.c||In function `main':|
E:\My project\10Questions\main.c|4|warning: implicit declaration of function `erfc'|
E:\My project\10Questions\main.c|4|error: too few arguments to function `erfc'|
||=== Build finished: 1 errors, 1 warnings ===|
mstlq 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 mzlogin 的回复:]
GCC 4.5.0
报警告
incompatible implicit declaration of built-in function 'erfc'
和错误
too few arguments to function "erfc"
[/Quote]

我的minGW让通过编译了,我觉得我应该去买彩票……
weilong8998 2010-05-18
  • 打赏
  • 举报
回复
学习了....
mzlogin 2010-05-18
  • 打赏
  • 举报
回复
GCC 4.5.0
报警告
incompatible implicit declaration of built-in function 'erfc'
和错误
too few arguments to function "erfc"
jbz001 2010-05-18
  • 打赏
  • 举报
回复
居然通过编译?
加载更多回复(21)

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧