Operating System - HP-UX
1825161 Members
2258 Online
109679 Solutions
New Discussion юеВ

main function not executed

 
Jorge Mora
Occasional Contributor

main function not executed

Hello,
I compile a program with aCC, but when it runs
it skips the main() completely: I put a print
statement at the very beginning of the main()
function but when I execute the program the
print statement is never called as all other
initializations and it looks like starts running some other piece of code which shouldn't be running unless interaction with
the user.
5 REPLIES 5
Mark Grant
Honored Contributor

Re: main function not executed

Just in case this is the post that doesn't get removed, I posted this in your other thread.

This can not behappening. What probably is happening is that you have done your printf but not flushed standard output yet with "fflush()" or even "\n" which might explain why you can't see it.

Can I suggest you post the code snippet so we can have a look.
Never preceed any demonstration with anything more predictive than "watch this"
Vitek Pepas
Valued Contributor

Re: main function not executed

main() function is always executed.
There may be nothing to print (check arguments of 'print' statement), the statement itself can be skipped in the flow of the program, standard output can be redirected, but main() has to be executed.
A. Clay Stephenson
Acclaimed Contributor

Re: main function not executed

My best guess is that some of the library functions were written in C++. There are some special rules for mixing C and C++ and one of those is that the main() should be written in C++. Try changing your main to C++ and see if you now see your fprintf's. There are some rules for using a main written in C and linking with C++; see the otherlangs.html file that is part of the aCC on-line documentation.
If it ain't broke, I can fix that.
Jorge Mora
Occasional Contributor

Re: main function not executed

Thank you all who responded,

The problem was the main() is in C while the
rest of the code is in C++. I followed all
the recommendations of mixing C and C++
objects, and this was working before I
upgraded to Oracle9i (I guess one of the
patches needed by oracle caused this
problem). Before I had to called _main()
in order to create all static objects and I
had control where in the code I called
_main(), but now I gues HP decided that an
explicit call to _main() was not necessary
anymore, so all the creation of static
objects is done automatically as it should
be.
Here is a small example:

file main.c:
int main()
{
/* Initializing communications */
...
/* call app */
func1();
}

file Obj1.C:
/* Globals */
Object1 *obj1 = new Object1();

void func1()
{
...
}

As this example shows the object 'obj1' is
created before even calling main() because
it is a global object. In my application,
the problem was that the constructor of
'Object1' tries to communicate to oracle
and the main application, but the
communication is initialized in main().

In order for this to work properly I changed
the file 'Obj1.C' to:
/* Globals */
Object1 *obj1;

void func1()
{
/* Create global objects */
obj1 = new Object1();
...
}

Now 'obj1' is created when calling func1(),
right after main() already initialized
communications with the main application.

Hope I was able to explain all of this well.

Thanks again for the all the help I got.
A. Clay Stephenson
Acclaimed Contributor

Re: main function not executed

Good. That was why I suggested that you convert your baby test program to C++ and link it with your libraries. That would have instantly revealed the problem. I was rather sure that this was your problem and if you are not aware of how linking with global objects work, it can send you down really crazy paths.
If it ain't broke, I can fix that.