Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know I'm not supposed to be posting problems related to programming here. But I couldn't think of any better place and it's quite relevant too. The problem is exactly what the title indicates: when I write a program in C language and it contains #include <math.h> it doesn't compile. I tried the -lm option in every variety I could find online. When I worked in GCC 4.7.3 I just had to add the -lm in the end and it worked, for example: gcc -o test test.c -lm. Another thing. In /usr/lib/gcc/i686-linux-gnu folder I found 4.7 4.7.3 4.8 4.8.1 folders. Is it some conflict the new version is having with old ones? And is it just GCC which has changed the procedures of -lm? Please suggest how I can compile programs containing the math library functions with GCC 4.8.1. And I am using Ubuntu 13.10 which I have recently updated from 12.10. The earlier version of GCC was 4.7.3. Oh by the way, this is the usual output I get when trying to compile:

$ gcc -o test test.c -lm
test.c:1:19: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
               ^
compilation terminated.

A sample program that suffered from this follows:

#include <stdio.h>
#include <math.h>

int main( void )
{
   double amount;
   double principal = 1000.0;
   double rate = .05;
   int year;

   printf( "%4s%21s\n", "Year", "Amount on deposit" );

   for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate, year );

      printf( "%4d%21.2f\n", year, amount );
   }
   return 0;
}

And here's the information about the current version of my GCC:

$ which -a gcc
/usr/bin/gcc

$ gcc --version
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ ls -l /usr/include/stdio.h /usr/include/math.h
ls: cannot access /usr/include/stdio.h: No such file or directory
ls: cannot access /usr/include/math.h: No such file or directory

And there is no whitespace before the #. Also, no program containing the stdio.h directive showed such problem.

share|improve this question
    
Please update your question with the answers to the following: What version of Ubuntu are you using? How did you update gcc? –  i08in Mar 2 at 17:40
    
Done. it's 13.10 and yes, GCC updated when I upgraded my system. –  nos_feratu Mar 2 at 18:12
    
Same problem @souravc –  nos_feratu Mar 2 at 18:23
    
Done. Everything seems to work except #include <math.h> @EliahKagan –  nos_feratu Mar 2 at 18:48

1 Answer 1

up vote 1 down vote accepted

When you use the C standard library from the official GCC included in Ubuntu, the header files are located in /usr/include. When you ran

ls -l /usr/include/stdio.h /usr/include/math.h

the output indicated that header files are missing from that directory, including stdio.h and math.h (the two headers with which you seem to have had problems). And GCC's error message says it can't find header files.

Therefore, replacing missing header files will likely solve this problem. Searching the Ubuntu packages database reveals /usr/include/stdio.h and /usr/include/math.h are provided by the libc6-dev package. Therefore, reinstalling the libc6-dev package should fix the problem:

sudo apt-get update && sudo apt-get --reinstall install libc6-dev
share|improve this answer
    
Thanks! That worked. –  nos_feratu Mar 3 at 6:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.