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

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

问题描述

我使用 math.h-lm 选项进行编译.我都试过了:

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

但错误:

未定义的对pow"的引用

发生在所有情况下.

解决方案

-lm 放在行尾.

gcc 按照它们在命令行中出现的顺序处理指定最终程序输入的参数.-lm 参数传递给链接器,例如编译 ssf.c 参数,并将生成的目标文件传递给链接器.>

链接器也按顺序处理输入.当它看到一个库时,如 -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

but the error:

undefined reference to 'pow'  

happens on all cases.

解决方案

Put the -lm at the end of the line.

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.

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.

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

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