Does g++ really need to compile main, if c++ code is involved?

rbmj rbmj@verizon.net
Thu Mar 20 18:27:00 GMT 2014


(Accidentally didn't reply all the first time, sorry)

On 20-Mar-14 12:38, Jonathan Wakely wrote:
> On 20 March 2014 15:50, Eric Wolf wrote:
>>
>> I really think, static initializations and exceptions are the problem,
>> because if I placed main in a C source file and compile it with gcc,
>> how are the initializations done? How are uncaught exceptions handled?
>
> Using the DWARF-based exception mechanism the compiler looks up the
> stack for a suitable catch handler and if none is found (either
> because the higher stack frames are C++ functions but don't catch the
> exception, or because they are not C++ functions at all) then
> std::terminate() is called at the throw site.
>
> So I believe an uncaught exception will terminate the process
> irrespective of what language main() is written in.

I agree with this.

>
> Global ctors/dtors are a separate issue, I'm not sure how they're
> handled if main() isn't written in C++.
>

Shouldn't these be handled in crt*.o by being placed in .dtors/.ctors? 
As long as these are linked in by gcc/g++ then everything should be fine 
(as a matter of fact the C compiler lets you hook into this 
functionality using attributes IIRC, so it needs to provide that as 
well...).



More information about the Gcc-help mailing list