C++ 如何让 IntelliSense 在 Visual Studio 2008 中可靠地工作

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

How to get IntelliSense to reliably work in Visual Studio 2008

c++cvisual-studio-2008intellisense

提问by Mark Ingram

Does anyone know how to get IntelliSense to work reliably when working in C/C++ projects? It seems to work for about 1 in 10 files. Visual Studio 2005 seems to be a lot better than 2008.

有谁知道在 C/C++ 项目中工作时如何让 IntelliSense 可靠地工作?它似乎适用于大约十分之一的文件。Visual Studio 2005 似乎比 2008 好很多。

Edit: Whilst not necessarily a solution, the work-around provided here:

编辑:虽然不一定是解决方案,但此处提供了解决方法:

How to get IntelliSense to reliably work in Visual Studio 2008

如何让 IntelliSense 在 Visual Studio 2008 中可靠地工作

Is probably the best bet if I want a decent IntelliSense system.

如果我想要一个像样的 IntelliSense 系统,这可能是最好的选择。

采纳答案by Steve Gury

I've also realized than Intellisense is sometime 'lost', on some big project. Why? No idea.

我也意识到 Intellisense 有时会在一些大项目中“丢失”。为什么?不知道。

This is why we have bought Visual Assist(from Tomato software) and disabled Intellisense by deleting the dll feacp.dll in the Visual studio subdirectory (C:\Program Files\Microsoft Visual Studio 8\VC\vcpackages)

这就是为什么我们购买了Visual Assist(来自Tomato 软件)并通过删除 Visual Studio 子目录 (C:\Program Files\Microsoft Visual Studio 8\VC\vcpackages) 中的 dll feacp.dll 来禁用 Intellisense

This is not a solution, just a workaround.

这不是解决方案,只是一种解决方法。

回答by John Richardson

Native C++ intellisense does not work reliably in any version of Visual Studio. I find there are two common problems:

本机 C++ 智能感知在任何版本的 Visual Studio 中都不能可靠地工作。我发现有两个常见的问题:

1) Header file paths are not set-up correctly. When you find a type where intellisense is not working, use the IDE to click through each header file to find the one containing the type. (Right click on #include and select Open Document...). If this fails before you get to the file which declares the type then this is your problem. Make sure header file search paths are set-up correctly.

1) 头文件路径设置不正确。当您发现智能感知不起作用的类型时,请使用 IDE 单击每个头文件以查找包含该类型的文件。(右键单击#include 并选择打开文档...)。如果这在您到达声明类型的文件之前失败,那么这就是您的问题。确保头文件搜索路径设置正确。

And,

和,

2) The intellisense database is corrupt. This happens ALL The time. You need to close the solution, delete the .ncb file, and then reopen the solution. I posted the macro I use for this in answer to another question here.

2) 智能感知数据库已损坏。这事儿常常发生。您需要关闭解决方案,删除 .ncb 文件,然后重新打开解决方案。我张贴在回答这个宏我用另外一个问题在这里



The preprocessor can also confuse intellisense - so make sure any #defines during build are also available to intellisense. Other than that, I don't know what else can break it. I've not seen any particular issues with forward declarations.

预处理器也可能混淆智能感知 - 因此请确保构建期间的任何 #defines 也可用于智能感知。除此之外,我不知道还有什么可以打破它。我没有看到任何关于前向声明的特定问题。

回答by Nick McCowin

It looks like there's hope on the horizon for those of us unable to obtain Visual Assist:

对于我们这些无法获得 Visual Assist 的人来说,似乎有希望:

Rebuilding Intellisense

重建智能感知

回答by Sara Chipps

Do you have any add-ins installed (or uninstalled)? I find that effects my intellisense.

您是否安装(或卸载)了任何加载项?我发现这会影响我的智能感知。

Besides that just making sure your Tools->Options->Text Editor->All Languages "Auto List Members" and "Parameter Information" are checked off.

除此之外,只需确保您的工具->选项->文本编辑器->所有语言“自动列表成员”和“参数信息”被选中。

回答by TW Burger

My fix to itellisense was required after that awful refactor utility minced my code. The problem was a class header file that included an #include of itself. The recursive reference destroys itellisense. A symptom of this is if itellisense can see other classes but not the current one. Also:

在那个糟糕的重构实用程序粉碎了我的代码之后,我需要修复 itellisense。问题是一个包含自身 #include 的类头文件。递归引用会破坏 itellisense。这种情况的一个症状是 itellisense 是否可以看到其他类而不是当前类。还:

Use #pragma once to eliminate duplicate header loads

使用 #pragma once 消除重复的标头加载

If the project now takes a very much longer time to load, that itellisense trying to make sense of the conflict that is causing then lack of completion support.

如果项目现在需要更长的时间来加载,itellisense 试图理解导致缺乏完成支持的冲突。

Often it is only one class object that is affected, This shows you what files (usually headers) to look at.

通常只有一个类对象受到影响,这会向您显示要查看的文件(通常是头文件)。

回答by FlySwat

I don't use VS2008 for C++, only VB & C#, but I find that when intellisense stops working (true for VS2003/2005/2008) it's because something in the project/file is broken - usually a bad reference or code.

我不将 VS2008 用于 C++,仅用于 VB 和 C#,但我发现当智能感知停止工作时(VS2003/2005/2008 是正确的),这是因为项目/文件中的某些内容已损坏 - 通常是错误的参考或代码。

VB and C# have much better intellisense support due to the ability to reflect on the referenced assemblies to build the intellisense tree.

由于能够反映引用的程序集以构建智能感知树,VB 和 C# 具有更好的智能感知支持。

C++ has to walk the include files for function prototypes, and if the paths are not correct it will not find all the prototype headers.

C++ 必须遍历函数原型的包含文件,如果路径不正确,它将无法找到所有原型头文件。

回答by Mark Ingram

@John Richardson/ @Jonathan Holland

@约翰理查森/@乔纳森荷兰

My includes are setup correctly, no problems there. I've also tried the NCB rebuild several times but it never fixes it 100%.

我的包含设置正确,没有问题。我也多次尝试重建 NCB,但从未 100% 修复它。

I have a feeling it may be to do with forward declarations of classes. e.g. to reduce the complexity of includes in header files we normally do something like:

我有一种感觉,这可能与类的前向声明有关。例如,为了降低头文件中包含的复杂性,我们通常会执行以下操作:

class MyPredeclared;

class SomeOtherClass
{
private:
    MyPredeclared* m_pPointer;
}

I wonder if that screws it up? Any other ideas? It definitely gets worse the larger your project gets.

我想知道这是否搞砸了?还有其他想法吗?你的项目越大,它肯定会变得更糟。

回答by Francesco

I had a very annoying problem, intellisense was working only in some files, without any evident reason... it took me a couple of hours of digging through google, but I finally understood that the reason was indeed recursive reference! I was using the:

我有一个非常烦人的问题,intellisense 只在某些文件中工作,没有任何明显的原因......我花了几个小时通过谷歌挖掘,但我终于明白原因确实是递归引用!我正在使用:

#ifndef CLASS_H
#define CLASS_H
...
#endif

to avoid redefinition of symbols, and this sometimes breaks intellisense in big projects.

避免重新定义符号,这有时会破坏大型项目中的智能感知。

But it is enough to comment the ifndef-define-endif and put a:

但足以评论 ifndef-define-endif 并放置:

#pragma once 

at the beginning of the header files to still avoid redefinitions and have Intellisense working again =)=)

在头文件的开头仍然避免重新定义并让 Intellisense 再次工作 =)=)

At least, this worked for me, hope it's useful...

至少,这对我有用,希望它有用......

Cheers Francesco

干杯弗朗切斯科

回答by the swine

I have recently studied Intellisense in VS2008, as I'm developing a rather large C++ numerical linear algebra library where templates and such are used extensively. Intellisense stopped working shortly into the project and I sort of gave up, but now it became really annoying without it so I set to investigate. This is what I found out:

我最近在 VS2008 中研究了 Intellisense,因为我正在开发一个相当大的 C++ 数值线性代数库,其中模板等被广泛使用。Intellisense 很快就停止了该项目的工作,我有点放弃了,但现在没有它它变得非常烦人,所以我开始调查。这是我发现的:

Assuming there is a file(s), containing code that "breaks" Intellisense,

假设有一个文件,其中包含“破坏”智能感知的代码,

  • if header files that break Intellisense are in the project, but are not #included, it still works in the rest of the files
  • if they are included, but no type declared inside is used, it still works
  • if they are included and a type declared inside is used, it might still work a bit (no Intellisense for members, no Intellisense after occurrence of given type, but at least global names and argument info before)
  • if Intellisense is broken in one .cpp file, it can still work in the others where the problematic code is not included or used (but i imagine if it crashes bad, it will get disabled for the whole project, although that did not happen to me)
  • Intellisense seems to be updated after successful compilation (sometimes not before)
  • putting broken code inside any of #if 0, /* .. */or //seems to put Intellisense at ease
  • 如果破坏 Intellisense 的头文件在项目中,但不是#included,它仍然可以在其余文件中使用
  • 如果包含它们,但没有使用内部声明的类型,它仍然有效
  • 如果包含它们并且使用了内部声明的类型,它可能仍然有效(成员没有智能感知,在给定类型出现后没有智能感知,但至少是全局名称和参数信息之前)
  • 如果 Intellisense 在一个 .cpp 文件中被破坏,它仍然可以在没有包含或使用有问题的代码的其他文件中工作(但我想如果它崩溃了,它会在整个项目中被禁用,尽管这并没有发生在我)
  • Intellisense 似乎在编译成功后更新(有时不是之前)
  • 将损坏的代码放入任何一个#if 0/* .. */或者//似乎让 Intellisense 放心

From the C++ features I used, actually only a few break Intellisense:

从我使用的 C++ 特性来看,实际上只有几个打破了 Intellisense:

  • comparison with '>' or '>=' in template parameter (e.g. static_assert<(size > 0)>)
    • not solved by using double parentheses (static_assert<((size > 0))>does not help)
    • solved by using '<' or '<=' instead (static_assert<0 < size>works)
    • solved by storing the value in enum and using that to specialize the template
  • explicit function template specialization disables argument info (e.g. function<type>(args))
    • probably unable to solve (maybe wrap in a macro), but I can live with it being broken
  • instantiation of template member type, such as Matrix::MakeMatrixType<3, 3>::Result r;
    • kind of hard to figure out exactly why this happens (likely because of use of Eigen)
    • workaround by moving such code in a separate .cpp where IS won't work (not always possible)
  • 与模板参数中的 '>' 或 '>=' 进行比较(例如static_assert<(size > 0)>
    • 不能通过使用双括号解决(static_assert<((size > 0))>没有帮助)
    • 通过使用 '<' 或 '<=' 代替(static_assert<0 < size>有效)解决
    • 通过将值存储在枚举中并使用它来专门化模板来解决
  • 显式函数模板特化禁用参数信息(例如function<type>(args)
    • 可能无法解决(可能包含在宏中),但我可以忍受它被破坏
  • 模板成员类型的实例化,如 Matrix::MakeMatrixType<3, 3>::Result r;
    • 有点难以弄清楚为什么会发生这种情况(可能是因为使用了 Eigen)
    • 通过在单独的 .cpp 中移动此类代码来解决方法,其中 IS 不起作用(并非总是可行)

It would seem that some of those problems are due to some "simplified" parsing, which is less strong than a proper C++ parser. With the above information at hand, a "reliable" method of making Intellisense work in an existing code:

其中一些问题似乎是由于一些“简化”的解析造成的,它不如适当的 C++ 解析器强大。有了上述信息,在现有代码中使 Intellisense 工作的“可靠”方法:

  1. Set up an empty project (a console app), create Main.cpp with dummy void main() {}in it.
  2. Include one of your broken header files, and math.h
  3. Build (it must compile, in order for Intellisense to update reliably)
  4. Test whether Intellisense is working by typing e.g. sin(and seeing if argument help pops up. Sometimes, this would work, but member help wouldn't - so try that as well.
  5. Make an instance of something in the header file, build, see if that manages to kill IS.
  6. Remove code from the culprit file and go to step 3
  7. After finding and fixing problematic code, put back code removed in step 5, try again
  8. After making a whole class work well, make an instance of the next class, and so on ...
  1. 设置一个空项目(一个控制台应用程序),创建 Main.cpp,其中包含 dummy void main() {}
  2. 包括您损坏的头文件之一,以及 math.h
  3. 构建(它必须编译,以便 Intellisense 可靠地更新)
  4. 通过键入 egsin(并查看是否弹出参数帮助来测试 Intellisense 是否正常工作。有时,这会起作用,但成员帮助不起作用 - 所以也尝试一下。
  5. 在头文件中创建一个实例,构建,看看是否能够杀死 IS。
  6. 从罪魁祸首文件中删除代码并转到第 3 步
  7. 找到并修复有问题的代码后,将步骤 5 中删除的代码放回原处,再试一次
  8. 在让整个班级运行良好后,再做一个下一个班级的实例,依此类推……

I found it easy this way to pinpoint locations of code that made problems (I realize that this might be unfeasible for really large projects, in my case only a single file out of 97 made problems). Note that 'Build' here refers to compiling, the linking stage does not need to finish, so unresolved externals are ok, the IS should update regardless.

我发现通过这种方式来确定产生问题的代码位置很容易(我意识到这对于真正的大型项目来说可能是不可行的,在我的情况下,97 个问题中只有一个文件)。注意这里的'Build'是指编译,链接阶段不需要完成,所以未解析的外部是可以的,IS应该无论如何更新。

Another method of updating IS (other than building) is to save everything, close workspace, delete .ncb file and reopen it. Then wait for 'Updating Intellisense ... (N)' to disappear from the status bar (N counts towards zero, if it doesn't go all the way, it kind of shows progress where problems occurred). I found this rather tedious.

更新 IS(除了构建)的另一种方法是保存所有内容、关闭工作区、删除 .ncb 文件并重新打开它。然后等待“正在更新 Intellisense ... (N)”从状态栏中消失(N 计数为零,如果它没有完全消失,它会显示发生问题的进度)。我觉得这很乏味。

回答by Aaron Middleton

The problem is with the .vcproj files.

问题在于 .vcproj 文件。

You will find if you switch to release mode from debug mode, build, then try intellisense it often works.

你会发现如果你从调试模式切换到发布模式,构建,然后尝试智能感知它通常有效。

Close Visual Studio. If you search for the .vcproj files in your project, edit them and search for the first two instances of AdditionalIncludeDirectories. The value for this should look something like "..\,....\" rather than "../..".

关闭 Visual Studio。如果您在项目中搜索 .vcproj 文件,请编辑它们并搜索 AdditionalIncludeDirectories 的前两个实例。这个值应该类似于“..\,....\”而不是“../..”。

Reopen your project, let the Intellisense finish building, then it should be fixed.

重新打开你的项目,让 Intellisense 完成构建,然后它应该被修复。