除非使用完整的相对路径,否则 Eclipse CDT 无法找到包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12399994/
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-09-19 18:58:26  来源:igfitidea点击:

Eclipse CDT Can't Find include file unless full relative path is used

c++eclipseincludeeclipse-cdt

提问by Tod

I've been using Eclipse for a while and this has me stymied. I created a library name ITS8410_LIB a few weeks ago and then let it sit while I worked on a supporting library called Reach_Lib. The directory include paths I added back when I first created the project work fine. Today I finished Reach_Lib and went to add its path to ITS8410_Lib. I used the project properties C++ settings to add a workspace path (like I've done many times before). I have only one build configuration. The path is shown in the image (the one highlighted in blue is the specific one I'm talking about in this question).

我已经使用 Eclipse 有一段时间了,这让我受阻。几周前我创建了一个名为 ITS8410_LIB 的库,然后在我开发一个名为 Reach_Lib 的支持库时让它保持不变。该目录包含我第一次创建项目时添加回来的路径工作正常。今天我完成了 Reach_Lib 并去添加它到 ITS8410_Lib 的路径。我使用项目属性 C++ 设置来添加工作区路径(就像我以前做过很多次一样)。我只有一种构建配置。路径显示在图像中(蓝色突出显示的是我在这个问题中谈论的特定路径)。

enter image description hereWhen I go to include the file in the source neither Eclipse (I see the yellow question mark) nor the compiler (I get a compiler error) can locate the file. If I add the a directory path using the full absolute path syntax the results are the same. I can modify the source code and specify the #include via a full relative path from the current directory, and it is found. Eclipse just won't use the path specified in the project settings. This same Reach_Lib has an associated unit test project and the path is specified with a workspace setting from there and it works fine.

在此处输入图片说明当我将文件包含在源代码中时,Eclipse(我看到黄色问号)和编译器(我收到编译器错误)都无法找到该文件。如果我使用完整的绝对路径语法添加目录路径,结果是相同的。我可以修改源代码并通过当前目录的完整相对路径指定#include,然后找到了。Eclipse 只是不会使用项目设置中指定的路径。这个相同的 Reach_Lib 有一个关联的单元测试项目,路径是从那里指定的工作区设置,它工作正常。

enter image description here

在此处输入图片说明

Directory paths that were added before today appear fine. Paths (there are two libs showing this behavior) I add today just seem to be ignored. I took a look at the compiler settings (see image below) and the path specified to the compiler appears to be OK.

在今天之前添加的目录路径看起来不错。我今天添加的路径(有两个库显示了这种行为)似乎被忽略了。我查看了编译器设置(见下图),为编译器指定的路径似乎没问题。

enter image description here

在此处输入图片说明

Running under Windows7 64 bit (but using a 32 bit version of Eclipse Indigo). I have tried:

在 Windows7 64 位下运行(但使用 32 位版本的 Eclipse Indigo)。我试过了:

  1. rebuilding indices
  2. Exiting and restarting Eclipse multiple times
  3. workspace and absolute file paths
  4. modifying the order of the include paths
  5. different .h include files in project
  1. 重建索引
  2. 多次退出并重新启动 Eclipse
  3. 工作区和绝对文件路径
  4. 修改包含路径的顺序
  5. 项目中不同的 .h 包含文件

Update

更新

I took a look into the .cproject file and while I don't know much about what these files are supposed to look like I think I have a problem. Maybe the question is: How do I tell Eclipse to rebuild the .cproject file? I appear to have two similar sections and Eclipse is getting confused. I can see different version numbers tagged as suffixes. It appears to me that the visual editors are letting me modify one section but the IDE and compiler only care about the other section. Here's the relevant portions

我查看了 .cproject 文件,虽然我不太了解这些文件应该是什么样子,但我认为我有问题。也许问题是:我如何告诉 Eclipse 重建 .cproject 文件?我似乎有两个相似的部分,而 Eclipse 越来越困惑。我可以看到标记为后缀的不同版本号。在我看来,可视化编辑器让我修改一个部分,但 IDE 和编译器只关心另一部分。这是相关部分

...

                <folderInfo id="nbeclipse.toolchain.nbl.rel.396284009." name="/" resourcePath="">
                    <toolChain id="nbeclipse.toolchain.nbl.rel.tc.1465290409" name="NetBurner Device Library" superClass="nbeclipse.toolchain.nbl.rel.tc">
                        <targetPlatform id="nbeclipse.toolchain.nbl.rel.tc.targetPlatform.283367649" isAbstract="false" name="NetBurner Module Library" superClass="nbeclipse.toolchain.nbl.rel.tc.targetPlatform"/>
                        <builder buildPath="${workspace_loc:/ITS8410_Lib/Release}" id="nbeclipse.toolchain.nbl.rel.tc.builder.2064025940" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Release ELF builder (MINGW)" superClass="nbeclipse.toolchain.nbl.rel.tc.builder"/>
                        <tool id="nbeclipse.toolchain.nbl.rel.tc.gcc.278195048" name="GNU C Compiler" superClass="nbeclipse.toolchain.nbl.rel.tc.gcc">
                            <inputType id="nbeclipse.toolchain.nbl.rel.tc.gcc.input.1060035859" name="GNU C Input" superClass="nbeclipse.toolchain.nbl.rel.tc.gcc.input"/>
                        </tool>
                        <tool id="nbeclipse.toolchain.nbl.rel.tc.gpp.767008127" name="GNU C++ Compiler" superClass="nbeclipse.toolchain.nbl.rel.tc.gpp">
                            <option id="nbeclipse.toolchain.super.gcc.directories.includepaths.1666029541" name="Include paths (-I)" superClass="nbeclipse.toolchain.super.gcc.directories.includepaths" valueType="includePath">
                                **inside here is an option list with all my include paths**
                    ...
                </folderInfo>

Then right after the folderInfo close tag another folderInfo section starts

然后在 folderInfo 关闭标记之后,另一个 folderInfo 部分开始

                <folderInfo id="nbeclipse.toolchain.nbl.rel.396284009.2022231003" name="/" resourcePath="src">
                    <toolChain id="nbeclipse.toolchain.nbl.rel.tc.1982112905" name="NetBurner Device Library" superClass="nbeclipse.toolchain.nbl.rel.tc" unusedChildren="">
                        <targetPlatform id="nbeclipse.toolchain.nbl.rel.tc.targetPlatform" isAbstract="false" name="NetBurner Module Library" superClass="nbeclipse.toolchain.nbl.rel.tc.targetPlatform"/>
                        <tool id="nbeclipse.toolchain.nbl.rel.tc.gcc.1671008421" name="GNU C Compiler" superClass="nbeclipse.toolchain.nbl.rel.tc.gcc.278195048">
                            <inputType id="nbeclipse.toolchain.nbl.rel.tc.gcc.input.436341699" name="GNU C Input" superClass="nbeclipse.toolchain.nbl.rel.tc.gcc.input"/>
                        </tool>
                        <tool id="nbeclipse.toolchain.nbl.rel.tc.gpp.498310867" name="GNU C++ Compiler" superClass="nbeclipse.toolchain.nbl.rel.tc.gpp.767008127">

                            **the newly added paths don't show up here, if I add them manually things start working** 

采纳答案by Tod

The key to the answer was posted by dbrank0 but he didn't promote it to an answer so I just upvoted the comment. Something went wrong in the .cproject file and editing it manually fixed the problem.

答案的关键是由 dbrank0 发布的,但他没有将其提升为答案,所以我只是对评论投了赞成票。.cproject 文件出了点​​问题,手动编辑它修复了问题。

回答by Torsten

Try to replace relative workspace path to real path (add same include as filesystem) in include directories project settings.

尝试在包含目录项目设置中将相对工作区路径替换为真实路径(添加与文件系统相同的包含)。

回答by Honwhy Wang

full absolute path is okay at any time. when use the relative directory you should be careful. in eclipse the current directory is not the same as run directory.

完整的绝对路径在任何时候都可以。使用相对目录时要小心。在 eclipse 中,当前目录与运行目录不同。

string strRunDir, strCurDir;
strRunDir = AfxGetApp()->m_pszExeName;
strCurDir = GetCurrentDirectory(...);

and strRunDir != strCurDir;

和 strRunDir != strCurDir;

when you want to use the relative directory, you should reference to the runDir;

当你想使用相对目录时,你应该参考runDir;