Linux “未定义对‘pow’的引用”即使使用 math.h 和库链接 -lm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16344445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"undefined reference to `pow'" even with math.h and the library link -lm
提问by user2344169
I'm using math.h
and the -lm
option to compile. I have tried all of:
我正在使用math.h
和-lm
编译选项。我已经尝试了所有:
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.
发生在所有情况下。
采纳答案by Eric Postpischil
Put the -lm
at the end of the line.
将 放在-lm
行尾。
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.
gcc 按照它们在命令行中出现的顺序处理指定最终程序输入的参数。该-lm
参数被传递给链接器和ssf.c
参数,例如,被编译,并且将所得的目标文件被传递给链接器。
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.
链接器还按顺序处理输入。当它看到一个库时,如-lm
指定的那样,它会查看该库是否提供了链接器当前需要的任何符号。如果是,它会从库中复制带有这些符号的模块并将它们构建到程序中。当链接器看到一个目标模块时,它会将该目标模块构建到程序中。将目标模块引入程序后,链接器不会返回查看是否需要来自早期库的任何内容。
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.
因为您首先列出了库,链接器没有从库中看到它需要的任何东西。如果首先列出目标模块,链接器会将目标模块带入程序。在执行此操作的过程中,链接器将列出对象需要的所有未定义符号。然后,当链接器看到库时,它会看到库为这些符号提供定义,并将带有这些符号的模块带入程序。