我目前正在为 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