DEV Community

kisorge
kisorge

Posted on

【编译器】VSCODE编译C语言

【编译器】VSCODE编译C语言


@TOC

前言

使用工具:
1.ARM仿真器/J-OBV2仿真器


提示:以下是本篇文章正文内容,下面案例可供参考

一、下载配置

Release of 14.2.0-rt_v12-rev0

网络上很多了,可以参考引用文章配置

二、代码

1.main.c

#include <stdio.h> //io头文件用于调用 printf函数 #include <stdlib.h> //lib头文件用于调用system函数  //主函数 int main() { printf("hello world\n"); //打印hello world \n为换行符 system("pause"); //暂停运行,否则直接运行完程序会直接关闭窗口(vscode需要,其他编译器看情况)  } 
Enter fullscreen mode Exit fullscreen mode

2.lanuch

{ "version": "0.2.0", "configurations": [ { "name": "C/C++ Runner: Debug Session", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "externalConsole": true, "cwd": "${workspaceFolder}", "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } 
Enter fullscreen mode Exit fullscreen mode

3.task

单文件不需要task 
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

三、编译运行——方法一:编译器运行

1.编译:终端-运行生成任务(ctrl+shift+B)

在这里插入图片描述

2.运行:运行-启动调试(F5)

在这里插入图片描述

在这里插入图片描述

四、编译运行——方法二:指令

1.生成exe

gcc test.c -o test 
Enter fullscreen mode Exit fullscreen mode

2.运行exe

./test 
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、多文件调试

1.main.c

#include <stdio.h> #include "test1.h" #include <stdlib.h> int main(void) { printf("hello world\r\n"); test();//此函数在test1.c中实现,在test1.h中声明 system("pause"); return 0; } 
Enter fullscreen mode Exit fullscreen mode

2.test1.c

#include "test1.h" #include <stdio.h>  void test(void) { printf("this is test\r\n"); } 
Enter fullscreen mode Exit fullscreen mode

3.test1.h

void test(void); 
Enter fullscreen mode Exit fullscreen mode

4.launch.json

{ "version": "0.2.0", "configurations": [ { "name": "C/C++ Runner: Debug Session", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "externalConsole": true, "cwd": "${workspaceFolder}", "program": "${workspaceFolder}\\${workspaceRootFolderName}.exe", "MIMode": "gdb", //"miDebuggerPath": "C:/TDM-GCC-64/bin/gcc.exe", "miDebuggerPath": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } 
Enter fullscreen mode Exit fullscreen mode

5.task.json

注意,这个地方我使用"${workspaceFolder}/*.c",会报错,所以改为每个文件添加

{ "tasks": [ { "type": "cppbuild", "label": "C/C++: gcc.exe 生成活动文件", "command": "C:/TDM-GCC-64/bin/gcc.exe", "args": [ "-fdiagnostics-color=always", "-g", //"${workspaceFolder}/*.c", "${workspaceFolder}\\main.c", "${workspaceFolder}\\test1.c", "-o", "${workspaceFolder}/${workspaceRootFolderName}.exe", ], "options": { "cwd": "C:/TDM-GCC-64/bin/" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" } 
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

六、参考资料

VScode任务配置详解(task.json与launch.json)
Windows系统中vscode的c/c++开发环境配置(三):多文件编译和调试
VSCode中的tasks.json配置为*.c后发现报错 fatal error: *.c: Invalid argument compilation terminated.
VScode搭建C/C++开发环境
使用Vscode创建一个C_Hello程序

总结

本文仅仅简单介绍了【编译器】VSCODE编译C语言,评论区欢迎讨论。

Top comments (0)