Imagine the following source files:
src/StdAfx.h
src/moresrc/MyFile.cpp
In MyFile.cpp
, I need to include the precompiled header file StdAfx.h.
If I do this:
#include "../StdAfx.h"
then I will get the compiler error:
warning C4627: '#include "../stdafx.h"': skipped when looking for precompiled header use
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
So that doesn't work. If I do just
#include "StdAfx.h"
then that compiles correctly, but I get the annoying error highlighting in Visual Studio 2013 that looks like this:
So, like I said, this compiles, but it is annoying.
How can I include "StdAfx.h" without the underlining, but so that it will compile?
You can add
$(ProjectDir)
(project root directory) to directories list in project options (C++ -> General -> Additional Include directories). Then you'll be able to specify paths relative to project root in includes. For instance if you haveutils/a.h
and want to use it fromfoo/bar/b.h
you can write#include "utils/a.h"
instead of#include "../../utils/a.h"
. And if you havestdafx.h
in project root you can include it with just#include "stdafx.h"
.