伙计们,我在 VS 中尝试做类似的事情:
#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
最佳答案
由于 MSVC 预编译 header 的工作方式,您不能在 stdafx.h 周围放置条件。一旦找到 stdafx.h(并且通常要求 #include "stdafx.h"
成为文件中的第一行),它基本上会用预编译的头文件内容替换所有内容,所以就好像你从来没有写过 #if _MSC_VER
并且有一个额外的 #endif
。
两种解决方案:
1) 不要在你的项目中使用预编译头文件。您仍然可以使用 stdafx.h 来包含您需要的所有 header ,但编译速度会很慢。
2) 将条件编译放在stdafx.h文件中。
(摘自here)
关于c++ - 如何检查 MS 编译器是否会编译我的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8152388/