Generating compile commands with cmake

cmake offers the option to generate a compilation database in json format using the option CMAKE_EXPORT_COMPILE_COMMANDS=YES. The database holds essentially the command, paths and compilers flags used to compile each file of a project. The generated file (usually called compile_commands.json) can be used in conjunction with various code completion tools like YouCompleteMe and Visual Studio’s IntelliSense in order to offer more complete information, completion and errors about the code.

The official cmake tutorial

There are many resources, posts and articles about using cmake and writing modern cmake code. Unfortunately for a new user it is sometimes difficult to locate the proper resource that contains no deprecated options or any other non-recommended practices. One of the quickest and simplest resource I became aware recently is the official tutorial that lives in the documentation of cmake.

Using ccache to speedup compilation of C/C++

ccache is a utility that caches intermediate files during compilation offering significant speedups. It is especially useful if you are switching between two branches of the same project see below.

To use it, prepend it before the compiler i.e. Use ccache gcc instead of gcc ... . If you are using cmake with a version greater that 3.4 you can use the option -DCMAKE_CXX_COMPILER_LAUNCHER=ccache for older version you can modify the compiler settings or use the variable RULE_LAUNCH_COMPILE for more see here.

[Read More]

Thousands separator in C++

As you print more and more numbers in the screen it may become cumbersome to count digits just to see is if you are in the range of millions or 100’s of millions. Visually grouping digits can simmplify things. There is a pleathora of ways to do this in C++ but the one I find the best is the one provided by the standand C++ library through the std::numpunct object. Here is a small example that uses groups digits using commas.

[Read More]

Keep running until crash with gdb

Many times when trying to catch a bug , or to validate that your parallel code runs correctly you wish to run your code again and again and again until break on an assertion or even better until you persuade yourself that your code is good enough. Once more, stakoverflow has the answer for us:

set $n = 100
while $n-- > 0
  printf "starting program\n"
  run
  if $_siginfo
    printf "Received signal %d, stopping\n", $_siginfo.si_signo
    loop_break
  else
    printf "program exited\n"
  end
end

If you use it often save it in a file and launch it using

[Read More]

Type selector using template specialization

In an effort to contribute back to the great matplotlib-cpp library I came across the need of having to create a mechanism that can select between different types in C++. This was needed in order to allow passing data from C++ to python without any copies using the numpy library. The only caveat in this case in that numpy requires to know the type you are passing. Since the matplotlib-cpp library is template-based there is a nice compile-time way to do it using template specialization. In simple terms it boils down to creating a base case for all the types you do not support and specialize for the types you care. Using a simple struct to hold the type completes the whole construct. The final result is :

[Read More]

Usefull GCC compilation flags

Having the -Wall flag enabled by default can potentially save lots of time and headaches when programming in C/C++. Same other usefull flags are :

  • -Wduplicated-cond : Warn about duplicated condition in if-else-if chains.
  • -Wduplicated-branches : Warn when an if-else has identical branches.
  • -Wlogical-op : Warn about use of logical operations where a bitwise operation probably was intended.
  • -Wnull-dereference: Warn when the compiler detects paths that dereferences a null pointer.
  • -Wjump-misses-init: Warn if a goto statement or a switch statement jumps forward across the initialization of a variable, or jumps backward to a label after the variable has been initialized.

source

[Read More]