How to use Quickfix feature of Vim for compilation

Compile, view errors and fix them from inside Vim using Quickfix
Compile, view errors and fix them from inside Vim using Quickfix

The Quickfix feature of Vim is great if you work regularly with source code of languages which need to be compiled, like C or C++. If you are coming from Visual Studio or Eclipse CDT, the Quickfix feature basically gives an IDE-like feel of the edit-compile-fix cycle. (The only missing piece is a visual debugger inside Vim.) For example, you can view the errors and warnings produced on compiling your code in the Quickfix window, jump to the location of the error or warning, fix them and compile the code again. All from within Vim! 🙂

Here are some pointers on how I use Quickfix:

  • Quickfix is a plugin that ships with Vim. So, there is nothing to install!

  • If you have a compile command, then redirect its output to a file. Use the -q option of Vim to open this file and you can navigate the errors from there (as described later below). For example:

$ g++ main.cpp 2> errors.txt
$ vim -q errors.txt
  • If you are using CMake or Make, then life is much easier for you. Open Vim in build directory and invoke make from within it using the command :make. This runs Make and fills up the Quickfix buffer with the error output.

  • To open the Quickfix window, use the command :copen. In the screenshot above, the Quickfix window is visible at the bottom.

  • Navigate the Quickfix window as usual and press Enter on any error or warning line. That file, which may not even be in the same directory, is opened in the top window and the cursor is placed at the line and column of the error. Now, is that not like an IDE! 🙂

  • Now you can jump between the editor window and Quickfix window, using the usual Ctrl + w + w command and fix all the errors.

  • Once done, you can compile again right from inside Vim using :make. More errors? Fix them. And the edit-compile-edit cycle continues 🙂

  • For more help on Quickfix, see :help quickfix

Tried with: Vim 7.4 and Ubuntu 14.04

How to change make location in Eclipse

One of the easiest ways to use existing C++ code in Eclipse is to use the Makefile project. This enables you to use the existing source files right from where they are. However, Eclipse assumes that the Makefile location is at the root of the directory you used to create the project.

Configuring Eclipse to use a Makefile that is located somewhere else in the project directory is easy:

  1. Go to Project > Properties > C/C++ Build

  2. In the Builder Settings > Build location > Build directory entry, you will find ${workspace_loc:/foobar} by default. Here foobar is an example project name. By default, when you build this project, Eclipse will run make from this directory.

  3. To configure Eclipse to use the Makefile that is a different location inside the project directory, change this entry. For example, if my Makefile is located in a build subdirectory, then I can change this to ${workspace_loc:/foobar}/build

That is it! You should be able to build your Makefile project no matter where the file is located now 🙂

Tried with: Eclipse 3.7.2 and Ubuntu 12.04 LTS

How to pipe make output to less

When you compile your code using make it can throw output that can run many lines. Piping this output directly to a pager like less does not work. This is because make is writing its output to the stderr stream. To get it into less this needs to be redirected to the stdout stream. That can be done like this:

$ make 2>&1 | less

How to view commands executed by make

When you run make to build a project, sometimes not all the commands or internal workings are visible at the shell. This is especially common if your Makefile is generated by CMake. It hides all the commands that are actually being executed to compile the files of the project. Here are some methods to print what make is doing:

  • For Makefile generated by CMake, try this when you build using make:
$ make VERBOSE=1
  • For normal Makefile, try this:
$ make SHELL='sh -x'

This prints out all the commands executed by make preceded by a plus.

  • With newer versions of make, tracing is available as an option:
$ make --trace

Tried with: GNU Make 4.1 and Ubuntu 16.04

How to build CUDA programs using CMake

CMake is a popular option for cross-platform compilation of code. CMake has support for CUDA built in, so it is pretty easy to build CUDA source files using it.

Let us assume that I want to build a CUDA source file named src/ To build this using CMake, we create a file named CMakeLists.txt containing these lines:

# CMakeLists.txt to build
cmake_minimum_required(VERSION 2.8)

# Specify binary name and source file to build it from

To build our program on Linux, we first run cmake to generate a Makefile and use that Makefile to build and run. On Windows, cmake would generate a Visual Studio solution, that can be used to build the code.

Steps to build and run program on Linux:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
$ ./hellocuda

Any non-trivial CUDA program will need special compilation flags, include directories, library directories and multiple source files. The CMakeLists.txt shown below is an example of how to configure all these features:

Typically nvcc calls both the host compiler and device compiler by itself on both .cu and .cpp files. The behavior of the Makefile generated by CMake is slightly different: it calls the default C++ compiler for .cpp files and calls nvcc for .cu files. This does not cause any problems, just something you might need to be aware of.

For information about other CMake commands for CUDA see the file /usr/share/cmake-2.8/Modules/FindCUDA.cmake and the files inside /usr/share/cmake-2.8/Modules/FindCUDA/.

Note: Include directories cannot be specified for a particular target using target_include_directories. This is a known limitation of the CUDA module of CMake currently. More details here.

Tried with: CMake 2.8.7, CUDA 5.0 and Ubuntu 12.04 LTS

Make error with cpan

The first time you try to install a Perl package from CPAN, you might get this error:

Writing MYMETA.yml
  make -- NOT OK
Running make test
  Can't test without successful make
Running make install
  Make had returned bad status, install seems impossible

The fix is simple: you need to install the make package. This error typically occurs on Cygwin, which does not have make installed by default. Install make and then try to install the package again and it should work now.

Tried with: Cygwin x86 1.7.25