读书人

gcc下的链接有关问题…

发布时间: 2012-04-30 22:33:26 作者: rapoo

gcc下的链接问题……
测试代码……
main.c

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




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

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


ps:此代码源自我接手的某个项目,原来的库里就有类似的代码,引发了一堆诡异行为,把我搞晕了+_+

[解决办法]
gcc 4.4.1版?
error: too few arguments to function 'erfc'
[解决办法]
man erfc

的确有这个函数啊。



SYNOPSIS
? #include <math.h>

? double erf(double x);
? float erff(float x);
? long double erfl(long double x);

? double erfc(double x);
? float erfcf(float x);
? long double erfcl(long double x);

? Link with -lm.

[解决办法]
我在unix下试了下?
C/C++ code
#include<stdio.h>#include <math.h>int main(){    int d=erfc(11);/*请注意,这是一个未曾定义也未曾声明过的函数名*/    printf("%d",d);    return 0;}
[解决办法]
NAME
? erf(), erff(), erfl(), erfw(), erfq(), erfc(), erfcf(), erfcl(),
? erfcw(), erfcq() - error function and complementary error functions

?SYNOPSIS
? #include <math.h>

? double erf(double x);

? double erfc(double x);

? Itanium(R)-based System Only
? float erff(float x);

? long double erfl(long double x);

? extended erfw(extended x);

? quad erfq(quad x);

? float erfcf(float x);

? long double erfcl(long double x);

? extended erfcw(extended x);

? quad erfcq(quad x);

?DESCRIPTION
? erf() returns the error function of x, defined as:

? (2/sqrt(pi)) *
? (integral with respect to t from 0 to x of (exp(pow(-t,2))))

? erfc() returns the complementary value, 1.0 - erf(x). It prevents the
? extreme loss of relative accuracy if erf(x) is called for a large x
? and the result is subtracted from 1.0. (For example, for x = 5, twelve
? decimal places are lost.)
Choose a letter between 'a' and 'z' (press RETURN)[1] + Stopped man erfc
odsetl1@dsadm#[/dsadm] man erfc

?erf(3M) erf(3M)

?NAME
? erf(), erff(), erfl(), erfw(), erfq(), erfc(), erfcf(), erfcl(),
? erfcw(), erfcq() - error function and complementary error functions

?SYNOPSIS
? #include <math.h>

? double erf(double x);

? double erfc(double x);

? Itanium(R)-based System Only
? float erff(float x);

? long double erfl(long double x);

? extended erfw(extended x);

? quad erfq(quad x);

? float erfcf(float x);

? long double erfcl(long double x);

? extended erfcw(extended x);

? quad erfcq(quad x);

?DESCRIPTION
? erf() returns the error function of x, defined as:

? (2/sqrt(pi)) *
? (integral with respect to t from 0 to x of (exp(pow(-t,2))))

? erfc() returns the complementary value, 1.0 - erf(x). It prevents the
? extreme loss of relative accuracy if erf(x) is called for a large x
? and the result is subtracted from 1.0. (For example, for x = 5, twelve
? decimal places are lost.)

? Itanium-based System Only
? erff() and erfcf() are float versions of erf() and erfc()
? respectively; they take a float argument and return a float result.



? erfl() and erfcl() are long double versions of erf() and erfc()
? respectively; they take a long double argument and return a long
? double result.

? erfw() and erfcw() are extended versions of erf() and erfc()
? respectively; they take an extended argument and return an extended
? result.

?Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003

?erf(3M) erf(3M)

? erfq() and erfcq() are equivalent to erfl() and erfcl() respectively
? on HP-UX systems.

?USAGE
? To use these functions compile either with the default -Ae option or
? with the -Aa and -D_HPUX_SOURCE options.

? To use (for Itanium-based systems) erfw(), erfcw(), erfq(), or
? erfcq(), compile also with the -fpwidetypes option.

? To use any of these functions, make sure your program includes
? <math.h>, and link in the math library by specifying -lm on the
? compiler or linker command line.

? For more information, see the HP-UX Floating-Point Guide.

?RETURN VALUE
? erf(+-0) returns +-0.

? If x is +INFINITY, erf() returns 1.0.

? If x is -INFINITY, erf() returns -1.0.

? If x is +INFINITY, erfc() returns zero.

? If x is -INFINITY, erfc() returns 2.0.

? Whether erf() and erfc() raise the inexact exception is unspecified.

? If x is NaN, erf() and erfc() return NaN.

?ERRORS
? No errors are defined.

?SEE ALSO
? exp(3M), pow(3M), sqrt(3M), math(5).

?STANDARDS CONFORMANCE
? erf(), erfc() : SVID3, XPG4.2, ISO/IEC C99 (including Annex F, ``IEC
? 60559 floating-point arithmetic'')

? erff(), erfl(), erfcf(), erfcl() : ISO/IEC C99 (including Annex F,
? ``IEC 60559 floating-point arithmetic'')

?Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003
[解决办法]
test.c:4: warning: incompatible implicit declaration of built-in function 'erfc
test.c:4: error: too few arguments to function 'erfc'


gcc version 4.3.2


路过Jf
[解决办法]
这个的问题是,erfc()这个函数本就是个数学函数,跟sin()一样

在某些编译器中,要用这样的函数需要手动的加-lm参数,表示链接数学库。但在gcc中,好像不需要加也

可以,编译器会默认的去搜索库的目录,就是ldconfig检测的那个路径,如果在库目录下找到这个函数所在

的库了,那就直接链接,但会给出一个警告,大概如下:

test.c:5: warning: incompatible implicit declaration of built-in function ‘erfc’
test.c:8: warning: incompatible implicit declaration of built-in function ‘sin’

而要是显示的包含了math.h,那就不存在这个问题了。警告就会消除掉
[解决办法]
GCC 4.5.0
报警告
incompatible implicit declaration of built-in function 'erfc'
和错误
too few arguments to function "erfc"
[解决办法]
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 ===|

[解决办法]

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)

这个这个。。。

读书人网 >C语言

热点推荐