2011-11-16 47 views
1

家伙,我试图在VS做这样的事情:如何检查是否MS编译器编译我的源代码

#ifdef _MSC_VER 
#include "stdafx.h" 
#endif 

,但我得到一个错误,告诉我:

C1020: unexpected #endif 

什么是做到这一点的正确方法?

编辑
/这是stdafx.h中的内容/

// stdafx.h : include file for standard system include files, 
// or project specific include files that are used frequently, but 
// are changed infrequently 
// 

#pragma once 

#include "targetver.h" 

//#include <stdio.h> 
//#include <tchar.h> 
#include <iostream> 
using std::cout; 
using std::cerr; 

// TODO: reference additional headers your program requires here 
+0

这是造成错误的确切代码段吗?你能显示“stdafx.h”的内容吗? –

+0

和targetver.h中是什么?只要你的头文件是正确的,原始代码也是正确的。尝试删除文件中的#ifdef ... #endif。汇编应该失败,并给你真正的错误。 – PierreBdR

+0

#PierreBdR好吧,我刚刚找到它。此代码编译,但在VS为了编译它必须关闭预编译头。不管怎么说,还是要谢谢你。 – smallB

回答

9

你不能把周围的stdafx.h的,因为这样MSVC预编译头的工作条件句。一旦stdafx.h被发现(通常需要#include "stdafx.h"成为文件的第一行),它基本上会替换所有内容,包含预编译的头文件内容,所以就好像你从未写过#if _MSC_VER并且有一个额外的#endif

两个解决方案:

1)不要在项目中使用预编译的头。你仍然可以使用stdafx.h来包含所有你需要的头文件,但是编译速度会很慢。

2)将条件编译放入stdafx.h文件中。

(取自here

+0

我绝对爱第二选择。谢谢! – smallB