Eclipse CDT 未正确跟踪宏定义

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

Eclipse CDT Does not track macro defines correctly

eclipsedebuggingeclipse-cdt

提问by Steven Lu

In the build settings I have it define DEBUGwhen in the debug build configuration, so that I may make my code do separate things depending on which type of build it is.

在构建设置中,我DEBUG在调试构建配置中定义了它,这样我就可以让我的代码根据构建的类型来做不同的事情。

However in Eclipse it darkens out the parts of the code which are excluded by the preprocessor, and this doesn't keep up when I change the setting (whether I'm currently building debug or release). So, I have a bunch of code which is perpetually darkened out, and the Eclipse indexer and other helpful features don't work inside of those areas.

然而,在 Eclipse 中,它会使预处理器排除的代码部分变暗,并且当我更改设置时(无论我当前是在构建调试还是发布),这并没有跟上。所以,我有一堆永远变暗的代码,并且 Eclipse 索引器和其他有用的功能在这些区域内不起作用。

How to fix? Has anyone else encountered this?

怎么修?有人遇到过这种情况么?

Update: Still having this issue. I eventually abandoned my unit-test build configs and simply put that functionality into a command-line switch. Just so that I could get indexing to work while I write my unit tests.

更新:仍然有这个问题。我最终放弃了我的单元测试构建配置,并简单地将该功能放入命令行开关中。这样我就可以在编写单元测试时让索引工作。

回答by Simon Lehmann

You have to set the option "Build configuration for the indexer" to "Use active build configuration" in the projects C/C++ indexer preferences. Open the properties panel for the project, go to c/C++ General and Indexer and enable project specific settings and change the radiobutton on the bottom to "Use Active build configuration".

您必须在项目 C/C++ 索引器首选项中将选项“为索引器构建配置”设置为“使用活动构建配置”。打开项目的属性面板,转到 c/C++ 常规和索引器并启用项目特定设置并将底部的单选按钮更改为“使用活动构建配置”。

CDT indexer settings

CDT indexer settings

Of course you can also set this in the global properties panel of Eclipse to change this setting for all projects.

当然你也可以在 Eclipse 的全局属性面板中进行设置,为所有项目更改此设置。

回答by dgrant

Go to your project's Properties, then go to C/C++ General->Paths and Symbols->Symbols. Add your defines there and it will work.

转到项目的属性,然后转到 C/C++ General->Paths and Symbols->Symbols。在那里添加您的定义,它将起作用。

回答by mtalexan

1) If you want to change the defines for your system based on build configuration, you need to do as dgrant said: "Project properties and select C/C++ General->Paths and Symbols then select the Symbols tab", but you need to make sure you have the correct configuration selected at the top of your window. Also be sure when you add the symbol to the list that you DON'T check the "add to all configurations" checkbox.

1)如果您想根据构建配置更改系统的定义,您需要按照 dgrant 所说:“项目属性并选择 C/C++ 常规->路径和符号,然后选择符号选项卡”,但您需要确保在窗口顶部选择了正确的配置。还要确保将符号添加到列表中时不要选中“添加到所有配置”复选框。

2) If you want your correct set of symbols to be parsed by CDT in your editor, you'll also need to make sure you have the correct build configuration active as well. To do that, right click on your project and select Build Configurations->Set Active-> and select the build configuration you want to make active. CDT will now parse all the files as if this build configuration were active instead, using the global symbols you defined in the last step.

2) 如果您希望编辑器中的 CDT 解析正确的符号集,您还需要确保正确的构建配置也处于活动状态。为此,右键单击您的项目并选择 Build Configurations->Set Active-> 并选择您想要激活的构建配置。CDT 现在将使用您在上一步中定义的全局符号来解析所有文件,就好像此构建配置处于活动状态一样。

There is a problem I've seen with every version of Eclipse where it doesn't always decide to rebuild your index files immediately. To get it to do so, you can either start a build (the build doesn't actually have to complete), or you can right click on your project and go to Index->Rebuild. Both cause an immediate re-indexing to occur. You shouldn't need this however, I can confirm that simply changing the build configuration as described in step 2 above will cause a #ifdef that checks for a symbol defined only in one of my build configurations immediately causes it to become greyed-out or un-greyed-out as it should.

我在每个版本的 Eclipse 中都看到过一个问题,即它并不总是决定立即重建索引文件。为此,您可以开始构建(构建实际上不必完成),或者您可以右键单击您的项目并转到索引-> 重建。两者都会导致立即重新索引。但是,您不应该需要这个,我可以确认,简单地更改上面第 2 步中描述的构建配置将导致 #ifdef 检查仅在我的构建配置之一中定义的符号会立即导致它变灰或未变灰,因为它应该。

回答by Steven Lu

My satisfactory solution has been to move away from Eclipse to editors with a working libclang plugin: Sublime Text, Vim.

我令人满意的解决方案是从 Eclipse 转移到带有可运行的 libclang 插件的编辑器:Sublime Text、Vim。

For an intelligent IDE for C/C++, one probably can't go wrong with XCode or MSVC.

对于 C/C++ 的智能 IDE,XCode 或 MSVC 可能不会出错。