gpt4 book ai didi

c++ - Visual Studio 包含路径优先级

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:28 26 4
gpt4 key购买 nike

在花了几个小时试图弄明白为什么我在调用 afxtempl.h 中的 new 时遇到编译错误,用尽了通过谷歌找到的其他解决方案后,我意识到有一个本地 new.h 导致了问题(当我说本地,我的意思是它在构成解决方案的几十个项目之一中)。

这花了一段时间才找到,因为我只是模糊地熟悉代码库,没想到会找到一个新的.h,还因为我没想到这会干扰尖括号

#include <new.h> 

在 afxtempl.h 中。我一直觉得

#include <file.h> 

意思是先在系统中查找。

查看我所有的违规项目的包含路径(除了我的机器上实际上不存在的几个本地路径)是:

$(VC_IncludePath)
$(WindowsSDK_IncludePath)

那么给定 new.h 是(曾经)在一个不同的项目中,它是如何被选中的?

此时我应该补充一点,该解决方案刚刚从 VC++ 6 迁移到 VS 2013。

顺便说一句,局部的 new.h 没有#define _INC_NEW,也没有定义/声明新的。这个名字只是巧合,与内存分配无关。

我很好奇是否有一些我不知道的包含设置,并且不希望在未来有同样令人沮丧的几个小时。

谢谢。

最佳答案

首先在每个项目的附加包含目录(C/C++ - General - Additional Include Directories)中搜索尖括号包含。这是 documented行为:

Angle-bracket form

The preprocessor searches for include files in this order:

  • Along the path that's specified by each /I compiler option.

  • When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.

...

For example, the command

CL /ID:\MSVC\INCLUDE MYPROG.C

causes the preprocessor to search the directory D:\MSVC\INCLUDE\ for include files such as STDIO.H.

您的项目目录似乎已添加到其他包含目录列表中。您可以在项目设置的 C/C++ - CommandLine 选项卡中查看传递给编译器的所有命令行选项。

这是 C/C++ 编译器的通常工作方式,例如参见 g++文档:

#include <file>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).

所以,如果您想保留额外的包含目录列表,恐怕没有办法改变这种行为。作为解决方法,您可以将包含文件放在项目根目录的子目录中,并将项目根目录添加到其他目录列表中,因此您的包含看起来像 #include "project/new.h",这将需要代码重组虽然。

关于c++ - Visual Studio 包含路径优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28715606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com