欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux下vscode编译和调试时链接库

程序员文章站 2024-02-29 12:35:46
...

linux下vscode编译和调试时链接库

我们可以在命令行里使用-l指定库

gcc filename -lxxx #xxx是库名

但是每次都在命令行里输入太麻烦了,我们可以设置tasks.json文件里的args属性

  	"args": [
  		"-g",
  		"${file}",
  		"-lncurses",    //gcc -l 参数
  		"-o",
  		"${fileDirname}/${fileBasenameNoExtension}" 			
  		]
{//tasks.json
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "look here",//标识
			"command": "/usr/bin/gcc-9",
			"args": [
				"-g",
				"${file}",
				"-lncurses",//gcc -l 参数
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "/usr/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/bin/gcc-9"
		},
		{
			"type": "cppbuild",
			"label": "C/C++: gcc-9 build active file",
			"command": "/usr/bin/gcc-9",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "/usr/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: /usr/bin/gcc-9"
		}
	]
}

然后ctrl+shift+b使用刚刚设置的tasks.json直接运行程序

linux下vscode编译和调试时链接库

这样就能在编译时指定库文件
但是这样只能在编译时链接库,而不能调试时也链接库,
所以我们可以设置launch.json里preLaunchTask属性就可以在调试时使用我们设置的tasks.json

“preLaunchTask”: “tasks label” //相应的tasks.json里的label属性

{//launch.json
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "调试",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "look here",//使用的task设置
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

,