News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

Having a C main function

Started by ben2ong2, October 06, 2006, 10:58:25 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Darren Smith)

...

No you do not need a c++ main program. However the streams library intialises
several static members. If these are not initialised prior to a write to cout
for example then the program will dump.

You can get around this by calling the function _main(); as one of the first calls as part of your c main program. This will initialise the streams library.


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 7 May 93

That advice is VERY system-specific.

The C++ language specification does not discuss writing the main program
in any language other than C++.

Some C++ implementations require that main be written in C++.

Some C++ implementations allow main to be written in C, as long as it is
compiled with with the vendor's C++ compiler in "C" mode.

Some C++ implementations don't care what language or compiler is used
for the main program, but require the vendor's linker to be used.

Here is a portable solution which allows the main program to appear to
be written in C:  First, rename main.  Let's assume we change the name
of main written in C to be C_main.  Then do the following in C++:

   extern "C" int C_main(int, char**); // it is a C function

   int main(int argc, char** argv)
   {
       // static constructors will be called first
       return C_main(argc, argv);
       // static destructors will be called afterward
   }

Compile this as a normal C++ main program and everything should be OK.

If your implementation allows an extra environment parameter to main,
add the third parameter to C_main and to main.

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login