Tuesday, October 13, 2015

Netbeans for C++

I've been using Netbeans for C++ development. One of the big issues I had is getting the Netbeans Code Assistance feature to work properly. Meaning, I want:

  • syntax highlighting
  • code completion
  • refactoring
  • call graphs
In short, everything I expect from Netbeans when using Java. I am dealing with a large CMake based project, with existing sources. So I build the project manually, and CMake emits the Makefiles. Now I have a project that netbeans can open (an "existing Makefile based project"). The next thing you need to do is set your include directories (shown below). 

Now this is where things get weird. Despite having set the includes several times none of the Code Assistance features worked. I tried deleting the NB cache, restarting, etc, etc. Nothing worked. If I write click on the project, and select Code Assistance I can dump the diagnostics. Notice how the user include paths that I entered ARE NOT PRESENT? WTF?


 from project main [/Users/ghendrey/git/main]
Lang=CPP Flavor=CPP excluded=false
User Include Paths:
User Macros:
System Include Paths:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks
/Library/Frameworks



But, my user includes are not showing up! Finally, I tried Project > Properties > General and added my "src" folder to the "Project Source Folders". Now this seems like a bit of a "duh", but it wasn't obvious, since "." was already listed under project source folders, which presumably grabs everything. Furthermore, once I save the properties, and close the dialog, the "src" folder disappears, and only "." is shown. HOWEVER, it seems that adding "src" somehow kicked started the Code Assistance. Now  I see the correct user includes when I dump the diagnostics:

 from project main [/Users/ghendrey/git/main]

Lang=CPP Flavor=CPP excluded=false
User Include Paths:
/Users/ghendrey/splunk/current/include
/Users/ghendrey/git/main/src
/Users/ghendrey/git/main/src/libzero
/Users/ghendrey/git/main/src/util
User Macros:
System Include Paths:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks
/Library/Frameworks

And lo and behold, all of code completion is working. Incidentally, the Code Assistance right click menu is often broken (several of the items become unelectable until I close the project and restart netbeans). So, if you can deal with these annoyance, once you (finally) get up and running with code assistance in netbeans, it is a way way better c++ development environment than eclipse. 



4 comments:

  1. Vladimir2:30 AM

    Geoffrey,
    Have you tried to build your application from IDE using Clean&Build action?
    Once you do that successfully there is a button in Output window (on left side) - "Configure Code Assistance", using it you can force IDE to automatically extract all includes/macros used during build and update project settings for you. Could you try it?
    Btw, which NB version do you use?

    Thanks,
    Vladimir.

    ReplyDelete
  2. since I already got things working, this didn't have any noticeable effect. Good news is, it is working. The button you referred to was not documented was it? If so I missed the docs.

    ReplyDelete
  3. Vladimir1:34 AM

    I think you are right. It is not documented and difficult to find once you don't know about it. Btw, probably, you have missed some other effect of this button :-)
    What it does with in more details when IDE is listening of Clean&Build activity:
    - extract exact compile line for each compiled translation unit
    - put this line as "Compile Line" for each observed source file
    - extract -I options to initialized Include Folders/Include Files
    - extract -D options to initialized Macro Defs
    - merge common Includes and Macro Defs to parent Folder level and leave exclusive on File level

    Btw, Now each file in project tree in it's Properties have non-empty "Compile Line" option.
    So if you changed code and just want to recompile one file (i.e. as syntax check) you can press F9 and file would be recompiled with exact compile line.
    When you change header file, then IDE uses Compile Line property of Source file which includes this header, so Compile File is available for headers as well and compile it correctly at least in the context of one Translation Unit.

    Hope it helps,
    Vladimir.

    ReplyDelete
  4. Ah, yes I do see the Compile File (F9) option now, and it works.

    ReplyDelete