我在我的计算机(Ubuntu)上安装了一个名为 xx
的 C 库。 ,其头文件位于 /usr/local/include/xx/xx.h
和 .a file
和.so file
位于路径 /usr/local/lib
中命名libxx.a
和libxx.so
.
测试文件:
#include<xx/xx.h>
#include<stdio.h>
int main(){
printf("Test\n");
call_function_declared_in_the_header_file();
return 0;
}
当我使用gcc编译gcc test.c -o test时,出现以下错误:
/tmp/ccb7O0eh.o: In function `main':
test.c:(.text+0xa): undefined reference to `call_function_declared_in_the_header_file'
collect2: error: ld returned 1 exit status
我不知道为什么。我不太擅长C语言。那么我该如何解决这个问题呢?
最佳答案
您必须链接该库,即如果您想包含 <math.h>
( libm.so
/libm.a
) 您必须链接到:
gcc program.c -o program -lm
其中-lm
调用链接
参见http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html ,
https://www.rapidtables.com/code/linux/gcc/gcc-l.html
编译总是涉及编译器和链接器
关于c - 海湾合作委员会 : undefined reference to 'function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138005/