Monday, May 24, 2010

Need a clarification about #pragma directive in C++?

When I compiled and ran the below code in Turbo-C++ compiler it is running fine. But when I tried to run the same code in VC++6.0 compiler it is giving two warnings as "Unknown Pragma"... it is not recognizing the key words Startup and exit. That is why the warnings a coming. Can you pls mention the equivalent CPP code which can run on a VC++6.0 compiler.





The code is as below:





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void f1();


void f2();


#pragma startup f1


#pragma exit f2





main()


{


cout%26lt;%26lt;"EXECUTING MAIN \ n \ n";


return(0);


}








void f1()


{


cout%26lt;%26lt;"EXECUTING F1 \ n \ n";


}





void f2()


{


cout%26lt;%26lt;"EXECUTING F2 \ n \ n";


}








OUTPUT:


EXECUTING F1





EXECUTING MAIN





EXECUTING F2

Need a clarification about #pragma directive in C++?
Ok, this is what happens in Turbo C++. The statement #pragma startup f1 tells the preprocessor that in this program the special module name startup, usually predefined as pointing to the library startup routine; is to have the function name f1 expanded into it at the end of the function. So, when the program is loaded and about to begin execution, before it begins main; it finds and executes f1. The same happens with the f2 function and exit.


What you need to know to make this work is what pragma name the VC++ 6.0 compiler uses instead of startup. There are three ways to find out. From worst to best: 1) Guess and try, it could take thirty seconds or thirty years. 2) Search out an answer from the included header files. Probably get done in a few days at most. 3) Quickest and best is ask Microsoft by using the search facilities at the link below. I hope this helps you out.


No comments:

Post a Comment