抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

记录 Ubuntu 下配置 VSCode + CMake 过程,存在多个可执行文件。


前期准备

  • Ubuntu 22.04
  • VSCode 1.83
  • g++ 11.4
  • cmake 3.22.1
  • VSCode Extension
    • C/C++ 1.19.9
    • CMake 0.0.17
    • 不安装 CMake Tools
  • CMake 项目,其中包含多个可执行文件,存在 CMake 嵌套

CMake 项目

目录结构

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
.
├── CMakeLists.txt
├── include
│   └── efanna2e
│   ├── distance.h
│   ├── dkm.hpp
│   ├── dkm.hpp.bak
│   ├── exceptions.h
│   ├── index.h
│   ├── index_nsg.h
│   ├── index_wadg.h
│   ├── lru_cache.h
│   ├── neighbor.h
│   ├── parameters.h
│   └── util.h
├── src
│   ├── CMakeLists.txt
│   ├── index.cpp
│   ├── index_nsg.cpp
│   ├── index_wadg.cpp
│   ├── lru_cache.cpp
├── tests
│   ├── CMakeLists.txt
│   ├── test_cal_top_K_precision.cpp
│   ├── test_nsg_index.cpp
│   ├── test_nsg_optimized_search.cpp
│   ├── test_nsg_search.cpp
│   └── test_wadg_search.cpp

存在 CMake 的多级嵌套,其中 ./tests 文件夹中的源文件都存在 main 函数,需要分别编译为可执行文件。

编译命令

1
2
3
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j

执行命令

1
2
3
4
5
test_wadg_search ../../dataset/sift/sift_base.fvecs ../../dataset/sift/sift_query.fvecs ../../nsg_graph/sift.nsg 200 200 ../../search_result/sift_200nn.ivecs

test_nsg_search ../../dataset/sift/sift_base.fvecs ../../dataset/sift/sift_query.fvecs ../../nsg_graph/sift.nsg 200 200 ../../search_result/sift_200nn.ivecs

test_cal_top_K_precision ../../search_result/sift_200nn.ivecs ../../dataset/sift/sift_groundtruth.ivecs 100

VSCode 配置

编译可以使用 CMake Tools 插件,但是这样的话 launch.jsontasks.json 不起作用,所以打算手动配置。

加入头文件路径

配置 c_cpp_properties.json 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// 加入项目中的头文件
"${workspaceFolder}/include"
],
"defines": [],
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

编译

配置 tasks.json 文件,自定义任务,使用CMake 编译命令编译程序。

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
38
39
40
41
42
43
44
45
// .vscode/tasks.json
{
"version": "2.0.0",
"options": {
// 指定工作目录
"cwd": "${workspaceFolder}/build"
},
"tasks": [
// 任务 1
{
"type": "shell",
"label": "cmake",
// 命令
"command": "cmake",
// 参数
"args": [
// Release, Debug
"-DCMAKE_BUILD_TYPE=Debug", ".."
]
},
// 任务 2
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
// 命令
"command": "make",
// 参数
"args": [
"-j"
]
},
{
"label": "Build",
// 依赖于上面两个 task 命令
"dependsOn": [
// 与 label 一致
"cmake",
"make"
]
}
]
}

运行调试

配置 launch.json 文件,读取可执行文件。需要进行修改地方的是指定运行的文件,其次我们还可以在里面添加 build 任务,用于调试,添加命令行参数执行程序。

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{
"version": "0.2.0",
"configurations": [
// 配置 1,运行可执行文件 1
{
// name 可自定义
"name": "test_wadg_search",
"type": "cppdbg",
"request": "launch",
// 需要运行的可执行文件
"program": "${workspaceFolder}/build/tests/test_wadg_search",
// 需要输入的命令行参数
"args": [
// data_file
"./dataset/sift/sift_base.fvecs",
// query_file
"./dataset/sift/sift_query.fvecs",
// nsg_path
"./nsg_graph/sift.nsg",
// search_L
"200",
// search_K
"200",
// result_path
"./search_result/sift_200nn.ivecs"
],
"stopAtEntry": false,
// 指定工作目录。如果参数包含文件路径,需要注意相对路径
"cwd": "${workspaceFolder}",
"environment": [],
// 终端
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
// 配置 2,运行可执行文件 2
{
"name": "test_nsg_search",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/test_nsg_search",
"args": [
// data_file
"./dataset/sift/sift_base.fvecs",
// query_file
"./dataset/sift/sift_query.fvecs",
// nsg_path
"./nsg_graph/sift.nsg",
// search_L
"200",
// search_K
"200",
// result_path
"./search_result/sift_200nn.ivecs"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
// 配置 3,运行可执行文件 3
{
"name": "test_cal_top_K_precision",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/test_cal_top_K_precision",
"args": [
"./search_result/sift_200nn.ivecs",
"./dataset/sift/sift_groundtruth.ivecs",
"100"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

运行选择右上角的图标,选择相应的可执行文件配置运行即可。