安装VS 2017之后,为什么命令行csc无法编译C#7? [英] Why can't the command-line csc compile C# 7 after installing VS 2017?

查看:381
本文介绍了安装VS 2017之后,为什么命令行csc无法编译C#7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了Visual Studio 2017 RC,已已发布几天前并支持C#7.我可以在IDE中使用C#7功能:

I just downloaded Visual Studio 2017 RC, which was released a few days ago and comes with support for C# 7. I can use C# 7 features from the IDE:

但是,此行为似乎不适用于命令行.我正在一个需要csc可执行文件来处理C#7和更高版本的项目.但是,当我尝试更改为与项目相同的目录并编译文件时,我得到了

However, this behavior does not seem to hold for the command-line. I am working on a project that requires the csc executable to handle C# 7 and higher. However, when I try changing to the same directory as the project and compiling the file, I get

> csc Program.cs /target:exe
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(12,23): error CS1026: ) expected
Program.cs(12,25): error CS1001: Identifier expected
Program.cs(12,25): error CS1002: ; expected
Program.cs(12,26): error CS1002: ; expected
Program.cs(12,26): error CS1513: } expected
Program.cs(13,32): error CS1003: Syntax error, '=>' expected
Program.cs(13,32): error CS1525: Invalid expression term '='

很显然,在我的PATH中找到的csc版本似乎不支持C#7.对此我做了一些研究,发现了

So clearly, it looks like the version of csc found in my PATH does not support C# 7. I did a little research on this and found a similar question for C# 6, which suggested checking to make sure you are invoking the csc from %PROGRAMFILES(x86)%\MSBuild\14.0\Bin instead of the old one from C:\Windows\Microsoft.NET\Framework\4.0.30319, since the latter only supports C# 5. So I did just that:

> where csc
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

如您所见,选择了MSBuild\14.0\Bin目录中的目录,而不是v4.0.30319中的旧目录.我还运行了csc /version,它告诉我csc的版本是1.3.1.60616,实际上仅支持C#6.

As you can see the one from the MSBuild\14.0\Bin directory gets chosen, not the old one in v4.0.30319. I additionally ran csc /version which tells me that the version of csc is 1.3.1.60616, which indeed only supports C# 6.

有人知道如何在命令行上启用csc版本的C#7功能吗?谢谢!

Does anyone have a clue how to enable C# 7 features for the version of csc on the command line? Thanks!

推荐答案

您必须启动"VS 2017 RC开发人员命令提示符".然后您会看到csc.exe的版本号为2.0.

You have to launch "Developer Command Prompt for VS 2017 RC". Then you can see that csc.exe will have a version number of 2.0.

众所周知,对于每个新的VS版本,都应使用其专用的提示符并加载适当的环境变量.

It has been well known that for every new VS release you should use its dedicated prompt with the appropriate environment variables loaded.

这篇关于安装VS 2017之后,为什么命令行csc无法编译C#7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆