I'm somewhat new to C/C++, and I find myself spending exorbitant amounts of time searching through header files (one innocent include might actually bring dozens more header files with it). Even helpful IDEs like Visual Studio aren't always helpful; sometimes when I try to go to the definition of a function, it prompts me to choose from several--the very thing I was trying to avoid.
So...in a very large project with thousands of header files and many occurrences of functions that share the same name and parameters, what's the best way to determine, without question, which specific function is being called?
Try adding /showIncludes to the compiler command line (In the project settings, Configuration Properties, C/C++, Command Line). This outputs all the headers used in a given .cpp file compilation. This is not a fast way, but it is a sure way.
When Intellisense isn't working, I recommend Find in Files. It is easier to track down the definition in the header this way. I find I can usually tell which is the relevant declaration.
Keep in mind that you cannot find the source in the header, unless you are dealing with templates or inlined functions. So there is generally no reason to attempt to discriminate which declaration(s) are being applied. If the definition exists in a SOURCE file (.c,.cpp), then there can only be one function of that name and signature for it to compile. It is generally better to google the function name if it is a published API from Microsoft or another source.
Tools such as Visual Assist for Visual Studio improve the ability to locate such definitions, as well.
Also, you can massage Intellisense into working better. Try deleting the Intellisense Database and having it be rebuilt. You can see where it has trouble by the "errors" it shows in the error view. Often you need to improve the includes directories, especially if this is a makefile project. If it grays out code that shouldn't be grayed out, some preprocessor symbol is wrong. Maintaining the Intellisense is often worth it because it's great when it works.
Tools such as Visual Assist for Visual Studio have their own, often improved intellisense-like method of finding definitions.
Admittedly, I'm still new to C++ (and programming in general) as well. But, I think that the Visual Studio feature you describe in your question is the most help you'll get. It will narrow things down a little for you, but you'll still have to do some good ole sleuthing.
Ask the compiler. No, really! It's the only way to be sure.
Try using Microsoft visual studio 2005. You can easily jump onto the function definition, Function declaration also you can see the Function call and callers graph as well.
Related
This question already has answers here:
Creating .cpp files from .h files visual studio
(5 answers)
Closed 5 years ago.
This may be a minor question, but a solution would save me a lot of time and prevent mistakes.
I am working on a C++ project in Visual Studio. If I define a function in a class in a header file, say
void InitButton(int ButtonNum);
I usually copy and paste the signature to the cpp file. Then, I insert the class name, and replace the semi-colon with curly braces, like so:
void Button::InitButton() {
}
However, I'll often forget the class name, or accidentally type it before the return type. This also happens for any static variables I need to define in code. This seems small, but piles up since I'm at the beginning phase of a project. Is there a quicker way to auto-generate these in Visual Studio C++? Or a best practice I'm missing out on?
EDIT: It appears this has been asked before: Auto-create implementation in Visual Studio C++ 2010
EDIT 2: The best solution for me appears here: http://www.radwin.org/michael/2011/05/10/stubgen/
In Visual Studio 2015 there's the "Quick Actions" feature, a light bulb that shows up whenever you hover over a piece of code. In a header file one of the options is "Create definition of ... in ....cpp". This option generates the function definition in the corresponding header file.
Visual Assist has a generator for this. I'm sure there are a lot of plugins that have the same functionality, but you might have to part with some cash for them. Visual Assist is well worth the money though, as it gives you a lot of functionality which I find invaluable. My killer features are the better function and method info, better auto complete and find file in projects (one key combination and you get a list of all files in all projects and can search for a particular file without knowing where in the hierarchy it lives).
Is there a possibility in Visual Studio 2010 to view a class tree for a CPP-file without loading it into the current project?
I want to open a couple (header file, source file) and be able to browse through all the methods of all the contained classes. I have searched here on the forum as well as on the Internet, but could not find anything.
Are there similar tools from third-party vendors?
At least as far as I know, VS won't do much of anything with your code unless it's part of a project.
Depending on exactly what you want, there are quite a few similar tools. The most common is probably doxygen.
Many (most?) UML tools can also reverse engineer (UML) class diagrams from source code, and quite a few support "round-tripping" (ability generate a class diagram from source code, manipulate the UML, then generate new code matching the edited UML).
I should probably also point out, however, that these tools aren't necessary very popular (in fact, some people really hate them) and it's probably a pretty rare case where generating class diagrams with them is going to be any quicker or easier than creating a project file so you can do it inside of VS. Most of them have quite a few features that VS does, so they may be worthwhile if you want to do things VS doesn't support. If, however, you're happy with the results VS produces, using VS and creating a project file is probably the easiest way to get it.
Say I have a *.cpp/.h/.hpp* file with #include <map> in it but I do not use anything defined or referenced by map header file. I want VS to tall me a warning or something that I do not use data from file I included.
So does Visual Studio 2012 (in any of its editions) show unused includes? (answer required shall include simple - YES/NO)
These sort of answers drive me up the wall. Every other language does this. Any C++-able linker does it. It's easy:
Compile everything, producing, among other things, a symbol table for that module
Link it.
Anything that's used, but not defined, already generates an undefined external error.
Everything that's defined, but not used, should generate an extraneous external definition warning. While you're at it, group these by source file, and only generate the warning if no symbols in that module are referenced.
Either
show a warning: "Hey, man, you're not really using this thing... so... whatever, man. Cheers", or
enable a context menu options, like "remove unused includes," to clean it up.
For a static language, you have a symbol table.
When linking, you can see what items in that table are used.
You can offer an option to at least highlight everything that's NOT used.
If you really need to, build the thing, go through the above steps, and rebuild after removing potentially unused references. If it explodes, roll it back.
There are a hundred ways to solve this problem. The fact that none of the IDEs out there apparently do this is surprising.
At the very least, make it a "helpful hint" option. It's not the job of the compiler to do the programmer's job. It's the job of the IDE to do as much of the programmers job as possible, with the mutual understanding that ultimate responsibility lies with the human in the chair.
Source I used to write C compilers for a living.
There is no known way to tell if you don't use anything from an include file. It's an unsolved problem except in utterly trivial cases.
Consider the following include file:
int x;
How could you tell if x was or wasn't referenced? The compiled output that included that file might link to another object file that accesses x. The compiled object file might be linked in more than one way at more than one time.
The best way to tell if an include file is unnecessary is to comment it out or remove it entirely, and see if the code still compiles. This is a process that would be easy to automate.
I build my C++ code base with MSVC++ 2008 and 2010. Is it even possible to get translation unit, analyze it, insert some code if necessary and then pass on to the compilation process? Original source code should not be affected.
Sure, it should be transparent for a developer who builds a project. Finally, it will only affect object files. Visual studio is very powerful. I guess, there should be some kind of plugin API or hooks to do that. Please, give me a hint.
I don't believe this is possible as you describe it, though I don't know for sure. It would certainly be non-trivial. The only similar project that springs to mind is OpenMP, but I got the impression that Microsoft was the one who implemented their version of it.
I could see a template engine such as Cheetah sufficing though. You would likely give up your bells and whistles like code completion and intellisense though.
Basically, you would set up the files to use a custom compiler to generate the new code in another file. The C++ compiler would then compile the generated files. I don't think it would be elegant or pleasant to use, to be frank.
I've used CMake to do similar things, though I did not target it as a general tool. I wrote a one off for some content generation.
Maybe if you actually describe some of the specifics of what you want to do we can offer a better solution.
While working within C++ libraries, I've noticed that I am not granted any intellisense while inside directive blocks like "#ifndef CLIENT_DLL ... #endif". This is obviously due to the fact that "CLIENT_DLL" has been defined. I realize that I can work around this by simply commenting out the directives.
Are there any intellisense options that will enable intellisense regardless of directive evaluation?
By getting what you want, you would lose a lot.
Visual C++ IntelliSense is based on a couple major presumptions
1. that you want good/usable results.
2. that your current IntelliSense compiland will present information related to the "configuration" you are currently in.
Because your current configuration has that preprocessor directive, you will not be able to get results from the #ifndef region.
The reason makes sense if you think it through. What if the IntelliSense compiler just tried to compile the region you were in, regardless of #ifdef regions? You would get nonsense and non-compilable code. It would not be able to make heads or tails of your compiland.
I can imagine a very complex solution where it runs a smaller (new) parse on the region you are in, with only that region being assumed to be part of the compiland. However, there are so many holes in this approach (like nothing in that region being declared/defined) that this possible approach would immediately frustrate you, except in very very simple scenarios.
Generally it's best to avoid logic in #ifdef regions, and instead to delegate the usage of parameterized compilation to entire functions, so that the front-end of the compiler is always compiling those modules, but the linker/optimizer will select the correct OBJ later on.
Hope that helps,
Will
Visual Studio 6.0 has a little better support for C++ in some arena's such as this. If you need the intellisense then just comment it out temporarily, build and then you should have intellisense. Just remember to recomment it when you're through if that was your intent.
I just wish Intellisense would work when it SHOULD in VS2008. MS "workarounds" don't work (deleting .ncb files) most of the time. Oooh,
here's another SO discussion..., let's see what IT has to say (I just love SO)
I'm often annoyed by that too ... but I wonder whether intellisense would actually be able to provide any useful information, in general, within a conditioned-out block?
The problem I see is that if the use of a variable or function changes depending on the value of a preprocessor directive then so may it's definition. If code-browsing features like "go to definition" were active within a conditioned-out block would you want them to lead to the currently-enabled definition or to one that was disabled by the same preprocessor conditions as the disabled code you're looking at?
I think the "princple of least surprise" dictates that the current behaviour is the safest, annoying though it is.
Why you want to do explicitly in the code?
There is already cofiguration setting in VS and the way you can enable and disble the intellisense.
see the link.
http://msdn.microsoft.com/en-us/library/ms173379(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ks1ka3t6(v=VS.80).aspx
This link may help you.