天天看點

ubuntu上VSCode建構cmake工程

1.下載下傳Visual Studio Code 編譯器

可以去Ubuntu自帶的應用商店下載下傳,或者使用指令行下載下傳

2.設定中文顯示

直接下載下傳的是英文版本,需要設定成中文顯示

先去VS Code自帶的商店下載下傳插件,快捷鍵:Ctrl+Shift+x,搜尋Chinese (Simplified) Language Pack for Visual Studio Code,點選下載下傳;

然後配置語言,快捷鍵:Ctrl+Shift+p,在選框中輸入configure display language,點選确定,在出現的locale.json檔案中,将"locale":“en” 改為“locale”:“zh-CN”;

重新開機VSCode;

3.下載下傳建構CMake工程的各種依賴包

和第二步類似去VS Code自帶的商店下載下傳插件,快捷鍵:Ctrl+Shift+x,下載下傳各種依賴包,包括:c/c++,c/c++ clang command adapter,c++ intellisense,CMake和CMake Tools

4.各種json檔案配置

打開一個含有CMakeLists.txt的完整的Cmake工程檔案夾

在.vscode檔案夾中要建立三個json檔案才能對Cmake工程進行編譯和調試,分别是c_cpp_properties.json,launch.json和tasks.json

4.1 c_cpp_properties.json的配置

ctrl+shift+p→C/Cpp: Edit Configurations(JSON),生成c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}
           

4.2 launch.json配置

按F5,生成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) 啟動",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/application",  //輸入程式名稱,例如 ${workspaceFolder}/a.out
            "args": ["test1", "test2"],  //輸入程式運作參數
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",  //設定程式運作初始路徑
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊列印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
           

4.3 tasks.json配置

ctrl+shift+B→生成tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make build",//編譯的項目名,build
            "type": "shell",
            "command": "cd ./build ;cmake ../ ;make",//編譯指令
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
        }
    ]
}
           

5. 編寫CMakeLists.txt

例如:

cmake_minimum_required(VERSION 2.8)

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")
add_compile_options(-std=c++11)

set(source ./block.h ./graph.h ./graph.cpp ./maxflow.cpp ./main.cpp)

add_executable(maxflow ${source})
           

6.執行

ctrl+shift+B 編譯程式

F5 執行程式

7.調試

在CMakeLists.txt檔案中添加

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")
           

即可通過斷點進行調試

一個通過cmake組織的工程通過以上步驟可以變成一個可debug的vscode工程

繼續閱讀