C program for GCC and MS Visual Express C++ works only for GCC

I'm trying to make my school assignment to work in both GCC and MS VS enviroments, but for some reason, it fails to compile in MS VS...

The errors are:

warning C4627: '#include ': skipped when looking for precompiled header use - Add directive to 'Stdafx.h' or rebuild precompiled header

or

unexpected #endif (the one after #include "Stdafx.h")

or

When I put the "Stdafx.h" header to first line, it behaves like there's no stdio and everything (HANDLE, int, etc. ) is illegal declaration.

#include <stdio.h>
#include <stdlib.h>

#ifdef _MSC_VER
    #include "Stdafx.h" 
    #include <windows.h>
#endif // _MSC_VER

#ifdef __GNUC__
    #include <unistd.h>
#endif  // __GNUC__

#ifdef _MSC_VER

int main ()
{       
    printf("___MS VS Studio/Express compiler___n");

    /*some stuff here*/ 
    return 0;
 }
#endif  //_MSC_VER

#ifdef __GNUC__ 

int main()
{
    printf("___GCC compiler___n");
    /*some other stuff here*/
     return 0;
 }
 #endif // __GNUC__    

It works fine on GCC and I suspect it has something to do with the #includes in #ifdef conditions in MS VS, but I dunno how to do it correctly..

Can anyone please correct me on how to make this work properly? Any useful advice welcome, thanks!


Your file should start like below, the "stdafx.h" file must be included first. This is required by Visual Studio's "precompiled headers" feature.

#ifdef _MSC_VER
    #include "Stdafx.h" 
    #include <windows.h>
#endif // _MSC_VER

#include <stdio.h>
#include <stdlib.h>


#ifdef __GNUC__
    #include <unistd.h>
#endif  // __GNUC__

If this doesn't workmtry to do launch the Build - Rebuild Solution command.

If this doesn't work you can remove the precompiled header like this:

  • Launch the Project - Properties command
  • In the C/C++ - Precompoiled Headers tab click on "Precompiled Headers" and select "Not Using Precompiled Headers".
  • 链接地址: http://www.djcxy.com/p/73590.html

    上一篇: 理解一个不常见的主要参数

    下一篇: 用于GCC和MS Visual C ++的C程序仅适用于GCC