VC的预编译头文件如何使用?
大家都知道,使用VC自动生成带预编译头文件的工程,提高编译速度节省时间神马的。我的疑问是:
1、例如iostream、vector这样的库文件,我几乎所有的cpp文件都会用到,所以我就把它们写到stdafx.h中,用于预编译,这样可以吗?
//stdafx.h#pragma once#include <cmath>#include <iostream>#include <vector>using namespace std;
//Eg.h#pragma onceclass A{ vector a;}
//Eg.h#pragma onceclass A{ vector a;}
[解决办法]
等价于
#ifndef _HEADER
#deinfe _HEADER
#endif
[解决办法]
>2、在同一工程中我定义了一个类,其声明文件为Eg.h,实现文件为Eg.cpp,那么stdafx.h是应该包含在Eg.cpp中的,但是如果在Eg.h中声明的类中使用到了vector对象,那应该怎么办呢?
照你所说,Eg.cpp应该是这样
#include "stdafx.h"
#include "Eg.h"
因为在包含Eg.h之前已经包含了stdafx,h,所以即使Eg.h用了vector,编译器也能认。