Search code examples
clibm

How to link the libm library in microsoft visual studio 2010


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 ?


Solution

  • 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.