Basic Math Problem. Undefined Math Functions

This is a discussion on Basic Math Problem. Undefined Math Functions within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> int main(void) { double x; printf("Enter a number: "); scanf("%lf",&x); printf("\n\nOriginal value: %lf",x); printf("\nCeil: %lf",ceil(x)); ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    Basic Math Problem. Undefined Math Functions

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
    	double x;
    	
    	printf("Enter a number: ");
    	scanf("%lf",&x);
    	
    	printf("\n\nOriginal value: %lf",x);
    	printf("\nCeil: %lf",ceil(x));
    	printf("\nFloor: %lf",floor(x));
    	
    	if (x >= 0)
    		printf("\nSquare root: %lf",sqrt(x));
    	else
    		printf("\nNegative Number");
    		
    	printf("\nCosine: %lf\n",cos(x));
    	return 0;
    
    }
    A very simple example into using some of the Math functions found in math.h unfortunately gcc is complaining about the functions being undefined

    /tmp/ccRRA7p2.o(.text+0x57): In function `main':
    : undefined reference to `ceil'
    /tmp/ccRRA7p2.o(.text+0x83): In function `main':
    : undefined reference to `floor'
    /tmp/ccRRA7p2.o(.text+0xc1): In function `main':
    : undefined reference to `sqrt'
    /tmp/ccRRA7p2.o(.text+0xfb): In function `main':
    : undefined reference to `cos'
    collect2: ld returned 1 exit status
    The compile command if this helps

    gcc math.c -o math -W -Wall -ansi
    Bit puzzled to why I am getting the errors since ive included the header file.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try this:
    Code:
    gcc math.c -o math -W -Wall -ansi -lm
    You just need to link in the math library

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  2. math problem
    By Chaplin27 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-09-2005, 02:37 PM
  3. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Undefined Reference Problem
    By esilja in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2001, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21