编译://lib/x86_64-linux-gnu/libm.so.6:添加符号时出错:缺少DSO

Cat*_*rix 5 ubuntu 64-bit gcc libraries libm

我在 64 位 Ubuntu、Linux 上编译适用于 32 位 unix 系统的代码时遇到问题。有没有人有任何想法可能是什么问题?

gcc main.o test.o render.o transform.o model.o vector.o color.o -o the_thing -lSDL
/usr/bin/ld: transform.o: undefined reference to symbol 'cos@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

ran*_*anu 5

libm当您处理使用数学函数的代码时,您也应该链接。

这个答案

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

mpicc -o 示例 sample.c -lm

一个好处

我在您的编译行中看到您正在使用 -lSDL。SDL 适用于 C 和 C++,因此,如果您使用纯 C,则应包含默认的数学 C 标头,即:

#include <math.h>

我认为你做了这样的事情:

#include <cmath>

如果您正在使用 C++,则不应使用 C 编译器进行编译,g++而应使用。