我有一个简单的测试控制台应用程序。我尝试使用在simple.h文件中定义的函数nike2,实现是在simple.cpp中实现的。H和simple.cpp文件与主项目位于不同的目录中。
我已经在项目资源管理器的“头文件”中添加了simple.h,在“源文件”中添加了simple.cpp (我不确定是否需要)。
控制台应用程序:
#include "stdafx.h"
#include "..\..\simple.h"
int _tmain(int argc, _TCHAR* argv[])
{
nike2(5);
return 0;
}Simple.h
#include <cstdlib>
#include <iostream>
#ifndef MEMORY
#define MEMORY
int var;
int nike2(int f);
#endif /*MEMORY*/Simple.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;
}发布于 2012-08-07 22:06:37
Stdafx.h用于生成预编译头文件。它包含一些最标准和最常用的include。
这主要是为了加快编译过程,因为WinAPI是一个非常繁重的东西。
你也可以查看this question,它有一个更详细的答案。
发布于 2012-08-07 22:07:00
stdafx.h包含您不希望更改的内容的头文件。像标准库这样的东西都包含在stdafx.h中,所以它们只需要编译一次。您应该在需要的任何地方包含stdafx.h。如果您不想使用它,可以在项目首选项中将其关闭。
发布于 2012-08-07 22:07:34
魔法"StdAfx.h“是用来做什么的?
它用于Visual Studio中的预编译头。
您可以将其包含在您的.cpp文件中,或者为那些不需要包含所有Windows头的文件选择“不使用预编译头”。
https://stackoverflow.com/questions/11847716
复制相似问题