我有一个简单的测试控制台应用程序。我正在尝试使用 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/