-5
#include<stdio.h>
static int main()
{
   printf("foo");
   return 0;
}

the code gives error

nfo): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

what's the reason behind the error

kapil
  • 625
  • 12
  • 20
  • 1
    You are being downvoted because your question is not clear. What did you expect to get in the second piece code and what are you getting instead? Clarify what "gives error" mean and we may be able to help. – Christian Garbin Feb 22 '15 at 16:03
  • what error do you receive? – 4pie0 Feb 22 '15 at 16:05
  • 1
    Downvoted because it's unclear which language you're asking about, you didn't even specify what you expected to happen, the motivation behind putting `static` on `main`, and you didn't tell us the error message you are getting. – tenfour Feb 22 '15 at 16:13

3 Answers3

7

In C, static is the primary way of "hiding" implementation details. Marking a function or a variable static in C means restricting its visibility to the translation unit in which it is defined. Essentially, only functions inside the same C file can refer to them. Functions from other files or libraries have no way of accessing them.

Since function main needs to be accessed from your environment's start-up code (a piece of code that "bootstraps" your program execution) hiding it renders your program non-linkable: compiler tries to find main, but it is hidden, so the linker issues an error.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

It shows error because you have no correct main function in your program. main() cannot be static either in C nor C++. static storage specifier means that the function is visible only in this translation unit and C runtime environment cannot access it:

prog.c:3:12: warning: 'main' is normally a non-static function [-Wmain]
 static int main()
            ^
prog.c:3:12: warning: 'main' defined but not used [-Wunused-function]
/usr/lib/gcc/i586-linux-gnu/4.9/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
4pie0
  • 29,204
  • 9
  • 82
  • 118
1

static int main() is not correct signature in either of C or C++. Signature of main in C++

int main() { / ... / } 
int main(int argc, char* argv[]) { / ... / }  

in C

int main(void) { / ... / } 
int main(int argc, char* argv[]) { / ... / }    
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @T.C.; Does that mean the `static int main()` is valid? – haccks Feb 22 '15 at 16:16
  • I'm not sure about C; C++ explicitly labels declaring `main()` `static` ill-formed. – T.C. Feb 22 '15 at 16:18
  • @T.C.; I know. In both languages it is ill formed. – haccks Feb 22 '15 at 16:20
  • It's obviously intended to be ill-formed; I just can't find an obvious normative prohibition in the C standard, though I guess 5.1.2.2.1's two forms can be read as implying external linkage. – T.C. Feb 22 '15 at 16:23
  • 2
    The closest I can find is 6.7.4/4 which says `main` can't be declared `inline`. See also [Can the C main() function be static?](https://stackoverflow.com/questions/924890/can-the-c-main-function-be-static) –  Feb 22 '15 at 16:28
  • @tenfour Just a question of vocabulary, but the storage class is not part of the signature. I'm sure that the intent in C is that `main` have external linkage, but I can't find any text saying so either. (The requirements of `main` are one of the differences between C and C++.) – James Kanze Feb 22 '15 at 16:38