Why main must be declared as if it has external linkage?
Why it should not be static?
what is meant by external linkage??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because you link the startup files to your program, which contains (usually) assembler code that calls your main. If main were static, that code wouldn’t be able to call main.
external linkagemeans that other so-calledtranslation-unitscan see your symbol declared extern in its own translation-unit. So, your main is extern, and it will have an entry in its translation-units symbol table that states its address. Other translation-units will then be able to jump to that address when they want to call main.static linkagemeans your symbol is strictly translation-unit local. This means othertranslation unitswill not be able to see that symbol. Thus, symbols with static linkage can occur in different translation units multiple times, and they won’t clash with each other because they are local.Edit: Generally, files generated by the compiler from translation units are specific to that particular compiler. For gcc on linux, often the ELF object format is used. You can view its symbol table using
readelf -sW <file>.o(simple test-file below):test.c
Here is the output of readelf:
You see the main function, and a static foo function, called by main. Also there is a function called which is not defined in the file, but which is defined in another object file. As the object file wasn’t finally linked yet, the functions don’t have final addresses assigned yet. After the final link, these will be arranged into the executable and will have addresses assigned. The object file has entries for calls to not-yet defined functions, so that when the file is linked, those call instructions can have the final addresses stored (
readelf -r <file>.o):