简体   繁体   English

#include内部函数体不起作用(CDT / Eclipse C ++)

[英]#include inside function body doesn't work (CDT/Eclipse C++)

This question concerns a C++ project managed with CDT 8.1.2 inside Eclipse 4.2.2 (Juno). 这个问题涉及在Eclipse 4.2.2(Juno)中由CDT 8.1.2管理的C ++项目。 The following code snippet will compile but it will be displayed as having errors inside Eclipse. 以下代码段将编译,但是在Eclipse中将显示为错误。

I have a file named foo.h which reads: 我有一个名为foo.h的文件,内容为:

int a = 42;

This file is included in another file foo.cpp: 该文件包含在另一个文件foo.cpp中:

#include <cstdio>

int main() {
    #include "foo.h"
    printf("%d", a);
    return 0;
}

How can I fix the "Symbol 'a' could not be resolved"? 如何解决“无法解析的符号'a'”? In my understanding, the #include statement inside the main() function is supposed to trigger a mere copy-paste action in the preprocessor. 以我的理解,main()函数中的#include语句应该触发预处理器中的仅复制粘贴操作。 CDT seems to have indexed the file correctly because I can CTRL-Click on the filename "foo.h" which then opens the file in the IDE. CDT似乎已正确索引了该文件,因为我可以CTRL-单击文件名“ foo.h”,然后在IDE中打开该文件。 Interestingly, if I move the #include "foo.h" statement just below the #include <cstdio> statement, it works as expected. 有趣的是,如果我将#include "foo.h"语句移到#include <cstdio>语句的正下方,则它会按预期工作。 Is there any option inside CDT to perform preprocessing before resolving symbols? CDT内部是否有任何选项可以在解析符号之前执行预处理?
Side note: I know that this code design is frowned upon, however I need to import code written by somebody else and require content assist set up properly in order to be productive. 旁注:我知道这种代码设计不被接受,但是我需要导入其他人编写的代码,并且需要适当地设置内容帮助以提高工作效率。

Looks like there are many known issues with the code analyser in Eclipse. 看起来Eclipse中的代码分析器有很多已知问题。 See for example this question Turn off eclipse errors (that arent really errors) on how to disable some or all of the error messages. 例如,请参阅以下问题:有关禁用某些或所有错误消息的信息,请关闭Eclipse错误(该错误实际上是错误的) Not sure if that "solution" will be good enough for you but from what I understand it should not impact other aspects of the code indexer. 不知道该“解决方案”是否对您足够好,但是据我了解,它不会影响代码索引器的其他方面。

Since any include should just turn out to be some "plain copy&paste" this may well be a bug in Eclipse/CDT. 由于任何包含都应该只是一些“普通复制和粘贴”,所以这很可能是Eclipse / CDT中的错误。

If you use KDE you may try KDevelop . 如果使用KDE,则可以尝试KDevelop

I don't know whether there is everything around what you need but I just checked and there is at least no confusion about your minimal example above. 我不知道您需要的内容是否齐全,但我刚刚检查了一下,对于上面的最小示例至少没有任何困惑。 (Code highlightning and assist is working as far as I can tell.) (据我所知,代码高亮显示和协助正在起作用。)

在此处输入图片说明

Another quite popular IDE for Linux is Netbeans . 另一个非常流行的Linux IDE是Netbeans I can't give you any information on whether it will support that sort of "inline including" but both of them are popular and worth a try. 关于它是否将支持这种“内联包含”,我无法提供任何信息,但是它们两者都很受欢迎,值得一试。

Perhaps this error is a "hint" for you to refrain from doing what your intention was. 也许此错误是您“避免”执行自己的意图的“提示”。 I don't know whoever would write code into a header that is mean to be included inline but instead of putting effort in a change of your IDE / search for a new one, you should probably refactor the code, There would be a greater benefit I think. 我不知道谁会把代码写到包含在行内的标头中,但是您不应该费力地更改IDE /寻找新的标头,而应该重构代码,这样做会有更大的好处我认为。

Generally, C++ include files (.h, or header files) should be included at the head of the module, not intermingled with the module's code. 通常,C ++包含文件(.h或文件)应包含在模块的开头,而不是与模块的代码混合在一起。

It seems you have encountered an error with your development environment that in that it cannot properly handle a header file included in the middle of a function. 似乎您的开发环境遇到错误,因为它无法正确处理函数中间包含的头文件。

Move your #include "foo.h" statment above your void main()... statement and try again. 将您的#include "foo.h"语句移至void main()...语句上方,然后重试。

Your latest update implies that you can use other IDEs. 最新更新意味着您可以使用其他IDE。 Do they have to support GCC or are you limited to running on Linux or are you simply using CDT to provide call graphing and so on? 他们是否必须支持GCC,或者您仅限于在Linux上运行,还是只是使用CDT提供呼叫图表等等?

Can you use something like Visual Studio express to get the project compiling and use the tools available there to gain an understanding of how the code is structured? 您是否可以使用Visual Studio express这样的工具来进行项目编译并使用那里提供的工具来了解代码的结构?

It's unclear if you can actually use another IDE just to understand the code or if you need to be able to deliver it using the alternate IDE. 目前还不清楚您是否可以真正使用另一个IDE来理解代码,或者是否需要使用备用IDE来交付它。

VS express 2012 works just fine with this style. VS express 2012可以很好地配合这种风格。

Some of my colleagues use Source Insight (commercial, but fair price) and are pretty convinced about it. 我的一些同事使用Source Insight (商业性但价格公道),对此深信不疑。 But unfortunately this is only available for Windows, it seems. 但是,不幸的是,这似乎仅适用于Windows。

Besides Code Blocks (as others already mentioned) you might try the CodeLite IDE , can't tell anything about their quality compared to Eclipse CDT. 除了代码块 (如已经提到的其他代码块 )之外,您还可以尝试CodeLite IDE ,与Eclipse CDT相比,它无法提供任何有关其质量的信息。

Type extern int a; 输入extern int a; ............... ......

How about forward-declaring int a; 向前声明int a;怎么样int a; ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM