Reputation: 21
please, could you help me how we link the libm math library in microsoft visual studio 2010,
in order to use some trigonometric function in a c program ?
Upvotes: 0
Views: 5922
Reputation: 238
It should only be necessary to put
#include <math.h>
in your program.
The following compiles without error or warning, in a new empty project in 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;
}
Here is the VS2010 compile output:
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 ==========
If I omit #include <math.h>
I get this:
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 ==========
No changes need to be made to the VS2010 libraries that are linked.
There are no red underlines (intellisense errors) either.
Upvotes: 1
Reputation: 733
I think I know your problem. It is not the linker problem.
It is the header file name. For example, to use math library, you type #include <math.h>
. If you type #include <math>
, you will get cannot include file error.
Create a Visual C++ project in Visual Studio 2010 as per usual. Use the following to test:
#include <math.h>
#include <iostream>
using namespace std;
int main(void)
{
double test = 9.0;
double result = sqrt(test);
cout << "test = " << test << " result = " << result << endl;
}
And the result is:
test = 9 result = 3
Hope this helps.
Upvotes: 0