linux(ubuntu)GCC编译包含库函数的问题

GCC 编译命令通常为:gcc hello.c -o hello.out

注意:若hello.c中引用有库函数(比如math.h),直接编译会出错

"/tmp/ccalvMPY.o: In function `main':
/tmp/ccalvMPY.o(.text+0x40): undefined reference to `pow'"

原因:
因为linux中, pow 函数定义在 libm.so中,
不像一般DevC++或TC是放在C语言的/lib下,
所以, C无法在连接时, 找到相对应的库来连接.

解决方法:
编译器有一些比较通用的参数,其中的-l用来标识要链接的库信息:
-lm 表示需要链接数学库libm.a或libm.so或libm.sl

使用如下编译命令告诉程序数学库的位置即可

gcc hello.c -o hello.out -lm

GCC 编译命令通常为:gcc hello.c -o hello.out

注意:若hello.c中引用有库函数(比如math.h),直接编译会出错

"/tmp/ccalvMPY.o: In function `main':
/tmp/ccalvMPY.o(.text+0x40): undefined reference to `pow'"

原因:
因为linux中, pow 函数定义在 libm.so中,
不像一般DevC++或TC是放在C语言的/lib下,
所以, C无法在连接时, 找到相对应的库来连接.

解决方法:
编译器有一些比较通用的参数,其中的-l用来标识要链接的库信息:
-lm 表示需要链接数学库libm.a或libm.so或libm.sl

使用如下编译命令告诉程序数学库的位置即可

gcc hello.c -o hello.out -lm

猜你喜欢

转载自www.cnblogs.com/liu-jeet/p/9270642.html
今日推荐