crt0.o and crt1.o -- What's the difference?
Asked Answered
H

1

45

Recently I've been trying to debug some low-level work and I could not find the crt0.S for the compiler (avr-gcc) but I did find a crt1.S (and the same with the corresponding .o files).

What is the difference between these two files? Is crt1 something completely different or what? They both seem to have to do with something for 'bootstrapping' (setting up stack frame and such), but why the distinction?

Hildredhildreth answered 25/4, 2010 at 21:13 Comment(0)
T
52

Both crt0/crt1 do the same thing, basically do what is needed before calling main() (like initializing stack, setting irqs, etc.). You should link with one or the other but not both. They are not really libraries but really inline assembly code.

As far as I understand, crt comes in two "flavors"

  • crt1 is used on systems that support constructors and destructors (functions called before and after main and exit). In this case main is treated like a normal function call.
  • crt0 is used on systems that do not support constructors/destructors.
Topic answered 27/4, 2010 at 19:30 Comment(3)
What exactly are these constructors/destructors? Is this related to C++ or somethingelse? In my answer here It worked without crtbeginT.o and crtend.o which are the consructors and destructors.Craiova
I wrote it in my answer. Not it's not (directly) related to C++. It's functions that need to be called before main and after the end of the program. It's related to the OS the program is running on. But I'm not the best person to tell about that (never wrote such functions, others did it) and it would probably merit some answer of it's own.Topic
@kriss, thanks for the reply. I only saw it now because you did not use @ zboson. The destructor is really called after exit?Craiova

© 2022 - 2024 — McMap. All rights reserved.