Thread: Cannot resolve a compile error with GCC compiler in Fedora

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Johannesburg, South Africa
    Posts
    3

    Cannot resolve a compile error with GCC compiler in Fedora

    Hi guys

    I have been trying to get this code running using GCC compiler in Fedora . I get an error while compiling which I am not able to resolve. Can someone help me out to fix this error?

    Code:
    //This program calculates the cosine of different angles.
    
    #include<stdio.h>
    #include<math.h>
    
    #define PI 3.1416
    #define MAX 180
    
    main()
            {
             int angle;
             float x;
             float y;
             angle=0;
             printf("Angle  Cos(angle)\n\n");
    
             while(angle<=MAX)
                  { x=(PI/MAX)*angle;
                    y=cos(x);
                    printf("%15d    %13.4f\n",angle,y);
                    angle=angle+10;
                  }
            }
    [anishjp@anish C_programs]$ gcc cosine_function.c -o cosine_function
    /tmp/ccITJ2Tu.o: In function `main':
    cosine_function.c: (.text+0x37): undefined reference to `cos'
    collect2: ld returned 1 exit status

    Any help would be much appreciated!

    Regards,
    Anish

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to link with the math library

    gcc cosine_function.c -o cosine_function -lm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Also, for future reference, that's not really a compiler error, it's a linker error. You might want to read this.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Johannesburg, South Africa
    Posts
    3
    Thanks very much for helping me, I appreciate it much!

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Johannesburg, South Africa
    Posts
    3

    Thumbs up

    Quote Originally Posted by Salem View Post
    You need to link with the math library

    gcc cosine_function.c -o cosine_function -lm
    It worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to resolve this error in c++?
    By hr1212s in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2010, 02:47 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Can't resolve linker error!!!!
    By benshums in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 02:48 PM
  4. Replies: 3
    Last Post: 06-22-2005, 07:27 AM
  5. Resolve error in Windows Macro?
    By Adock in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 08:41 PM