This article describes how to compile the Redis project and run/debug it in JetBrains CLion.

0. Before you start

JSON Compilation Database

CLion supports CMake built projects by default, but Redis projects are built on Make. For projects built with Make, CLion can still import the project through the Compilation Database without changing it to a CMake project. At the same time, code analysis, jump and other functions can be realized through the Compilation Database, which is very helpful for our code debugging.

For the introduction of Compilation Database, please refer to these articles:

  • ClangThe officialCompilation DatabaseIntroduction page:JSON Compilation Database Format Specification — Clang 11 Documentation
  • This article describes generation in different scenariosCompilation databaseA variety of tools:Compilation Database — Sarcasm notebook
  • CLionThe help page is briefly introducedCompilation DatabaseAnd, inCLionThe use of:Compilation Database – Help | CLion

For Make-based Redis projects, there are tools that can generate the Compilation Database, such as the following:

  • Github.com/rizsotto/Be…
  • Github.com/rizsotto/sc…
  • Github.com/nickdiego/c…

The Compilation Database is usually generated by default as a compile_commands. Json file.

1. Download the Redis source code

git clone [email protected]:redis-io/redis.git
cd redis
You can switch to the branch corresponding to the specified versionGit checkout 5.0Copy the code

Compile and build Redis

Redis is built based on Make. Run the Make command to complete the build. Redis is optimized at the -O2 level by default. You can use make noopt to disable optimization for more debugging information.


We need to use tools to generate the Compilation Database so that CLion can be easily imported. The installation of these tools may vary on different operating systems. The following describes how to compile Redis and generate the Compilation Database on Ubuntu 16.04 and MacOS 10.15.

Ubuntu 16.04

Use the Bear tool

Download the Bear tool
sudo apt-get install bear
# Compile and build, and use Bear tool to generate Compilation Database
bear make noopt
Copy the code

Use the compiledb tool

# installation PIP
sudo apt-get install python-pip
# PIP install compiledb
pip install compiledb
# Compile build and generate Compilation Database using compiledb
compiledb make noopt
Copy the code

MacOS 10.15

Use the Bear tool

To disable SIP using Bear on MacOS, enter the recovery mode, run the csrutil disable command, and then restart it. For details, search by yourself.

To install Bear, use Homebrew. If Homebrew is not installed, install Homebrew first.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Copy the code

Start compiling Redis:

Download the Bear tool
brew install bear
# Compile and build, and use Bear tool to generate Compilation Database
bear make noopt
Copy the code

C :32:10: FATAL error: ‘stdlib.h’ file not found If an error message is displayed, run the following command and try again:

sudo mount -uw /
sudo cp -R /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include /usr
Copy the code

Use the compiledb tool

To install the COMPILedb, you need to use PIP. If you do not have PIP installed, you need to install PIP first.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
You may also need to set environment variables after installing PIP
Copy the code

Start compiling Redis:

# PIP install compiledb
pip install compiledb
# Compile build and generate Compilation Database using compiledb
compiledb make noopt
Copy the code

After compiling, you can verify the success of the following:

$./ SRC /redis-server --version redis server v= 5.0.8sha =1f7d08b7:1 malloc=libc bits=64 build=3530fd9b48a55c7fCopy the code

There should also be a non-empty compile_commands. Json file in the root directory.

Import CLion and debug it

Managing Makefile Projects In CLion is described in detail in CLion’s official help document: Managing Makefile Projects. This section briefly describes how to import a Redis project in CLion and run/debug it.

  1. Download and install CLion and install the Makefile Support plug-in. The latest version is recommended; older versions do not support make-built projects.

  2. Import the project. Open CLion, select Open Or Import, select compile_commands. Json in the Project directory, select Open as Project, and wait for the file to be indexed.

  3. Create a custom Build Target. Click on the File menu bar, Settings | Build, Execution, Deployment | Custom Build Targets, click + to create a new Target.

    • Name:TargetAfter the name is createdRun/DebugYou’ll see that name when you configure it
    • Click on theBuildorCleanThree points on the right, click in the popup+Two newExternal ToolThe first configuration is used to specify the build directive. Program and Arguments together form the “make noopt” command to execute.
      Name: make
      Program: make
      Arguments: noopt
      Working directory: $ProjectFileDir$
      Copy the code

      The second configuration is as follows to clean up the build output. Program and Arguments together make up the command to execute “make clean”

      Name: make clean
      Program: make
      Arguments: clean
      Working directory: $ProjectFileDir$
      Copy the code
    • ToolChainchooseDefault;BuildchoosemakeThe first one created aboveExternal Tool);Cleanchoosemake clean(The second created aboveExternal Tool)
  4. Create a custom Run/Debug configuration. Click on the Run menu bar,Edit Configurations, click + and select Custom Build Application. The configuration is as follows:

    # Name: Name of Configure
    Name: redis
    # Target: Select the "Custom Build Target" created in the previous step
    Target: redis
    Executable: Program execution entry, that is, the program that needs debuggingExecutable: Here we debug Redis Server by selecting '{source_root}/ SRC /redis-server'.# Program arguments: Used with "Executable" to specify its argumentsProgram Arguments: Here we choose"$ProjectFileDir$/redis.conf"Start the Redis server as a configuration fileCopy the code

    Executable and Program Arguments can be set according to the information needed to debug.

    If you don’t want to do the Build operation Before every run/debug (in this case, the Make Build process), you can delete the Build entry in the Before Launch box at the bottom of the edit page.

  5. Click Run/Debug to start running/debugging.

reference

  1. Redis debugging guide
  2. nickdiego/compiledb: Tool for generating Clang’s JSON Compilation Database files for make-based build systems.
  3. Managing Makefile Projects
  4. Dealing with Makefile Projects in CLion: Status Update