vscode配置Task编译程序文件
vscode中按Ctrl+Shift+P的快捷栏中可以选Tasks: Run Task
,而Task又可映射成一个批处理命令来执行。在一个vscode的工程目录下创建./.vscode/Tasks.json
,如下文的内容
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Build & Execute", "type": "process", "command": "./.vscode/scripts/buildExecMain.bat", "args": ["${fileDirname}", "${fileBasename}", "${fileExtname}"] } ] }
就建立了一个task,名为”Build & Execute”,类型是process
,启动它的时候执行command
,为这个命令设置了3个参数,分别是目录、文件名、扩展名。
使用命令编译程序的话,第一步先进入目录,第二步根据各扩展名分别执行编译或运行命令。最后把临时生成的可执行文件删去。在上述command
的路径下,编写的bat如下所述。
@echo off cls cd %1 IF ".cpp" == "%3" goto cpp IF ".cbl" == "%3" goto cobol IF ".hs" == "%3" goto haskell IF ".rkt" == "%3" goto racket IF ".scm" == "%3" goto scheme IF ".py" == "%3" goto python exit :cpp g++ -std=gnu++17 -Wall -Wextra -O2 -o a.exe %2 IF ERRORLEVEL 1 ( exit 1 ) goto exec :cobol cobc -x -O2 -o a.exe %2 IF ERRORLEVEL 1 ( exit 1 ) goto exec :haskell ghc %2 -O2 -o a.exe IF ERRORLEVEL 1 ( exit 1 ) del *.hi del *.o goto exec :exec a.exe < 1.in del a.exe goto end :scheme gosh %2 < 1.in goto end :racket Racket %2 < 1.in goto end :python python %2 < 1.in goto end :end