c++ - 使用头文件和 StdAfx.h

标签 c++ visual-studio-2010

我有一个简单的测试控制台应用程序。我正在尝试使用 simple.h 文件中定义的函数 nike2,实现在 simple.cpp 中。 simple.h 和 simple.cpp 文件都位于与主项目不同的目录中。

我在项目资源管理器中将 simple.h 添加到“头文件”并将 simple.cpp 添加到“源文件”(我不确定是否需要)

控制台应用程序:

#include "stdafx.h"
#include "..\..\simple.h"

int _tmain(int argc, _TCHAR* argv[])
{
nike2(5);
return 0;
}

简单.h

#include <cstdlib>
#include <iostream>

#ifndef MEMORY
#define MEMORY

int var;
int  nike2(int f);

#endif /*MEMORY*/

简单.cpp

#include <cstdlib>
#include <iostream>
#include "simple.h"


int  nike2(int f)
{
 return 0;
}

编译时出现错误:

Error   4   error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?   c:\c\simple.cpp 11  1   usingHeader

为什么?神奇的“StdAfx.h”有什么用?

更新

simple.cpp 现在看起来是这样,但仍然有同样的错误

#include "C:\c\usingHeader\usingHeader\stdafx.h"
#include <cstdlib>
#include <iostream>
#include "simple.h"


int  nike2(int f)
{
return 0;
}

最佳答案

Stdafx.h 用于制作预编译头文件。它包含一些最标准和最常用的 include

这主要是为了加快编译过程,因为 WinAPI 是一个非常的东西。

您还可以查看 this question ,它有更详细的答案。

关于c++ - 使用头文件和 StdAfx.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847716/

相关文章:

exception - 如何覆盖 Visual Studio Code Coverage 中的异常

visual-studio-2010 - 从 DLL 中去除特定符号

visual-studio - 在Visual Studio中停止鼠标选择

visual-studio-2010 - 限制 Visual Studio 2010 批量构建中的配置和平台组合

C# 提高程序效率?

c++ - Qt C++ 中 header 变量的 undefined reference

c++ - 如何检查 C/C++ 中是否存在函数?

c++ - 在映射中使用对作为键 (C++/STL)

c++ - 如何以微秒精度计算操作时间

c++ - 控制 C++ 析构函数和对象的 ivars 的顺序