Setting up include-what-you-need (IWYU)

Use following instructions to install IWYU (https://include-what-you-use.org/) on Ubuntu 18.04.3 LTS:

sudo apt-get update
sudo apt-get install -y build-essential cmake git zlib1g-dev libncurses5-dev llvm-6.0-dev libclang-6.0-dev libclang-6.0-dev clang-6.0
git clone https://github.com/include-what-you-use/include-what-you-use.git iwyu.git
cd iwyu.git
git checkout clang_6.0
mkdir -p build
cd build
cmake -DIWYU_LLVM_ROOT_PATH=/usr/lib/llvm-6.0 ..
make 
make install

Once installation is complete, you will find `include-what-you-need`

# include-what-you-use --version
include-what-you-use 0.10 (git:a1878c4) based on clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

To execute include-what-you-need:

Build your cpp project with cmake and pass on an extra parameter `-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE` to cmake. It will produce  `compile_commands.json` file in your build folder for the project.

  • Execute `iwyu_tool.py -p </path/to/folder/having/compile_command.json>`. It may take time depending upon the code size.
  • `fix_includes` can be used to apply recommended changes.

To use it in VSCode add following to the `tasks.json`

{
    "label": "CXX Analysis -- iwyu",
    "detail": "Run IWYU tool for CPP project",
    "command": "iwyu_tool.py",
    "args": [
        "-p",
        "${workspaceRoot}/build"
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": true,
        "panel": "shared",
        "showReuseMessage": true,
        "clear": false
    },
    "problemMatcher": "$gcc"
}

Ref: https://gist.github.com/aakbar5/268a2072138345893b7f82590dcc3d26

GCC – Dump default compile macros

Use following commands if you want to see default macros passed by the compiler  during compilation:

# Compiler default macros in standard build
gcc -dM -E - < /dev/null

# Compiler default macros with optimization level 0
gcc -dM -E -O0 - < /dev/null

# Compiler default macros in case of c++11
g++ -dM -E -x c++ -std=c++11 - < /dev/null

Thread Module in c++11

C++11 supports Thread library (http://en.cppreference.com/w/cpp/thread). If you are using it in your application you have to pass -pthread to g++ linker to inform it which sort of backend should be used for linking Thread library. If -pthread is not passed your application will end up with following execution error:

terminate called after throwing an instance of 'std::system_error'
 what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

 

Qt Creator was set to -std=gnu++0x

I was playing with c++11/14 in a project created using Qt Creator. I found that Qt creator is passing -std=gnu++0x to compiler whereas my .pro file was configured to use c++11.

My <qt_project>.pro file was having:

CONFIG += c++11

The fix for this problem was to force Qt creator to use specified c++ standard instead of using default one. This can be done by having strict_c++ keyword in .pro file.

CONFIG += strict_c++ c++11