问题描述
为什么我收到以下错误
/tmp/ccuWdVB3.o: In function `test':
MyCode.c:(.text+0x1c): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
MyCode.c
#include<stdio.h>
#include<math.h>
int test(int input1)
{
int x = 8;
int z = sqrt(x);
}
int main(int argc,char * a[]) {
}
使用命令运行时:
gcc -o a.out -lm MyCode.c
但是当我运行命令时
gcc MyCode.c -o a.out -lm
一切正常。为什么“MyCode.c”cli 选项的顺序在这里很重要? 还是我做错了什么?
解决方法
库在链接过程中只搜索一次(它们可能包含数百万个符号和对象),这就是为什么当链接器必须搜索的所有对象都已知时,它们应该被指定为最后一个。在项目中的每个目标文件之后搜索巨型库中的符号会非常低效。