Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的程序的一部分是计算sqrt浮点数。当我写sqrt(1.0f);我成功编译程序,但当我写sqrt(-1.0f); 编译失败undefined reference to 'sqrt'- 我想在这种情况下,nan值将被返回......我使用 gcc 编译程序。当我用 Visual Studio 编译它时,它会以 sqrt 的否定参数成功编译。问题怎么解决 谢谢
sqrt
sqrt(1.0f);
sqrt(-1.0f);
undefined reference to 'sqrt'
nan
您必须-lm在大多数基于 Unix 的系统上添加标志,如:
-lm
编译使用:
gcc -c file.c
然后使用链接:
gcc -o program file.o -lm
或者,如果您不想将两个编译步骤分开,只需编写:
gcc -o program file.c -lm
Link with-lm链接到数学库