vscode_wsl

Dec 2, 2023

VSCode WSL CMake

Inorder to take place Clion as IDE to run and debug for Cpp, I choose VSCode because it is totally free.

After VSCode install C++ extension, it can play well at Windows platform, perfect writing code, snippet highlight immediatly.

Clion also highlight snippet well at Mac, the way by remoting to local docker SSH, but Windows fail to support this wonderful technique. AFter tunning to use VSCode Windows remote to WSL-ubuntu, it can perfect hightlight the snippet, but frankly to say, my computer is more busy, another word - CPU high.

If you prefer using an edit tool, build in terminal, gdb in terminal, this will be like this:

1
2
3
4
5
6
7
$>cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug
$>cmake --build cmake-build-debug
$>gdb demo

$gdb>set env LD_LIBRARY_PATH ../libs/aarch64_android:libs/aarch64_android
$gdb>break main
$gdb>r

But is you can simple type F5 keyboard to start the whole process flow, a seamless collaboration between you and computer greatly improve the job efficiancy.

I think you can choose respective benefit to make your own decision, or fix it’s trouble(Such as can’t highlight, or CPU perform high, or any others)? Next, let me introduce you how to config VSCode WSL CMake:

VSCode Remote WSL

VSCode install extension C++, you can follow vscode wsl docs, including install WSL-ubuntu, a quick start sample, this will not introduce again.

In addition, if WSL-ubuntu change user, code command may disappear, an export command can help you define the comamdn code: export PATH=$PATH:"/mnt/c/Users/%USERNAME%/AppData/Local/Programs/Microsoft VS Code/bin"

After complete this phase, a small project can easily be started and examine, but how about a more complex task? Let’s go to next phase, let’s use CMake to constitude the project:

VSCode CMake

AS an example, I will introduce you with three key files: CMakeLists.txt / .vscode/tasks.json / .vscode/launch.json.

CMakeLists.txt just fill use what you want to config for you project, a simple sample:

1
2
3
4
5
6
7
cmake_minimum_required(VERSION 3.6)
project(app)

aux_source_directory(. SRCS)
add_executable(app ${SRCS} http/http_conn.cpp log/log.cpp timer/lst_timer.cpp CGImysql/sql_connection_pool.cpp)

target_link_libraries(app -lpthread -lmysqlclient)

./vscode/tasks.json help define three key tasks: cmake, make, CMake Build. Sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}"
},

"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-S",
".",
"-B",
"cmake-build-debug",
"-DCMAKE_BUILD_TYPE=Debug"
]
},
{
"label": "make",
"dependsOn": ["cmake"],
"command": "cmake",
"args":[
"--build",
"cmake-build-debug"
]
},
{
"label":"CMake Build",
"dependsOn":[
"cmake",
"make"
]
}
]
}

launch.json defines for VSCode how to start the project. In this sample, launch.json contains important fields: type / request / program / cwd / miDebuggerPath:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "cppdbg", // vscode插件需要下载对应的插件
"request": "launch",
"program": "${workspaceFolder}/cmake-build-debug/app", // program启动声明需要调用的exe文件
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/cmake-build-debug",
"environment": [],
"externalConsole": false,
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "CMake Build" // 断点启动前执行的任务,对应tasks的label。
}
]
}

After this phase, your program can simply run or debug, just take a try.

Ref

vscode wsl

VSCode的远程连接和CMAKE项目调试

20221121window_cmake