"未定义的引用'战俘'"即使文件math.h和库链接-lm [英] "undefined reference to `pow'" even with math.h and the library link -lm

查看:86
本文介绍了"未定义的引用'战俘'"即使文件math.h和库链接-lm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用文件math.h -lm 选项编译。我已经尝试了所有的:

I'm using math.h and the -lm option to compile. I have tried all of:

gcc -o ssf ssf_tb.c ssf.c -lm
gcc -o ssf ssf_tb.c -lm ssf.c   
gcc -o -lm ssf -lm ssf_tb.c ssf.c

但这个错误:

undefined reference to 'pow'  

发生在所有的情况下。

happens on all cases.

推荐答案

-lm 在该行的末尾。

GCC处理指定输入它们出现在命令行上的顺序的最后程序的参数。在 -lm 参数传递给链接器,以及 ssf.c 参数,例如,编译和生成的目标文件被传递给链接器

gcc processes the arguments that specify inputs to the final program in the order they appear on the command line. The -lm argument is passed to the linker, and the ssf.c argument, for example, is compiled, and the resulting object file is passed to the linker.

链接器也是为了处理输入。当它看到一个库, -lm 指定,它看起来,看看是否能库提供任何符号链接器目前需要。如果是这样,它的副本与从库那些符号的模块,并建立它们到程序中。当链接器看到的目标模块,它建立目标模块到程序中。把目标模块到程序后,链接器不回去,看看是否需要从早期库中任何东西。

The linker also processes inputs in order. When it sees a library, as -lm specifies, it looks to see if that library supplies any symbols that the linker currently needs. If so, it copies the modules with those symbols from the library and builds them into the program. When the linker sees an object module, it builds that object module into the program. After bringing an object module into the program, the linker does not go back and see if it needs anything from earlier libraries.

由于您列出的第一个库,链接器没看到什么,它从库需要。如果先列出对象模块,连接器将带来目标模块到程序中。在这样做的过程中,接头将使所有的对象需要的未定义的符号的列表。然后,当链接程序将库中,它会看到该库设备定义为那些符号,并且将与这些符号模块带入程序

Because you listed the library first, the linker did not see anything that it needed from the library. If you list the object module first, the linker will bring the object module into the program. In the process of doing this, the linker will make a list of all the undefined symbols that the object needs. Then, when the linker sees the library, it will see that the library supplies definitions for those symbols, and it will bring the modules with those symbols into the program.

这篇关于"未定义的引用'战俘'"即使文件math.h和库链接-lm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆