vsc配置docker容器中的c环境
环境:ubuntu,vsc,g++&gcc&gdb(确保有以上的GUN工具,没有安装的可以通过apt安装)
-
创建工程文件夹(test)
使用vsc打开该文件夹,在该文件夹中添加hello.c文件,编写简单程序,并通过gcc -g -o hello hello.c
编译c文件,生成hello二进制可运行文件,可以使用./hello
直接运行该文件。 -
vsc配置
在vsc的扩展中安装C/C++
安装后按f5,选择C++(GDB/LLDB),选择默认配置,系统会生成一个lunch.json文件,该文件如下{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "输入程序名称,例如 ${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
其中的program部分已经示例很清楚了,你要调试的文件是hello文件(也就是编译成功的二进制文件),将a.out替换为你的文件名并删除示例中文。
新建一个makefile文件,按语法要求写入
hello:hello.c gcc -g -o hello hello.c //前方空格为tab
你可以理解为一个自动化运行脚本,在.c文件更新或者hello文件不存在时,会执行下方的指令,参考C编译入门。
在json中添加"preLaunchTask": "build"
,该标签的功能是在调试前运行该任务,可以及时编译修改过的程序,保存后按f5,会提示没有配置任务,选择配置任务并选择other,系统会默认添加task.json文件,在该文件中,label是任务名,此处为build,command是触发该任务时会进行的命令,设定为make即可,修改后的两文件为
launch.json{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/hello", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ],"preLaunchTask": "build" } ] }
task.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make" } ] }
此时f5进行调试就可以正常编译和调试。
,理论上,win环境只要有gcc/g++/gdb工具也能够实现编译,更进一步,mingw-w64就是这么干的,关于上面两个json文件,最好了解下相关标签的含义。
参考
C语言入门