0

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 ?

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
user3466199
  • 21
  • 2
  • 10
  • Create a project in Visual Studio 2010 usually loads those for you. What is the actual error message you see? – Just a HK developer Sep 17 '14 at 01:17
  • What have you done so far? add the .lib files with the other MSVC 2010 libraries under C:/..../VC/include folder. Then if there are DLL files, add the .dll files under C:/Windows.../System32. To link, with MSVC 2010 go to Properties->Linker->Input->Additional Dependencies then add libm.lib files. – Juniar Sep 17 '14 at 03:45

2 Answers2

1

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.

lone gold
  • 238
  • 1
  • 3
  • 8
  • your program work perfectly, but in my case the cos function is in another function (function_name.c ), and cos function still underlined in red – user3466199 Sep 17 '14 at 11:07
  • You need to put `#include ` at the top of any file where you are using `cos`. So put it in your `function_name.c` file also. – lone gold Sep 17 '14 at 21:08
  • yeah that correct, the problem has been resolved, thanks very much – user3466199 Sep 18 '14 at 19:18
0

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.

  • unfortunently not in my case, there is a red line under sqrt function ? – user3466199 Sep 17 '14 at 01:39
  • 1
    I want a program in c not c++ – user3466199 Sep 17 '14 at 01:39
  • To compile C program go to Properties->C/C++->Advanced-> then select Compile as C Code(/TP). – Juniar Sep 17 '14 at 03:53
  • Ah. If there is a red line under sqrt function, please just say that in the first place! If there is a red line, that means the {[spell checker](http://msdn.microsoft.com/en-us/library/jj170375.aspx)} is working! Then just make sure you have the `#include ` at the top of the file. – Just a HK developer Sep 17 '14 at 07:04