Setting up cppcheck

To install pre-built cppcheck: http://cppcheck.sourceforge.net/

sudo apt-get update
sudo apt-get install cppcheck

In case, you want to use latest cppcheck, download and build it from the source code

wget https://github.com/danmar/cppcheck/archive/2.1.tar.gz
tar zxvf 2.1.tar.gz
cd cppcheck-2.1
mkdir build
cd build
cmake ..

P.S: In case you are interested for cppcheck GUI use cmake -DBUILD_GUI=ON ..

make
sudo make install

Once installation is complete.

which cppcheck
/usr/local/bin/cppcheck

cppcheck --version
Cppcheck 2.1

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 cppcheck -p </path/to/folder/having/compile_command.json>.

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

{
    "label": "Run cppCheck",
    "command": "/usr/local/bin/cppcheck",
    "args": [
       "--project=${workspaceRoot}/build/compile_commands.json"
    ],
    "problemMatcher": "$gcc",
}

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

dup2 usage

// A simple example of how dup2 can be used
const char* msg = "Text written by standard file descriptor\n";
int fd = open("dup2_test.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
write(fd, msg, strlen(msg));
/* Map stdout to our file */
dup2(fd, STDOUT_FILENO);
printf("Text written by dup @ stdout file descriptor\n");
close(fd);

view raw
dup2_usage.c
hosted with ❤ by GitHub

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": "Run iwyu",
    "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

VisualStudio code & compile_commands.json

If you are working with C/C++ in VSCode, you will find that VSCode keeps on showing

"${workspaceFolder}/build/compile_commands.json" could not be found. 'includePath' from c_cpp_properties.json will be used instead.

This message is generated by vscode-cpptools. Although it is an optional file however vscode-cpptools keeps on showing error message that it is missing.

This message can easily be fixed if you are using CMake. To do so simply add following to your CMake configuration file.

# Generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

enum as bitfield

By default enum can't be as bitfield
typedef enum _type_ {
CAT = 0,
DOG,
UNKNOWN
} TYPE;
typedef struct _animal {
TYPE type : 2;
};
GCC ((Sourcery CodeBench 2015.17-10) 5.2.0) will show error "width of 'type' exceeds its type"
Use -fno-short-enums to get rid of this. Details: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0774b/chr1411640303038.html

view raw
enum as bitfield
hosted with ❤ by GitHub

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)