tags:

views:

342

answers:

3

What does the obj file ctr1.o does in gcc compilier ?Why does the linker link this obj file whenever an executable is generated?

+1  A: 

Object files hold your compiled code, but are not in themselves executable. It is the job of the linker to take all the object files that make up a program, and join them into a whole. This involves resolving references between object files (extern symbols), checking that there is a main() entrypoint (for C programs), and so on.

Since each source file (.c or .cpp) compiles into a separate object file, which are then read by the linker, changes to a single C file mean only that can be re-compiled, generating a new object file, which is then linked with the existing object files into a new executable. This makes development faster.

UPDATE: As stated in another answer, the "crt.o" object files holds the C runtime code, which is assumed to be needed by most C programs. You can read the gcc linker options and find the --no-stdlib option, this will tell gcc that your particular program should not be linked with the standard C runtime files.

unwind
+2  A: 

I'm not sure to understand your question but I guess you are referring to 'crt1.o' in the GCC package.

The crt is one of the base packages of the libc which provides basic functionality to access the computer. IIRC it contains methods like 'printf' and such.

That's why it is often even included in the most basic C applications.

Kosi2801
crt0.o certainly does not contain printf.printf is typically located in libc.a
Tom
+6  A: 

I think it contains very basic stuf (crt stands for C run time) like setting up argv and argc for your main function etc ... Here is a link with some explanation

If you don't want it, because you are writing a tiny bootloader for example, without any bit of the libc, you can use the --no-stdlib options to link your program. If you go this way, youwill also need to write your own linker script.

shodanex
WARNING: -nostdlib option in GCC 4.1.2
psihodelia