5

我已经在我的 64 位 win10 上安装了 Microsoft Visual C++ Build Tools 2015,并且可以使用 cl.exe 通过以下步骤在普通的命令提示符窗口中编译和链接 C/C++ 程序(来自设置路径和环境的一些说明命令行构建的变量):

 1. cd "\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
 2. vcvarsall amd64
 3. cl helloworld.c

helloworld.c 只是一个简单的 C 源文件,用于打印“Hello world!”。我还尝试将task.json配置为直接编译和链接vs代码中的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 中已经添加了 和 的路径vsvarsallcl但它仍然不起作用(输出放在帖子的末尾)。所以我的问题是:我如何定义task.json,它可以先运行vcvarsall amd64设置系统变量,然后执行cl命令来编译和链接程序。

在此处输入图像描述

4

1 回答 1

2

As Rudolfs Bundulis said, make a batch file and just call it, inside it do everything you need to do.

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"    
}

And in your project have the build.bat goodness.

build.bat:

@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64      <----- update your path to vcvarsall.bat

..
cl %YourCompilerFlags% main.cpp %YourLinkerFlags%
..

I would mention that you'd like to have another visual studio code bootstraper batch that would set the vcvars environment and then starts up the editor so you don't set the vcvars for every build. Like so:

@echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64      <----- update your path to vcvarsall.bat

code

This way you can omit setting the vcvarsall.bat every time you compile the code. Minimal rebuild flag will also help you a lot so you only compile changed files.

于 2016-09-23T21:35:41.040 回答