4

I believe I am dealing with some kind of corrupted library. I wrote the following code:

//helloworld.c                                                                  

#include <stdio.h>

int main()
{
  printf("Hello, world!\n");

  return 0;
}

And compiled it with the following command:

gcc helloworld.c

The errors I got were many. Here is a sample:

/usr/include/stdio.h:510: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:514: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__wur’
/usr/include/stdio.h:517: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__THROW’
/usr/include/stdio.h:524: error: expected declaration specifiers before ‘__END_NAMESPACE_C99’
/usr/include/stdio.h:534: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:540: error: storage class specified for parameter ‘getchar’
/usr/include/stdio.h:541: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:553: error: storage class specified for parameter ‘getchar_unlocked’
/usr/include/stdio.h:563: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:567: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:576: error: expected declaration specifiers or ‘...’ before ‘FILE’
/usr/include/stdio.h:576: error: storage class specified for parameter ‘putc’
/usr/include/stdio.h:582: error: storage class specified for parameter ‘putchar’
/usr/include/stdio.h:583: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:604: error: expected declaration specifiers or ‘...’ before ‘FILE’
/usr/include/stdio.h:604: error: storage class specified for parameter ‘putc_unlocked’
/usr/include/stdio.h:605: error: storage class specified for parameter ‘putchar_unlocked’
/usr/include/stdio.h:612: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:615: error: expected declaration specifiers or ‘...’ before ‘FILE’
/usr/include/stdio.h:615: error: storage class specified for parameter ‘putw’
/usr/include/stdio.h:619: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:632: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__wur’
/usr/include/stdio.h:633: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:661: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘getdelim’
/usr/include/stdio.h:671: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘getline’
/usr/include/stdio.h:677: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:688: error: storage class specified for parameter ‘puts’
/usr/include/stdio.h:695: error: expected declaration specifiers or ‘...’ before ‘FILE’
/usr/include/stdio.h:695: error: storage class specified for parameter ‘ungetc’
/usr/include/stdio.h:702: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
/usr/include/stdio.h:708: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
/usr/include/stdio.h:710: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:732: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite_unlocked’
/usr/include/stdio.h:737: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:747: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:752: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:753: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:771: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ftello’
/usr/include/stdio.h:785: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:796: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:808: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:821: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:823: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:824: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:829: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:830: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:834: error: expected declaration specifiers before ‘__BEGIN_NAMESPACE_STD’
/usr/include/stdio.h:840: error: expected declaration specifiers before ‘__END_NAMESPACE_STD’
/usr/include/stdio.h:846:30: error: bits/sys_errlist.h: No such file or directory
/usr/include/stdio.h:856: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:866: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/stdio.h:872: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:878: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__THROW’
/usr/include/stdio.h:906: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:910: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:913: error: expected ‘)’ before ‘*’ token
/usr/include/stdio.h:936: error: expected declaration specifiers before ‘__END_DECLS’
helloworld.c:10: error: old-style parameter declarations in prototyped function definition
helloworld.c:10: error: expected ‘{’ at end of input

FWIW, I'm running Ubuntu 11.10; a fairly recent install. I presume there was something I failed to do when installing, but I have never had this problem before. I would appreciate any advice, and can answer any questions you may have to help me clarify/narrow down the problem. Thanks again!

EDIT: Thanks for your advice, folks, but I am still lost. I've uninstalled/reinstalled libc-dev-bin, libc6-dev, gcc and dpkg. It's been recommended that I ask the package manager where crt1.o and crti.o are, but I'm afraid I don't know how to do this.

Is there a surefire, brute-force way to completely rebuild all the libraries and headers, short of reinstalling Ubuntu? If there isn't, I may just decide to reinstall.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
Frank Harris
  • 305
  • 1
  • 6
  • 16
  • 4
    This does not happen on a clean install of 11.10 (just checked). I'd remove and re-install gcc and the headers. – Brian Roach Feb 28 '12 at 18:03

1 Answers1

8

You almost have to have a broken set of header files. To confirm, try the following:

extern int printf(char *, ...);
int main() {
    printf("hello world!\n");
    return 0;
}

If this compiles cleanly, somehow stdio.h or one of the files it includes has become corrupted. If the output binary runs and produces the expected output, then at a first glance the libraries themselves would look ok.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
  • With the code you posted, I get the following: hellotest.c: In function ‘main’: hellotest.c:2: warning: return type of ‘main’ is not ‘int’ /usr/bin/ld: cannot find crt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No such file or directory collect2: ld returned 1 exit status I changed main to int and added return 0; to the end and got: /usr/bin/ld: cannot find crt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No such file or directory collect2: ld returned 1 exit status So what is the proper fix for this? I was already pretty sure of the problem. – Frank Harris Feb 28 '12 at 18:37
  • The first warning is from the compiler, saying that I've got a slightly incorrect declaration of main. (I'll fix that shortly.) The rest imply to me that, in addition to broken headers, you seem to have broken libraries as well. I don't really know what to suggest. Can you force-reinstall some of the base libc packages, and maybe your compiler as well? – Dale Hagglund Feb 29 '12 at 10:31
  • @FrankHarris: A few more thoughts. Something is very odd if you can't find `crt1.o`, but it explains why other programs work. They've already been linked against `crt1.o` either when the packages they came from were built or when you compiled them before this started happening. Have you upgraded your system, or installed / upgraded any packages recently? Try asking the package manager which packages provides `crt1.o` and `crti.o`, and maybe think about reinstalling that. – Dale Hagglund Feb 29 '12 at 10:45
  • Accepted...If perhaps many many months too late. It turned out I added something to $PATH at the end of the previous semester, that basically provided an extra crt1.o. I removed it from my path and the problem was solved. – Frank Harris Jun 20 '12 at 15:45