How to use CPPCheck with Eclipse CDT

The biggest problem of C++ code is bugs. Bugs can slip through even when compiling code with all warnings turned on. A useful tool in the arsenal of a C++ programmer is CPPCheck, a static analysis tool. You are in luck if you are using Eclipse CDT because the CPPChEclipse plugin makes using CPPCheck with your C++ project very easy.

  • First install CPPCheck:
$ sudo apt install cppcheck
  • Install CPPChEclipse in Eclipse CDT by adding the URL http://cppcheclipse.eclipselabs.org.codespot.com/svn/update/ to Help -> Install New Software. Your Eclipse will restart after the installation.

  • Go to Window -> Preferences -> C/C++ -> CPPChEclipse. First, set the path of CPPCheck to /usr/bin/cppcheck. In the Problems subsection, choose the types of problems you want to be reported about (warnings, errors, style problems). In the Settings subsection, choose the C and C++ standard versions you want to be applied. You can also indicate the CPPCheck use the include paths used by your project (this is very useful).

  • To run CPPCheck on a project, right-click on its name in the Project Explorer and choose CPPCheck -> Run CPPCheck. The default keyboard shortcut for this is Shift + Ctrl + C. After CPPCheck runs, you can view the problems it has reported in the Problems view at the bottom of Eclipse.

Tried with: CPPCheck 1.61, CPPChEclipse 1.0.0, Eclipse 4.4.2 and Ubuntu 14.04

Advertisements

Eclipse Content Assist not working

20150528_content_assist

Problem

When using a fresh install of Eclipse CDT I found that the content assist was not working. This is the feature similar to autocomplete in Visual Studio, where you get completion proposals on pressing Ctrl + Space while typing code.

Solution

Go to Window -> Preferences -> Editor -> Content Assist -> Advanced and enable Parsing-based Proposals. Click Apply. Content assist should start working immediately. No restart of Eclipse required.

Tried with: Eclipse 4.4.2 and Ubuntu 14.04

How to full screen in Eclipse using plugin

Eclipse has no option to display in full screen mode! By full screen mode, we mean that Eclipse occupies the entire display, with the Dash (on the left) and Panel (on the top) hidden. This is typically achieved in other applications by pressing F11. Note that, there is a maximize mode where the current Edtitor window is maximized, but this is different from full screen.

Thankfully, this can be achieved using the Eclipse Full Screen plugin.

  1. Download the plugin zip file from here.

  2. Unzip the file and copy the .jar file to the plugins directory of your Eclipse.

  3. Close Eclipse and start it again.

  4. To go full screen, click Window -> Full Screen or use the keyboard shortcut Ctrl + Alt + Z.

Tried with: Eclipse Full Screen 1.0.7, Eclipse 4.4.2 and Ubuntu 14.04

How to use ZealLookup plugin for Eclipse

Zeal is an offline documentation browser which can be used to lookup help for most popular libraries and APIs. The ZealLookup plugin (also called ZealEclipsePlugin) enables you to lookup help in Zeal right from inside Eclipse! 🙂

Install

  • Shutdown Eclipse.
  • Download the plugin .jar file from here and place it in the plugins directory of your Eclipse installation.
  • Start Eclipse.

Usage

  • You need to configure a keyboard shortcut for ZealLookup. Go to Window -> Preferences -> General -> Keys. Look for Lookup in Zeal command and set a keyboard shortcut for it.

  • I use Vrapper, so I set the keyboard shortcut as \z which is the same as what I use to call Zeal from within Vim.

  • To lookup a word in Zeal: highlight the word and press the keyboard shortcut you have set earlier. Zeal should open with the documentation for the word. I found that sometimes Zeal stays in the background, but it has looked up the word.

  • For use with Vrapper, I mark the current word using viw (visually mark inner word) and then press the ZealLookup keyboard shortcut.

Tried with: ZealLookup 1.0.0, Eclipse 4.4.2 and Ubuntu 14.04

Include browser of Eclipse

Include graph of term.c in the Vim source code
Include graph of term.c in the Vim source code

The include graph of a large C++ project can be really gnarly. Sometimes, you end up with compile errors due to circular inclusion of types or headers and cannot figure out the origin of the error. Other times, you might want to know the path of header files through which a certain type is available in a source file. To view these details, I generate a header include graph using Doxygen.

However, if you are using Eclipse CDT you have something that is easier and more convenient: its Include Browser! 🙂

  • To open it: Go to Window -> Show View -> Include Browser. It will typically be displayed at the bottom of Eclipse.

  • To view the include graph of a source or header file: Go to the Project Explorer, find the file and drag its name into the Include Browser window.

  • By default, the include graph of the includes of the file are displayed.

  • You can also view which other files include the current file.

  • If the include graph is too cluttered, you can disable display of system includes. These are typically standard C++ header files.

  • You can also disable display of includes in inactive code.

Reference: Eclipse help on Include Browser

Tried with: Eclipse 4.4.2 and Ubuntu 14.04

How to toggle comments using Vrapper

Using plugins, a block of code can be commented or uncommented in Vim using the command gc. Similar capability to toggle comments is available in Vrapper, but only in the language specific addons.

For example, if you are working with Eclipse CDT (C/C++), then install the Vrapper – C/C++ Extensions plugin to get this capability.

Tried with: Vrapper 0.54.0, Eclipse CDT 4.4.2 and Ubuntu 14.04

How to set mark using Vrapper

Mark in Vrapper set as a Eclipse bookmark
Mark in Vrapper set as a Eclipse bookmark

Setting a mark using Vrapper is the same as in Vim. Press the m key followed by a lowercase or uppercase letter.

There is a slight difference. If you use a lowercase letter, the mark is set as an invisible mark, like in Vim. However, if you use an uppercase letter, the mark is also set as an Eclipse bookmark. It even appears in the bar on the left as a visual bookmark. Bookmarks set this way can also be viewed by using the Bookmarks view.

Tried with: Vrapper 0.52.1, Eclipse 4.4.1 (Luna) and Ubuntu 14.04

Link with Editor feature of Eclipse

20150301_link

The Project Explorer and Outline views in Eclipse have a Link with Editor feature that can be very useful. This can be enabled in either view, by clicking the down arrow button in the view and choosing Link with Editor (see screenshot above).

  • If Link with Editor is enabled in Project Explorer: The current file open in the Editor will be highlighted in Project Explorer.

  • If Link with Editor is enabled in Outline: The class or method at the location of the cursor will be highlighted in Outline.

Tried with: Eclipse 4.4.1 Luna and Ubuntu 14.04

How to setup Eclipse for Algorithms by Sedgewick

Prof. Sedgewick has written a popular Algorithms book and developed Algorithms I and II courses on Coursera. Both of these use Java and provide many source code files and assignments to the students. These have been developed for the Dr. Java IDE or to work from the commandline. If you prefer to work with Eclipse for this book or course, just follow these steps:

  • Install a Java SDK:
$ sudo apt install openjdk-7-jdk
  • Install Eclipse. Ubuntu ships with old versions of Eclipse. I prefer to install the latest from the Eclipse Downloads webpage. Get the one named Eclipse IDE for Java Developers.

  • Create a directory named algs4 anywhere under your home directory. This directory will be used to store the packages and configuration files required to work with this book or course.

  • Download the algs4.jar and stdlib.jar packages to the algs4 directory. These include the classes and methods you will need to work on the assignments.

  • Install the FindBugs plugin as described here. This will be useful to check your assignment code for bugs before you submit them. Download the findbugs.xml filter file to your algs4 directory. In the FindBugs preferences dialog, add this file as an Exclude filter file.

  • Install the CheckStyle plugin as described here. Your assignment submission has to adhere to the coding standard of this book or course and this plugin makes it easy to check that. Download the checkstyle.xml configuration file to your algs4 directory. Add this configuration file in the CheckStyle preferences.

  • Using this CheckStyle configuration file in Eclipse gave me these errors:

cannot initialize module TreeWalker - Unable to instantiate RedundantThrows
cannot initialize module TreeWalker - Unable to instantiate JUnitTestCase

I removed the lines in checkstyle.xml that used these modules and it worked after that. My version of this configuration file can be downloaded here.

  • You are now ready to work on the assignments! 🙂 Create a new Java project in Eclipse to work on each new assignment. Use the algs4.jar and stdlib.jar packages in the project by adding them in Properties -> Java Build Path -> Libraries -> Add External JARs.

  • Each assignment provides a barebones template of one or more Java classes. Choose New -> Java class to create a new Java file for each of these classes under the project for that assignment.

  • Important: When creating the Java class, make sure the Package entry is empty. This puts it under the default package. This is required since the algs4.jar and stdlib.jar also provides their classes under the default package. If you do not do this, you will get a cannot be resolved to a type error when you try to use any of the classes from this course.

  • Eclipse automatically compiles the code whenever you save the file. So, you can run the code as soon as you are done writing the code. Make sure to run the CheckStyle and FindBugs before you submit your code.

Tried with: CheckStyle 6.2, FindBugs 3.0.0, Eclipse 4.4.1 (Luna) and Ubuntu 14.04

How to use FindBugs plugin in Eclipse

FindBugs is a popular tool that is used to find bugs in Java source code. It uses static code analysis to find bugs. It is available as a plugin for Eclipse.

  • Install: To install this plugin, go to Help -> Install New Software, add the URL http://findbugs.cs.umd.edu/eclipse

  • The configuration options of this plugin can be found in Window -> Preferences -> Java -> FindBugs.

  • Filter files can be added to include or exclude classes or methods from these checks. This can be done in the above dialog too.

  • The same configuration can be done on a per-project level. Right-click on the project and choose Properties -> FindBugs. Choose the Enable project specific settings option.

  • To check the files of a project for bugs, right-click on the project and choose Properties -> Find Bugs -> Find Bugs.

Tried with: FindBugs 3.0.0, Eclipse 4.4.1 (Luna) and Ubuntu 14.04