C++ 编译器找不到我的头文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16883026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:43:50  来源:igfitidea点击:

Compiler Can't Find My Header?

c++visual-studio-2012compiler-constructionheader

提问by Rome_Leader

Despite hash-including it, my project cannot seem to find its header file. I have included a screenshot because I think it's the most effective way to showcase my problem:

尽管包含散列,但我的项目似乎找不到它的头文件。我提供了一个屏幕截图,因为我认为这是展示我的问题的最有效方式:

ERROR MESSAGE

错误信息

1>c:\users\wood\desktop\old programs\locker.cpp(2): fatal error C1083: Cannot open include file: 'Locker.h': No such file or directory

1>c:\users\wood\desktop\oldprograms\locker.cpp(2):致命错误 C1083:无法打开包含文件:'Locker.h':没有那个文件或目录

As you can see, I have included Locker.h at the top of the file, and it is also listed under 'Header Files' on the left.

As you can see, I have included Locker.h at the top of the file, and it is also listed under 'Header Files' on the left.

Any ideas as to what's gone wrong? I've tried: 1) Cleaning the project and rebuilding it. 2) Creating a brand new, identical project.

关于出了什么问题的任何想法?我试过: 1)清理项目并重建它。2) 创建一个全新的、相同的项目。

Thanks!

谢谢!

回答by Chuck Rittenhouse

Ok, so let's say I have a program with source code located in C:\Users\Chuck\desktop\programming, but it requires a header file from C:\Users\Chuck\desktop\headers. I can #include the header all I want, but if I don't include the absolute file name, the compiler will refuse to look in the right place.
With you, however, the easiest solution you might try would be to include the absolute path to the header file. So if it's located at

好的,假设我有一个源代码位于 C:\Users\Chuck\desktop\programming 的程序,但它需要来自 C:\Users\Chuck\desktop\headers 的头文件。我可以随心所欲地#include 标头,但是如果我不包含绝对文件名,编译器将拒绝查找正确的位置。
但是,对于您来说,您可能尝试的最简单的解决方案是包含头文件的绝对路径。所以如果它位于

    C:\Users\Wood\desktop\old programs\locker.h

you might try including that instead of simply "locker.h"
Code::Blocks gets real fidgety with me if I don't include the header file in the same directory. Normally I can work around that using this same method. Try it, see if it works.


如果我不将头文件包含在同一目录中,您可能会尝试包含它而不是简单的“locker.h” Code::Blocks会让 我感到烦躁。通常我可以使用相同的方法解决这个问题。尝试一下,看看它是否有效。

回答by Kate Gregory

Double click locker.h in the Solution Explorer to open it, then hover over the tab to see the full path. Switch to the file that is trying to include it and hover over the tab to see the full path. If the two files are not in the same folder, you will have to do one of these things:

在解决方案资源管理器中双击 locker.h 将其打开,然后将鼠标悬停在选项卡上以查看完整路径。切换到尝试包含它的文件并将鼠标悬停在选项卡上以查看完整路径。如果这两个文件不在同一个文件夹中,则必须执行以下操作之一:

  • adjust your "C++ Include Directories" property (type "include" in the Quick Launch area at the top right of the screen to find that property
  • use a relative path eg #include "..\headers\Locker.h"
  • copy the file to the same folder as the .cpp file, remove it from the project (so the old location isn't important any more), delete that old copy, then use Add Existing to add the newly-copied version (that is in the same folder as the .cpp) to the project again
  • 调整您的“C++ 包含目录”属性(在屏幕右上角的快速启动区域中键入“include”以找到该属性
  • 使用相对路径,例如 #include "..\headers\Locker.h"
  • 将文件复制到与 .cpp 文件相同的文件夹中,将其从项目中删除(因此旧位置不再重要),删除该旧副本,然后使用 Add Existing 添加新复制的版本(即在与 .cpp 相同的文件夹中)再次复制到项目中

One of these should do the trick.

其中之一应该可以解决问题。