相关疑难解决方法(0)

log(10.0)可以编译但是log(0.0)不能吗?

对于以下C源代码:

#include <math.h>

int main(void)
{
    double          x;

    x = log(0.0);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时gcc -lm,我得到:

/tmp/ccxxANVH.o: In function `main':
a.c:(.text+0xd): undefined reference to `log'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但是,如果我替换log(0.0)log(10.0),那么它可以成功编译.

我不太明白这一点,因为无论它们是否具有数学意义,它们都应该编译 - 没有语法错误.有人能解释一下吗?

以防万一,我的gcc -v输出:

Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 …
Run Code Online (Sandbox Code Playgroud)

c gcc math.h constantfolding

46
推荐指数
2
解决办法
2714
查看次数

将代码与 `floor();`、`ceil();` 和 `pow();` 链接时出错

我在 GNU/Linux Debian 8.5 下编码

我有一个简单的程序。

如果我用gcc prog.c它编译它就可以了!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

int main(int argc, char const *argv[]) {

    float _f = 3.1415f;

    floor(_f);
    ceil(_f);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加pow(),它会说它找不到pow,我需要添加gcc prog.c -lm以使其正确。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

int main(int argc, char const *argv[]) {

    float _f = 3.1415f;

    floor(_f);
    ceil(_f);
    pow(_f, 2);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我是对的,pow(), ceil(),floor()都是来自<math.h>?

那么,为什么不floor() …

c flags math.h linker-errors undefined-reference

2
推荐指数
1
解决办法
1029
查看次数