linux下使用vscode进行c++项目开发,CMakeLists.txt、launch.json、tasks.json联合配置需要注意的地方

  使用这三者配合,可以实现自动化的运行和调试,如下是三个文件的内容,只要目录结构是build外编译类型的,那么launch.json和tasks.json则可以直接copy过去使用。

  • CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(MOTION_AVERAGING)
set(SRC_LIST src/main.cpp src/comp.cpp)
set(CMAKE_BUILD_TYPE Debug)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(exename ${SRC_LIST})
  • 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": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/${workspaceFolderBasename}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "preLaunchTask": "build",
      "setupCommands": [
          {
    
    
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
          }
      ]
  }
  ]
}
  • tasks.json:
{
    
    
	"version": "2.0.0",
	"options": {
    
    
		"cwd": "${workspaceFolder}/build"    //需要进入到我们执行tasks任务的文件夹中
	},
	"tasks": [    //tasks包含三个小任务
		{
    
    
			"type": "shell",
			"label": "cmake",    //第一个任务的名字叫cmake
			"command": "cmake",    //它要执行的命令是cmake
			"args": [
				".."    //参数是..
			]
		},
		{
    
    
            "type": "shell",
			"label": "make",    //第二个任务的名字叫make
			"group": {
    
    
				"kind": "build",
				"isDefault": true
			},
			"command": "make",    //它要执行的命令是make
			"args": [],
            "options": {
    
    
                "cwd": "${workspaceFolder}/build",
            },
		},
		{
    
    
			"label": "build",    //第三个任务的名字叫build
			"dependsOrder": "sequence",    //顺序执行依赖项
			"dependsOn":[    //依赖的两个项为cmake和make
				"cmake",    //即第一个任务的label
				"make"      //即第二个任务的label
			]
		}
	]
}

  其中,launch.json、tasks.json中的${workspaceFolder}表示vscode当前打开的目录路径,而${workspaceFolderBasename}则表示当前打开目录的名字。这里需要注意的点主要有两个:

  • launch.json文件中的"program"项目应该是CMakeLists.txt中add_executable命令里exename的路径,其中exename通常放在当前项目中的build路径下。如果使用了${workspaceFolderBasename},则exename应该填写为当前项目的名称;反之,如果exename为其他名称,则launch.json中"program"项目的名称就应该是"${workspaceFolder}/build/exename"。总之就是需要对应一致,否则会报错找不到build出来的可执行文件。
  • tasks.json文件中的最后一个代码块中的"label"值,应该与launch.json文件中的"preLaunchTask"项目的值保持一致,比如这里全部名为“build”。否则也会报错找不到。

猜你喜欢

转载自blog.csdn.net/weixin_44120025/article/details/129229450