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

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直接运行程序

使用tasks.json的设置编译

这样就能在编译时指定库文件
但是这样只能在编译时链接库,而不能调试时也链接库,
所以我们可以设置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"
        }
    ]
}

,

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值