c++ - 如何检查 MS 编译器是否会编译我的源代码

标签 c++

伙计们,我在 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/

相关文章:

c++ - 为什么 for-loop 不是编译时表达式并且扩展的 constexpr 允许在 constexpr 函数中进行循环

C++用另一个编辑二进制文件

c++ - 合并多个 csv 文件

c++ - 无法访问 header

c++ - 为什么一个类不能对函数和数据成员具有相同的名称?

c++ - 刷新 QSqlDatabase 对象的设置

c++ - 从类到 double 的隐式转换

c++ - T&& 如何运作?

c++ - 基类是依赖类型时是否存在缺陷

c++ - 在 C++ 中重载 [] 运算符以支持自定义 set 函数