Thread: Strange compiler error

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Strange compiler error

    When trying to compile a simple script with Gcc:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	double a = cos(360);
    	printf ("cos: %d\n\n", a);
    
    	double b = sin(360);
    	printf ("sin: %d\n\n", b);
    
    	double c = tan(45);
    	printf ("tan: %d\n\n", c);
    
    	return 0;
    }
    I get an error:

    /tmp/cckLVeVp.o: In function `main':
    trig.c.text+0x1b): undefined reference to `cos'
    trig.c.text+0x3f): undefined reference to `sin'
    trig.c.text+0x63): undefined reference to `tan'
    collect2: ld returned 1 exit status
    I compile via: gcc trig.c -o test, however when I compile using
    g++ trig.c -o test it works.

    Why is it compiling with c++ compiler and not c compiler since it is a c script ????

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It seems you are failing to link with the C standard library. Consult the docs on how to do that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And of course Elysia means the math library, not the standard library. To link the math library, add -lm to the end of your gcc command line.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    And of course Elysia means the math library, not the standard library. To link the math library, add -lm to the end of your gcc command line.
    Duh, how am I supposed to know of a compiler that keeps two separate libraries?
    I'm living in the current and don't need to type commands from the command line to compile or link to whatever-you-call-it libraries. I push a simple button and it works. That is today.
    Hmph.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, what compiler needs what switches depends on both the compiler and the library implementation. The glibc that is used with Linux has a separate math library (which has the benefit that it can be replaced with a different math library, should someone wish to do so).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM