ld:对符号’log2 @@ GLIBC_2.2.5’的未定义引用

什么错了

for (level = 1; level <= log2((double)size); level++) ^ 

看起来像使用log2()但有什么不对? 我实际上正在使用它与OpenMPI代码,但注释这一行修复了一些事情。

完整来源 ( 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 

好像log2(4)会起作用,但我不能传入一个变量?

在为了链接libm你需要添加-lm参数,作为这个文件; Linux下的数学系MPI说:

如果您的代码包含数学函数(如exp,cos等),则需要链接到数学库libm.so. 通过在编译命令的末尾添加-lm,就像进行串行编译一样,这样就完成了,也就是说,

mpicc -o sample sample.c -lm