5

想象一下以下源文件:

src/StdAfx.h
src/moresrc/MyFile.cpp

MyFile.cpp中,我需要包含预编译的头文件 StdAfx.h。

如果我这样做:

#include "../StdAfx.h"

然后我会得到编译器错误:

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?

所以这行不通。如果我只是

#include "StdAfx.h"

然后编译正确,但我在 Visual Studio 2013 中突出显示恼人的错误,如下所示:

错误高亮

所以,就像我说的,这可以编译,但很烦人。

如何在没有下划线的情况下包含“StdAfx.h”,但这样它才能编译?

4

1 回答 1

5

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".

于 2015-01-15T19:40:37.573 回答