【Ubuntu版】VScode配置C++开发环境

说明:若不够详细,请参考系列博客: 【Windows版】VScode配置C++开发环境

一、参考资料

ubuntu18.04 配置vscode的C++/C环境+opencv

visual studio code搭建opencv环境

VScode搭建Opencv(C++开发环境)

VSCODE 运行调试c++程序

VScode搭建OpenCV环境

vscode搭建opencv4.5.5+opencv_contrib4.5.5开发环境

Ubuntu20下 vscode配置OpenCV 4.5.5

二、重要技巧

2.1 debug调试

需要先编译,然后再debug调试。

编译:ctrl+shift+B;

debug:Run —> Start Debugging;

逐步调式:

Step Over :逐行执行,如果遇到函数调用,当成整体执行过去

Step Into: 遇到函数调用,进去函数内部,可以看清楚函数内部的执行细节

Step Out : 当使用Step Into 已经进入了一个函数调用的内部执行了几行后,想跳过该函数剩余部分,就使用Step Out,返回到该函数调用的下一行。

2.2 追踪变量

如果你想在调试时追踪某几个变量的值,那么就可以使用 Watch 面板。打开左侧 Watch 窗口,点击 + ,输入想要追踪的变量名,回车确定。

2.3 配置头文件

Ubuntu20下 vscode配置OpenCV 4.5.5

三、文件头部和函数自动注释

vscode快速添加文件头部注释和函数注释

VScode自动添加注释

四、相关配置文件

settings.json

该文件是首选项的配置文件,配置包含:字体、window背景、头文件和函数注释模板等。

{
    "grunt.autoDetect": "on",
    "editor.fontSize": 16,
    "workbench.colorTheme": "Visual Studio Dark - C++",
    "window.openFilesInNewWindow": "on",
    "[cpp]": {
        "files.encoding": "gbk"
    },
    "[c]": {
        "files.encoding": "gbk"
    },
    "C_Cpp.codeAnalysis.clangTidy.headerFilter": "",
    "fileheader.configObj": {
        "createFileTime": true,
        "language": {
            "languagetest": {
                "head": "/$$",
                "middle": " $ @",
                "end": " $/",
                "functionSymbol": {
                    "head": "/** ",
                    "middle": " * @",
                    "end": " */"
                },
                "functionParams": "js"
            }
        },
        "autoAdd": true,
        "autoAddLine": 100,
        "autoAlready": true,
        "annotationStr": {
            "head": "/*",
            "middle": " * @",
            "end": " */",
            "use": true
        },
        "headInsertLine": {
            "php": 2,
            "sh": 2
        },
        "beforeAnnotation": {
            "文件后缀": "该文件后缀的头部注释之前添加某些内容"
        },
        "afterAnnotation": {
            "文件后缀": "该文件后缀的头部注释之后添加某些内容"
        },
        "specialOptions": {
            "特殊字段": "自定义比如LastEditTime/LastEditors"
        },
        "switch": {
            "newlineAddAnnotation": true
        },
        "supportAutoLanguage": [],
        "prohibitAutoAdd": [
            "json"
        ],
        "folderBlacklist": [
            "node_modules",
            "文件夹禁止自动添加头部注释"
        ],
        "prohibitItemAutoAdd": [
            "项目的全称, 整个项目禁止自动添加头部注释, 可以使用快捷键添加"
        ],
        "moveCursor": true,
        "dateFormat": "YYYY-MM-DD HH:mm:ss",
        "atSymbol": [
            "@",
            "@"
        ],
        "atSymbolObj": {
            "文件后缀": [
                "头部注释@符号",
                "函数注释@符号"
            ]
        },
        "colon": [
            ": ",
            ": "
        ],
        "colonObj": {
            "文件后缀": [
                "头部注释冒号",
                "函数注释冒号"
            ]
        },
        "filePathColon": "路径分隔符替换",
        "showErrorMessage": false,
        "writeLog": false,
        "wideSame": false,
        "wideNum": 13,
        "functionWideNum": 0,
        "CheckFileChange": false,
        "createHeader": false,
        "useWorker": false,
        "designAddHead": false,
        "headDesignName": "random",
        "headDesign": false,
        "cursorModeInternalAll": {},
        "openFunctionParamsCheck": true,
        "functionParamsShape": [
            "{",
            "}"
        ],
        "functionBlankSpaceAll": {},
        "functionTypeSymbol": "*",
        "typeParamOrder": "type param",
        "customHasHeadEnd": {},
        "throttleTime": 60000
    },
    "fileheader.cursorMode": {
        "description": "",
        "param ": "",
        "return": ""
    },
    "fileheader.customMade": {
        "custom_string_obkoro1": "Copyright (C) 2018-2022 * Ltd. All rights reserved.",
        "Author": "liulinjun", //编辑人
        "Date": "Do not edit", //文件生成的时间
        "LastEditTime": "Do not edit", // 文件最后修改的时间
        "LastEditors": "liulinjun",
        "Description": "", //文件内容描述
        "License": "Copyright (C) 2019-2021, "
    }
}

c_cpp_properties.json

该文件是编译器的配置文件,配置包含:gcc/g++路径、include头文件路径、C++标准等。

Ctrl+Shift+P
	---> 输入C/C++
		---> Edit Configurations(UI)
			---> 编译器路径:/usr/bin/g++
			---> IntelliSense 模式:linux-gcc-x64

修改launch.json文件:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                /* 项目所需的头文件路径 */
                "${workspaceFolder}/include",
                "/usr/include",
                "/usr/local/include/opencv",
                "/usr/local/include/opencv2"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": false
            }
        }
    ],
    "version": 4
}

tasks.json

该文件是编译C/C++程序(生成out文件)的配置文件,配置包含:include头文件路径、lib链接库路径等。

自动生成默认的tasks.json文件:

Terminal
	---> Configure Default Build Task
		---> C/C++:g++.exe build active file

tasks.json文件告诉vscode如何编译cpp程序。这会调用 g++ 编译器将源文件编译成可执行文件。为了方便VScode编译C++代码,可以将include头文件、lib动态链接库等路径写入 tasks.json配置文件里。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g", "${file}",
				"-o", "${fileDirname}/${fileBasenameNoExtension}",
                /* 项目所需的头文件路径 */
                "-I","${workspaceFolder}/include",
                "-I", "/usr/include",
				"-I", "/usr/local/include",
                /* OpenCV的lib库 */
                "-I", "/usr/local/include/opencv",
                "-I", "/usr/local/include/opencv2",
				"-L", "/usr/local/lib/libopencv_*",
                /* 项目所需的库文件路径 */
				"-L", "/usr/local/lib",
                /* CMake编译时选择了BUILD_opencv_world选项 */
                "-L", "libopencv_world455"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: /usr/bin/g++"
		}
	]
}

launch.json

该文件是debug调试C/C++程序(执行out文件)的配置文件,配置包含:debug类型等。

自动生成默认的launch.json文件:

Run
	---> Add Configuration
		---> C++(GDB/LLDB)
			---> g++.exe build and debug active file

修改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",
            /* 配置类型,cppdbg类型 */
            "type": "cppdbg",
            /* 请求配置类型,可以为launch(启动)或attach(附加) */
            "request": "launch",
            /* 将要进行调试的程序的路径 */
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            /* 程序调试时传递给程序的命令行参数,一般设为空即可 */
            "args": [],
            /* 设为true时程序将暂停在程序入口处,一般设置为false */
            "stopAtEntry": false,
            /* 调试程序时的工作目录 */
            "cwd": "${fileDirname}",
            "environment": [],
            /* 调试时是否显示控制台窗口,一般设置为true显示控制台 */
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

你可能感兴趣的:(编程工具,vscode,c++,ubuntu)