VSCode debugging file launch.json, tasks.json configuration, CMakeLists.txt template

Note: study notes, because it is cross-platform (Linux virtual machine and windows copy content is troublesome, so use this method to take some notes)

The learning video is: https://www.bilibili.com/video/BV1fy4y1b7TC?p=24&spm_id_from=333.880.my_history.page.click

launch是VSCode的启动程序配置文件
tasks是编译的任务的文件,配置好的tasks文件相当于运行了g++ -g main.cpp -o main
调试的时候,这两个文件配合就可以实现自动调试(不需要手动编译了)

CMakeLists.txt template

This is a CMake code template configured in VS Code. If you use any options, you can copy and paste them and uncomment them. It
may not be complete in normal use, and changes will be added slowly. Copy it
to the CMake user code template to use it.
Please add a picture descriptionPlease add a picture description
Please add a picture description
The same reason , you can also find code snippets of other files such as C/C++, python and other languages ​​in the user code snippets, which will not be described in detail here

"Print to console":{
    
    
		"prefix": "CMake",
		"body": [
			"# 指定cmake最低版本",
			"cmake_minimum_required(VERSION 3.0)",
			"",

			"# 项目名称:project(MAIN)",
			"project(${1:PROJECT_NAME})",
			"",

			"# 有些变量可能和add_compile_options中的重复,要注意",
			"# CMAKE_C_FLAGS:gcc编译选项",
			"# CMAKE_CXX_FLAGS:g++编译选项;\"${CMAKE_CXX_FLAGS} -std=c++11\":设置编译标准为c++11",
			"# CMAKE_BUILD_TYPE:编译类型;(Debug:调试;Release:发布)",
			"# CMAKE_C_COMPILER:指定c编译器",
			"# CMAKE_CXX_COMPILER:指定c++编译器",
			"# EXECUTABLE_OUTPUT_PATH:可执行文件输出存放路径",
			"# LIBRARY_OUTPUT_PATH:库文件输出存放的路径",
			"# set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++11\")",
			"# set(CMAKE_BUILD_TYPE Debug or Release)",

			"# 添加编译选项:add_compile_options(-Wall -g)",
			"# -g:输出带调试信息的可执行文件",
			"# -O[n]:优化代码,一般为-O2",
			"# -Wall:输出警告信息",
			"# -std=c++11:设置编译标准",
			"# -D:定义宏",
			"# add_compile_options(-Wall -g)",
			"",

			"# 添加头文件搜索路径:include_directories(./include)",
			"# include_directories(include_PATH)",
			"",

			"# 添加多个特定的库文件搜索路径:link_directories(LIB_PATH1 LIB_PATH2 ...)",
			"# link_directories(LIB_PATH)",
			"",

			"# 相当前工程添加存放源文件的子目录,并可以指定中间二进制和目标二进制存放的位置:add_subdirectory(source_dir [binary_dir])",
			"# add_subdirectory(source_dir)",
			"",

			"# 生成库文件:add_library(libname MODE source1 source2 ...)",
			"# add_library(libname MODE source1 source2 ...)",
			"# libname:生成库的名称",
			"# MODE:库的模式:{SHARED:动态库; STATIC:静态库}",
			"# source:源文件,可以使用set设置的变量代替",
			"# add_library(libname MODE source)",
			"",

			"# 设置变量名称:set(VALUE_NAME ./src/test01.cpp ./src/test02.cpp);${}:获取变量的值",
			"# 就是使用一个变量名称代替多个cpp文件,方便调用",
			"# set(VALUE_NAME ./src/test01.cpp)",
			"",
			
			"# 发现一个目录下所有的源代码文件放置到一个列表中,并将该列表存储到一个变量中:aux_source_directory(PATH VALUE_NAME)",
			"# PATH:源文件的路径",
			"# VALUE_NAME:生成的变量名",
			"# 如果main函数的源文件也在该路经中保存,那么直接可用add_executable(target ${VALUE_NAME})生成可执行的目标文件",
			"# aux_source_directory(PATH VALUE_NAME)",
			"",

			"# 编译可执行文件:add_executable(target main.cpp ${VALUE_NAME});${}:获取变量的值",
			"# add_executable(target main.cpp ${VALUE_NAME})",
			"",

			"# 为可执行文件链接共享库:target_link_lib(target library1 library2 ...)",
			"# target_link_lib(target library1)",
			"",

		],
		"description": "CMake framework"
	}

VSCode debug launch.json configuration

This is the configuration template of the launch.json file seen during the learning process.
The launch file is an important file used in the debugging process,
and the notes in it will be filled in slowly.

{
    
    
  "version": "0.2.0",
  "configurations": [
    {
    
    
      "name": "g++ - 生成和调试活动文件", // 配置名称,显示在启动配置下拉菜单中
      "type": "cppdbg", //配置类型
      "request": "launch", //请求配置类型
      "program": "${workspaceFolder}/build/main", //代表的可执行文件的绝对路径
      "args": [], //传递给程序的命令行参数
      "stopAtEntry": false, //可选参数。如果为true,则调试程序应在目标的入口点处停止。如果床底了processId,则不起任何作用
      "cwd": "${workspaceFolder}", //cd到工程的顶层目录
      "environment": [], //要添加到程序中的环境变量{"name":"config","value":"Debug"}
      "externalConsole": false, //true:启动控制台;false:在vscode的集成的控制台显示
      "MIMode": "gdb", //调试方式
      //
      "setupCommands": [
        {
    
    
          "description": "为 gdb 启用整齐打印", //
          "text": "-enable-pretty-printing", //
          "ignoreFailures": true //
        }
      ],
      "preLaunchTask": "Build", //调试前作的一个task,联合tasks.json进行自动化编译后调试
      "miDebuggerPath": "/usr/bin/gdb" //调试程序的路径,要知道自己把调试程序安装到哪里了
    }
  ]
}

VSCode debugging tasks.json configuration

{
    
    
	"version": "2.0.0",
	"options": {
    
    
		"cwd": "${workspaceFolder}/build" //进入build文件夹的绝对路径
	},
	//一个tasks中包含着三个小task
	"tasks": [
		{
    
    
			"type": "shell", //定义任务是被认为是进程还是在shell中运行
			"label": "cmake", //task名称
			"command": "cmake", //task命令
			//调用命令时传递的参数
			"args": [
				".."
			]
		},
		{
    
    
			"label": "make", //task名称
			"group": {
    
    
				"kind": "build",
				"isDefault": true
			},
			"command": "make", //task命令
			"args": [] //参数:无
		},
		{
    
    
			"label": "Build", //task名称,和launch中preLaunchTask一样
			"dependsOrder": "sequence", //按列出的顺序执行任务依赖项
			//依赖项
			"dependsOn": [
				"cmake",
				"make"
			]
		}
	]
}

The cooperation of launch and tasks can realize automatic debugging (no need to manually compile)
"preLaunchTask" in luanch: "Build"
"label" in tasks: "Build"
as long as the name is the same, not necessarily Build, sometimes vscode automatically generates "C/C++:g++ generates active files"
as long as the names of these two options are consistent

Guess you like

Origin blog.csdn.net/weixin_50727642/article/details/124967813