1. 程式人生 > >PCH Warning: header stop cannot be in a macro or #if block.

PCH Warning: header stop cannot be in a macro or #if block.

 

在編寫標頭檔案時,遇到這麼一個warning:PCH Warning: header stop cannot be in a macro or #if block. An intellisense PCH file was not generated.

查詢後大概原因是這樣:

如果一個頭檔案在你的工程或解決方案中,沒有被任何其他.c/cpp檔案包含(include),那麼就很有可能獲得一個PCH warning。

If the header is not included by any cpp file in your project/solution, you could potentially get a PCH warning. This is because our intellisense engine works with translation units instead of individual files. A translation unit is essentially a .c/cpp file and all the header files that were included. When a header file, say x.h, is opened, VS searches the include graph and find a suitable c/cpp file that includes x.h use that as the translation unit for the intellisense engine. However, if x.h is not found in the include graph (ie. not included by any .c/cpp file in the project), then the intellisense engine will use the header file itself as the translation unit (eg. treating that header file as a .c/cpp file). In that case, the intellisense engine won’t be able to generate the iPCH due to the reason described by the error message itself. As you already know, you can work around this by including the problematic headers in a .c/cpp file.

解決:

在該cpp檔案的開頭加上#pragma once