Zwabel’s Weblog

February 4, 2009

KDevelop4: Automatic include-directives and forward-declarations

Filed under: KDE,KDevelop — zwabel @ 1:09 am
Tags: , , , , , ,

Missing Include Completion
C++ is a great and powerful programming-language. Yet it has the downside against some other languages, that you always have to deal with include-directives or forward-declarations before you can use a class.

This is a factor that often motivates me not to create too many different source-files, although design-wise that would make sense. Wouldn’t it be nice if you could just start hacking without caring about the whole visibility stuff? Especially when you have to do the same includes again and again, and already know the library you’re using and it’s classes, this is nothing more than an annoyance.

I had implemented a feature to automatically add include-directives within KDevelop 4 about a year ago, but it wasn’t as comfortable and useful as it could be. During the last days, I’ve taken the time to polish up this feature so it’s worth a blog-post.

The whole thing is based on the DUChain, and it respects all declarations from within the global duchain store. This means that from the moment on that KDevelop has processed a source-file, you can start just using the declarations from within that file from anywhere. Whenever you type something and the completion-list does not offer any completions, which means it’s not visible, then the missing-include completion will start searching the duchain store for matching declarations, and when it finds some, it will offer you right within the completion-list to automatically add an include-directive for you.

This works in many different situations: If you try calling a function or constructing a class, template-instantiate a class, if you try to access the contents of a class that is either unresolved or has only been forward-declared, or if you just type in the name of an arbitrary declaration.

Sometimes however when writing header files, you do not need the whole class-definition but rather just a forward-declaration. From today on, the very same list will show you an entry to automatically add a forward-declaration for the typed class for you. This even works with template-classes, and it correctly respects namespaces, creating namespace declarations around the forward-declarations as needed.

Together, under the circumstance that KDevelop already knows the classes you are working with, this frees you of one annoying part of the hacking.


This is what happens when you try calling a known function that has not been included yet:

And this is the effect you will have when you push the up-arrow and execute the “Add #include …” action:

This is what happens when you simply type the name of a known class:

And this is what you’ll get when you pick the “Add forward-declaration” action. A fully valid forward-declaration was inserted. Well, apart from the fact that std::allocator is not defined. πŸ˜‰

Now std::vector is forward-declared. But what if you actually try using it? KDevelop will notice it, and will offer you including the correct header for being able to use it:

And this is what happens when you try to instantiate a known but not included template. Notice that it does offer to include the forwarding-header “QMap”, and not only the header that really contains the class which is “qmap.h”:

And now a little demonstration how this can help you using most any library:

See that ICore has a lot of members that again return pointers to other classes, that of course were only forward-declared. This usually means that before you can use the result of activeSession(), you have to find out where KDevelop::ISession is defined, and include it manually:

With KDevelop4, the content is only one key-press away:

This is a feature for experienced users who know what they are doing. It always has to be reviewed, and KDevelop does NOT do any programming for you. πŸ˜‰

Development Update
Although I don’t have that much time any more, I did find enough time to do quite some polishing during the last weeks. Many crashes and bugs were fixed, and I even found time to imlement some smaller features.
Here the most important points:
– The implementation-helper and signal/slot completions now are shown within separate groups in the completion-list, that are shown in an appropriate place(usually right at the top)
– Access-rights is now fully respected by the completion-list, including friend-declarations.
– Not found declarations can be highlighted with an error-underline now(There’s a new “highlight semantic problems” option in the configuration)
– Added context-sensitive code-completion for builtin C++ keywords
– Full code-completion, use-building and refactoring for namespace names
– Less annoying automatic completion, because it auto-hides when an item in the list is matched
– Workaround a multi-threading problem within kdelibs and ksycoca that caused KDevelop to crash very often
– Fix a repository-locking problem that made kdevelop reproducably crash under special circumstances

Although we do not have all the features we want together, we will be releasing a beta-version of KDevelop4 soon, because we believe it’s already a useful application. Aleix Pol Gonzales has started working on the much needed documentation integration, and Hamish Rodda has today started some new discussion on the debugger, and is planning to bring it into a usable state.


  1. This is cool BTW the kdevelop bottom pannel takes too much space doesn’t it? πŸ˜›

    Comment by edulix — February 4, 2009 @ 1:41 am

  2. @edulix: That’s because there is 2 status-bars down there. The upper one will hopefully get some useful content soon(breadcrumb-like document/duchain navigation), and I don’t know what happens in the lower one, looks a bit unneeded right now. πŸ™‚

    Comment by zwabel — February 4, 2009 @ 1:45 am

  3. WOW! I think kdevelop4 looks like it is going to be really great. These are the kinds of features I really love! Make writing code easier/ more enjoyable. Thank you a million times. Ok so I have a feature I would like to request. It very simple. lots of free-software programs have the GPL/license info at the top of every document. If there were an option to auto-fold it(like how function blocks can fold) that would be _so_ cool. I love the GPL, but I hate seeing it up top when I want to see the code; it distracts me. It would be easy to do a check to see if a given comment==”GPL text” and auto-fold/hide it. I apologize if this idea already exits.

    Comment by mike hancock — February 4, 2009 @ 3:18 am

  4. That sounds like a great feature. I look forward to trying it!

    Comment by Will Stephenson — February 4, 2009 @ 6:51 am

  5. I wonder why the first suggestion is string.h and not cstring as one would expect for a C++ programm?

    Does KDevelop know where to start searching for includes with custom makefiles? Is it possible to configure the -I directives?

    I guess this feature is only a hint to the programmer what has to be included. Using the correct paths is still left to the human.

    Comment by Christoph Bartoschek — February 4, 2009 @ 9:37 am

  6. Mmmmh… this is really cool… thanks a lot.

    Comment by hron84 — February 4, 2009 @ 10:04 am

  7. Very cool. Like all the rest of KDevelop. It’s good to see that you have managed to survive the desert of base framework development πŸ™‚ Especially because I only can kdevelop, neither vim nor emacs.

    Minor nitpick added: would be nice to also have the module name for Qt CamelCase includes, e.g. #include .

    Comment by frinring — February 4, 2009 @ 10:08 am

  8. @hancock:
    Good idea, that would be a great feature for kate(And thus kdevelop).

    @Christoph Bartoschek:
    It proposes the file that actually contains the header, plus it has some blacklisting and logic to detect forwarding-headers, but it doesn’t know what the library developers really want you to include. So it has to be human-reviewed.
    KDevelop can extract the includes from custom makefiles under some circumstances, by parsing the gcc command-line.

    Yes, it was a big desert, and we nearly got out of water πŸ˜‰
    CamelCase is supported, as you see in the QMap example. It’s a case of a forwarding-header, and those are proposed too when they are known.

    Comment by zwabel — February 4, 2009 @ 10:22 am

  9. @zwabel: I suppose frinring means headers like QtCore/QMap or QtGui/QWidget. (WordPress seems to mess up the angle brackets as unknown HTML tags.)

    Comment by Stefan Majewsky — February 4, 2009 @ 5:34 pm

  10. Wow, kdevelop4 is each day better. I now use it quite often and I’m very heppy with it !

    It will soon be as easy to develop in C++ as it was in Smalltalk, or even a lot easier…

    Comment by GaΓ«l — February 4, 2009 @ 8:55 pm

  11. So i think when one inputs “std::strlen” it tries including “cstring” ? Would be nice, at least πŸ™‚

    Comment by litb — February 5, 2009 @ 10:52 am

  12. one thing: great stuff dude!

    Comment by litb — February 5, 2009 @ 10:53 am

  13. Please put a check to not forward declare classes in namespace std because, the users are not allowed to modify that namespace, only the compiler/stdlib writers!

    Here is a reference link:

    Comment by obfuscated — June 5, 2009 @ 7:53 am

  14. is there no way to turn this stuff off? it consistently breaks the build by putting in relative or full paths to files that really dont need to be included or make any sense to include whatsoever. i also dont notice this until i’ve already committed changes because it builds fine on my machine. please tell me some way to remove this.

    Comment by Xicor — November 26, 2014 @ 2:09 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: