【问题标题】:Why can't the command-line csc compile C# 7 after installing VS 2017?为什么安装VS 2017后命令行csc不能编译C#7?
【发布时间】:2017-04-11 00:34:27
【问题描述】:

我刚刚下载了 Visual Studio 2017 RC,它是 released a few days ago,它支持 C# 7。我可以使用 IDE 中的 C# 7 功能:

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

> 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。我对此进行了一些研究,发现了 C# 6 的 similar question,它建议检查以确保您是从%PROGRAMFILES(x86)%\MSBuild\14.0\Bin 调用csc 而不是从C:\Windows\Microsoft.NET\Framework\4.0.30319 调用旧的,因为后者只支持C# 5。所以我就是这样做的:

> 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。

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

【问题讨论】:

  • 14.0 是 VS2015 的版本号,不是 VS2017。所以它仍然是一个旧的。使用 2017 Developer Command Prompt 并重复 where 命令以找到新的。

标签: c# .net visual-studio visual-studio-2015 msbuild


【解决方案1】:

您必须启动“VS 2017 RC 的开发人员命令提示符”。然后可以看到csc.exe的版本号是2.0。

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

【讨论】:

  • "众所周知,对于每个新的 VS 版本,您都应该使用其专用提示并加载适当的环境变量。"很难看出这句话的价值。
【解决方案2】:

您可以从 Visual Studio 2017 命令提示符运行“where csc.exe”,并通过将其添加到 Path 系统变量中来导出所需的 csc 版本。 我遇到过类似的情况,我需要通过命令行进行编译,我最终设置了

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\

在系统路径中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2013-12-25
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多