Sunday, June 9, 2013

Modifications for Micosoft Visual C++ as code portable with with different compilers(TC,GCC...)

   
                                                                                                                     
Program 1)       #include "stdafx.h"                       
                         int main()
                        {
                          printf("Hello World\n");
                          return 0;
                         }
                         If we compile(ctrl+F5) above code(Program1) in MS Visual C++ compiler means it will work fine.

Program 2)      
                         int main()
                        {
                          printf("Hello World\n");
                          return 0;
                         }
                         If we compile(ctrl+F5) above code(Program1) in MS Visual C++ compiler means it will show the following error
"fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?  "

MS Visual C++ compiler default contains " #include "stdafx.h" " included in source file,
if we work with this header no problems are arrive, but this is not a code portability how means, if we run the
same program in another compiler(Turbo C,GCC) means it will give error due to that header file.
So it is better to remove that header file by changing appropriate modifications in compiler settings,

For that you need modify 2 things
1) Change fromUse the  Precompiled Headers    to Not Using Precompiled Headers
  1)Goto the "solution explorer" menu
  2)Right click on the project name-->properties-->configuration properties-->C/C++-->Precompiled      
       header
      -->Click on Create/Use precompiled header(From right side)-->Click on triangular button  -->
         Not Using Precompiled Headers-->then apply-->Then Ok.

   3)Screen shots for above

a)


 b)


c)


 2) Change from  C++    to C  compiler:
       By default this compiler this compiler assumes our code is written C++ so that it will compile,
     so we need to change this one from C++ to C by following the below instructions

  1)Goto the "solution explorer" menu
  2)Right click on the project name-->properties-->configuration properties-->C/C++-->Advanced
      -->Click on Compile as (From right side)-->Click on triangular button  -->
         select the Compile as C Code (/TC)-->then apply-->Then Ok.

   3)Screen shots for above

 


 b)

 c)
 




Now your done.Your code contains portability with different compilers.

Reference: For more info refer let us" C solutions" text book  from sri Yashavant P. Kanetkar.





No comments:

Post a Comment