c - -lm 不工作,除非它在命令的末尾

标签 c gcc math.h

<分区>

我目前正在为 uni asssessment 编写一个程序,他们有一个编译它的设置行,所以如果它不能与它一起工作,它就不会被接受。 他们使用的命令是

gcc -Wall -ansi -lm program.c -o program.out

我的程序不会那样编译,它会给我一个 undefined reference 错误(使用 math.h 库引用我的 log10) 如果我使用:

gcc -Wall -ansi program.c -o program.out -lm

有效

我的问题是什么?

我使用的是 windows 10 64bit 并安装了 windows bash 和 gcc。

最佳答案

如果您的讲师使用的是 gold 而您使用的是 GNU ld,则可以解释这一点。这是两个链接器,都是 GNU 项目的一部分,并且都常用于 GCC。

如果您使用的是 GNU ld,您会得到“传统”行为:

The order of specifying the -L and -l options, and the order of specifying -l options with respect to pathname operands is significant.

这意味着您必须将 -lm 放在任何依赖于它的目标文件和库之后。

但是,如果您使用的是黄金,-l 选项可能会首先出现。

如果你的系统上安装了gold,你可以自己测试。

这是我得到的:

$ gcc -lm program.c 
/tmp/ccJmBjmd.o: In function `main':
program.c:(.text+0x15): undefined reference to `sin'
collect2: error: ld returned 1 exit status

但如果我使用黄金,效果很好:

$ gcc -lm program.c -fuse-ld=gold

关于c - -lm 不工作,除非它在命令的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722261/

相关文章:

gcc - gamma(double x) 的定义是什么?为什么它在两个 gcc 版本上不同?

c - 将字符串元素复制到另一个字符串时出现段错误

c++ - tr1/normal_distribution : No such file or directory

linux - ELF Relocation - 这些符号从何而来?

c - 去掉 "#include <math.h>"前面的注释破坏了我的代码

c++ - 与 "math.h"的链接错误 - 告诉链接器与其链接的选项是什么?

c - 在 C 中替代此 switch 语句

c - int 数组上的浮点异常 - C

c - C中的汇编代码是什么?

c++ - 在 g++ 中构建事务内存 C++ 代码