Building Clickhouse development environment on Windows system
General idea
Microsoft's development IDE is great. There are two kinds: Visual Studio and VS Code, one heavyweight and one lightweight. In recent years, VS Code has become more and more popular because of its lightweight and rich plug-ins. More importantly, VS Code consumes less resources and will not crash when opening large projects. Therefore, VS Code is selected.
Clickhouse can only be compiled and run on Linux and MacOS, and the development machine is Windows 10 system, so virtual machine or WSL is required. WSL is a Windows subsystem Linux, which is natively supported by Windows 10, so WSL is adopted.
GDB, the most commonly used GDB under linux, is used for debugging clickhouse in WSL environment, while the development environment runs on Windows, which requires remote connection to GDB.
Here are the specific steps.
Build the debug build of clickhouse
By default, Clickhouse builds a complete release version running file through static links: Clickhouse, with a size of more than 1G. This huge single release executable is not conducive to debugging. In order to develop and debug, we need symbol files. We need to break up the single giant files into a group of small dynamic link library files, so we need special cmake construction parameters.
Perform the following steps to complete the debug build. If there is a problem, refer to the official clickhouse documentation Build on Linux | ClickHouse Documentation
-
Install WSL
Run wsl --install in administrator PowerShell. If there is a problem, please refer to the official Microsoft documentation Install WSL | Microsoft Docs -
Install the build tools cmake and ninja
In the WSL environment, run sudo apt get install - y git cmake Python Ninja build. If there is a problem, refer to google. -
Install the clang compiler, which is said to be more efficient than gcc
In the WSL environment, run sudo bash - C "$(WGet - O- https://apt.llvm.org/llvm.sh )" -
Download the clickhouse code
git clone https://github.com/ClickHouse/ClickHouse.git cd ClickHouse git submodule update --init --recursive
-
Add parameters to compile the version of clickhouse suitable for development and debugging
export CC=clang-13 export CXX=clang++-13 cd ClickHouse mkdir build cd build cmake .. \ -DUSE_STATIC_LIBRARIES=0 \ -DSPLIT_SHARED_LIBRARIES=1 \ -DCLICKHOUSE_SPLIT_BINARY=1 \ -DCMAKE_BUILD_TYPE=Debug ninja
USE_STATIC_LIBRARIES=0 -- do not compile into static links
SPLIT_SHARED_LIBRARIES=1 -- split into shared libraries
CLICKHOUSE_SPLIT_BINARY=1 -- unpack compiled binary files
-
(optional) compile only clickhouse client and server
cmake .. \ -DCMAKE_C_COMPILER=$(which clang-13) \ -DCMAKE_CXX_COMPILER=$(which clang++-13) \ -DCMAKE_BUILD_TYPE=Debug \ -DENABLE_CLICKHOUSE_ALL=OFF \ -DENABLE_CLICKHOUSE_SERVER=ON \ -DENABLE_CLICKHOUSE_CLIENT=ON \ -DENABLE_LIBRARIES=OFF \ -DUSE_UNWIND=ON \ -DENABLE_UTILS=OFF \ -DENABLE_TESTS=OFF \ -DUSE_STATIC_LIBRARIES=0 \ -DSPLIT_SHARED_LIBRARIES=1 \ -DCLICKHOUSE_SPLIT_BINARY=1
Configure development environment
Install VS Code
Download and run VSCode from Microsoft's official website. I won't repeat it.
After installing VS Code, open VS Code and install the C + + plug-in: Microsoft C/C + +.
Associate a code warehouse in WSL
Because it is compiled and run in wsl and developed in windows, both sides should be synchronized. The best way is to use VS Code in Windows environment. The modified code is the code compiled and run in wsl. Fortunately, in the windows environment, we can directly access the file system in wsl through \ \ wsl $, and then through the windows function of mapping the network path into a drive letter, we can access the file system in wsl like accessing the local disk.
In this way, the / home / Alex / depot / CH Pro code directory in WSL is mapped to Z: \ home \ Alex \ depot \ ch Pro code directory. Open the Z: \ home \ Alex \ depot \ ch Pro directory with VS Code. Modifying its code will directly modify the code in WSL.
VS Code debug run configuration
Install gdb in WSL environment, apt install gdb
Important: create a new running configuration in VS Code. / vscode/launch.json.
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(clickhouse) start-up", "type": "cppdbg", "request": "launch", "program": "/home/alex/depot/ch-pro/build/programs/clickhouse-server", "args": [], "stopAtEntry": false, "cwd": "/home/alex/depot/ch-pro", "environment": [{"name": "CLICKHOUSE_WATCHDOG_ENABLE", "value":0}], "externalConsole": true, "windows": { "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "pipeTransport": { "pipeCwd": "", "pipeProgram": "C:\\Windows\\System32\\bash.exe", "pipeArgs": ["-c"], "debuggerPath": "/usr/bin/gdb" }, "sourceFileMap": { "/mnt/c": "C:\\", "/usr": "Z:\\usr", "/home": "Z:\\home" } } ] }
be careful
"environment": [{"name": "CLICKHOUSE_WATCHDOG_ENABLE", "value":0}] this line is particularly important. It is thought that the Clickhouse not started from terminal will turn the startup process into a watch dog process, and start another process as the real Clickhouse process. Then the process attach ed during debugging is the startup process, that is, the watch dog process, Unable to debug real Clickhouse code. The environment variable Clickhouse must be set_ WATCHDOG_ Enable = 0 to prevent Clickhouse from doing so.
Remotely start clickhouse and debug, as shown in the figure:
When you reach the breakpoint and interrupt the operation successfully, you will be prompted that the source code file cannot be found. You need to set the gdb setting, set substitute path < from_ path> <to_ Path > add address translation.
Under normal circumstances, you can debug at breakpoints.
After modifying the code, if files are added or deleted, you need to run cmake again. If you only modify files, you only need to run ninja. Ninja compiles only modified files.