Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to include stdafx.h, when it is 1 directory up?

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:

error highlighting

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?

like image 714
dan-O Avatar asked Jan 15 '15 18:01

dan-O


Video Answer


1 Answers

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 have utils/a.h and want to use it from foo/bar/b.h you can write #include "utils/a.h" instead of #include "../../utils/a.h". And if you have stdafx.h in project root you can include it with just #include "stdafx.h".

like image 171
dewaffled Avatar answered Sep 25 '22 03:09

dewaffled