本文分享了在使用GCC编译器时遇到的数学函数链接错误解决方案。作者在尝试编译一个包含数学函数的工程时,遇到了undefined reference to symbol 'pow@@GLIBC_2.2.5'的错误。通过将编译器从gcc更改为g++并正确添加-lm选项,成功解决了这一问题。文章还提到了Qt工程创建的makefile作为良好的学习资源。
@:~/Documents/InterviewSolution/InterviewSolution$ which cc /usr/bin/cc @:~/Documents/InterviewSolution/InterviewSolution$ ls -al /usr/bin/cc lrwxrwxrwx 1 root root 20 11月 8 19:59 /usr/bin/cc -> /etc/alternatives/cc @:~/Documents/InterviewSolution/InterviewSolution$ ls -al /etc/alternatives/cc lrwxrwxrwx 1 root root 12 11月 8 19:59 /etc/alternatives/cc -> /usr/bin/gcc
参考
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
If you are working with C++ you should not compile using the C compiler, use g++ instead.