0
votes
//........Project for ABC.dll
//ABC.h
#pragma once
class ABC{
public:
    ABC(){}
private:
    std::vector<int> m_vector;
};

//ABC.cpp
#include "Stdafx.h"
#include "ABC.h"

//Stdafx.h
#include <vector>

Till today, I've skipped #include <standard-lib.h> in my headers by delegating it to Stdafx.h header.

It's never been a problem when I worked in a single project file.

Now I'm trying to add a new DLL project to gather shared codes in one project.

It compiled well and generated ABC.dll too.

Here's a problem. When another project that uses ABC.dll show compile error saying that std::vector does not exist.

//...........Another Project using ABC.dll
int main(){
    ABC abc;
}

Error C2039 'vector': is not a member of 'std'

To get it working, I had to include all the libraries in the consumer's Stdafx.h too.

Maybe I've been misusing the precompiled header.

I want to know whether the way I've been doing with the PCH was wrong or right.

If it's wrong, I would appreciate it if you suggest right ways of using PCH.

Thanks.

1
The last snippet is missing #include <vector>. Which is required to get the ABC.h to compile. You never realized this before because stdafx.h included it. Also note that the #include for vector in the first snippet does not do anything useful, it was already included. None of this actually have anything to do with precompiled headers.Hans Passant

1 Answers

0
votes

Your problems have nothing to do with precompiled headers. A good practice is to include all the stuff directly used by current file. This will prevent changes in includes of one header file from potentially requiring changes in includes in files that are using this header. vector needs to be included in ABC.h because it is directly used there. Otherwise you'll end up with endless struggling to figure out which headers needs to be included when including this particular library header.