【问题标题】:C program for GCC and MS Visual Express C++ works only for GCCGCC 和 MS Visual Express C++ 的 C 程序仅适用于 GCC
【发布时间】:2016-08-07 13:20:47
【问题描述】:

我正在尝试让我的学校作业在 GCC 和 MS VS 环境中工作,但由于某种原因,它无法在 MS VS 中编译...

错误是:

警告 C4627:“#include”:在查找预编译标头使用时跳过 - 将指令添加到“Stdafx.h”或重建预编译标头

意外的#endif(#include "Stdafx.h" 之后的那个)

当我将“Stdafx.h”标题放在第一行时,它的行为就像没有 stdio 并且所有内容(HANDLE、int 等)都是非法声明。

#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__    

它在 GCC 上运行良好,我怀疑它与 MS VS 中 #ifdef 条件中的 #includes 有关,但我不知道如何正确地做到这一点..

任何人都可以纠正我如何使其正常工作吗? 欢迎任何有用的建议,谢谢!

【问题讨论】:

  • 请出示您的“Stdafx.h”文件
  • // stdafx.cpp : 仅包含标准的源文件包含 // project.pch 将是预编译的头文件 // stdafx.obj 将包含预编译的类型信息 #include " stdafx.h" // TODO:在 STDAFX.H 中引用您需要的任何其他头文件 // 而不是在此文件中

标签: c visual-studio gcc


【解决方案1】:

您的文件应该像下面这样开始,必须首先包含“stdafx.h”文件。这是 Visual Studio 的“预编译标头”功能所要求的。

#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__

如果这不起作用,请尝试启动 Build - Rebuild Solution 命令。

如果这不起作用,您可以像这样删除预编译的标头:

  • 启动 项目 - 属性 命令
  • C/C++ - Precompoiled Headers 选项卡中,单击“Precompiled Headers”并选择“Not Using Precompiled Headers”。

【讨论】:

  • 它仍然无法编译,出现很多错误,例如“错误 C2275: 'HANDLE' : 非法使用这种类型作为表达式”.. 代码应该是正确的,它可以在 MS 中编译E 当我不使用#ifdef 宏时..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多