C语言 ld:未定义对符号“log2@@GLIBC_2.2.5”的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16006145/
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
ld: undefined reference to symbol 'log2@@GLIBC_2.2.5'
提问by Jiew Meng
Whats wrong with
怎么了
for (level = 1; level <= log2((double)size); level++)
^
Seems like its from using log2()but whats wrong? I am using it with OpenMPI code actually, but commenting this line fixes things.
似乎它从使用log2()但有什么问题?我实际上将它与 OpenMPI 代码一起使用,但注释这一行可以解决问题。
Full Source(http://pastie.org/7559178)see line 40
完整来源( http://pastie.org/7559178)见第 40 行
[jiewmeng@JM Assign3]$ mpicc -o cpi cpi.c && mpirun -np 16 cpi
/usr/bin/ld: /tmp/cca9x4he.o: undefined reference to symbol 'log2@@GLIBC_2.2.5'
/usr/bin/ld: note: 'log2@@GLIBC_2.2.5' is defined in DSO /usr/lib/libm.so.6 so try adding it to the linker command line
/usr/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
Seems like log2(4)will work but I cant pass in a variable?
似乎log2(4)可以工作,但我不能传入一个变量?
回答by Shafik Yaghmour
In in order to link libmyou need to add -lmargument, as this document; MPI under Linux in the Math Departmentsays:
为了链接,libm您需要添加-lm参数,如本文档;数学系 Linux 下的 MPI说:
If your code includes mathematical functions (like exp, cos, etc.), you need to link to the mathematics library libm.so. This is done, just like for serial compiling, by adding -lm to the end of your compile command, that is,
mpicc -o sample sample.c -lm
如果您的代码包含数学函数(如 exp、cos 等),则需要链接到数学库 libm.so。这样做,就像串行编译一样,通过在编译命令的末尾添加 -lm,即,
mpicc -o 示例 sample.c -lm

