请帮我看看如何在 Microsoft Visual Studio 2010 中链接 libm 数学库,
为了在 C 程序中使用一些三角函数?
最佳答案
只需要放置
#include <math.h>
在你的程序中。
在 VS2010 中的新空项目中,以下内容编译时不会出现错误或警告:
#include <stdio.h>
#include <math.h>
int main(){
double a,b,c;
char d;
a = 0.0;
b = cos(a);
c = sqrt(b);
printf("cos(%lf) = %lf, sqrt(cos(%lf)) = %lf\n", a, b, a, c);
d = getchar();
return 0;
}
这是 VS2010 编译输出:
1>------ Rebuild All started: Project: test3, Configuration: Debug Win32 ------
1> source.c
1> test3.vcxproj -> c:\users\andy\documents\visual studio 2010\Projects\test3\Debug\test3.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
如果我省略#include <math.h>
我明白了:
1>------ Rebuild All started: Project: test3, Configuration: Debug Win32 ------
1> source.c
1>c:\users\andy\documents\visual studio 2010\projects\test3\test3\source.c(9): warning C4013: 'cos' undefined; assuming extern returning int
1>c:\users\andy\documents\visual studio 2010\projects\test3\test3\source.c(10): warning C4013: 'sqrt' undefined; assuming extern returning int
1> test3.vcxproj -> c:\users\andy\documents\visual studio 2010\Projects\test3\Debug\test3.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
无需对链接的 VS2010 库进行任何更改。
也没有红色下划线(智能感知错误)。
关于c - 如何在Microsoft Visual Studio 2010中链接libm库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880793/