如何定义 task.json 以使用 windows 上的 cl.exe 编译 vscode 中的 C/C++ 代码?
How to define the task.json to compile C/C++ code in vscode by using the cl.exe on windows?
我已经在我的 64 位 win10 上安装了 Microsoft Visual C++ Build Tools 2015,可以使用 cl.exe 编译和 link C/C++ 程序在普通命令中通过以下步骤提示 window(一些说明来自 Setting the Path and Environment Variables for Command-Line Builds):
1. cd "\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
2. vcvarsall amd64
3. cl helloworld.c
helloworld.c只是一个简单的C源文件打印"Hello world!"。我还尝试在 vs 代码中配置 task.json 直接编译和 link C/C++ 程序。这是我的 task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "vcvarsall amd64 && cl",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
并且在PATH中添加了vsvarsall
和cl
的路径。但它仍然不起作用(输出放在 post 的末尾)。所以我的问题是:如何定义task.json可以先运行vcvarsall amd64
设置系统变量然后执行cl
命令编译和link 程序。
正如 Rudolfs Bundulis 所说,制作一个批处理文件并调用它,在里面做你需要做的一切。
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "build.bat",
"isShellCommand": true,
"args": [],
"showOutput": "always"
}
在你的项目中有 build.bat 优点。
build.bat:
@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
..
cl %YourCompilerFlags% main.cpp %YourLinkerFlags%
..
我想提一下,您想要另一个 visual studio 代码引导程序批处理来设置 vcvars 环境,然后启动编辑器,这样您就不必为每个构建设置 vcvars。像这样:
@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
code
这样你就可以在每次编译代码时省略设置vcvarsall.bat
。 Minimal rebuild flag 也将对您有很大帮助,因此您只编译更改的文件。
我已经在我的 64 位 win10 上安装了 Microsoft Visual C++ Build Tools 2015,可以使用 cl.exe 编译和 link C/C++ 程序在普通命令中通过以下步骤提示 window(一些说明来自 Setting the Path and Environment Variables for Command-Line Builds):
1. cd "\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
2. vcvarsall amd64
3. cl helloworld.c
helloworld.c只是一个简单的C源文件打印"Hello world!"。我还尝试在 vs 代码中配置 task.json 直接编译和 link C/C++ 程序。这是我的 task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "vcvarsall amd64 && cl",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
并且在PATH中添加了vsvarsall
和cl
的路径。但它仍然不起作用(输出放在 post 的末尾)。所以我的问题是:如何定义task.json可以先运行vcvarsall amd64
设置系统变量然后执行cl
命令编译和link 程序。
正如 Rudolfs Bundulis 所说,制作一个批处理文件并调用它,在里面做你需要做的一切。
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "build.bat",
"isShellCommand": true,
"args": [],
"showOutput": "always"
}
在你的项目中有 build.bat 优点。
build.bat:
@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
..
cl %YourCompilerFlags% main.cpp %YourLinkerFlags%
..
我想提一下,您想要另一个 visual studio 代码引导程序批处理来设置 vcvars 环境,然后启动编辑器,这样您就不必为每个构建设置 vcvars。像这样:
@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
code
这样你就可以在每次编译代码时省略设置vcvarsall.bat
。 Minimal rebuild flag 也将对您有很大帮助,因此您只编译更改的文件。