首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否可以使用.NET Core Roslyn编译器编译单个C#代码文件?

是否可以使用.NET Core Roslyn编译器编译单个C#代码文件?
EN

Stack Overflow用户
提问于 2017-09-06 10:13:20
回答 2查看 33.9K关注 0票数 60

在旧的.NET中,我们曾经能够运行csc编译器来编译单个或多个.cs文件。

有了.NET核心,我们有了dotnet build,它坚持要有一个合适的项目文件。有没有一个独立的命令行编译器,可以在没有项目的情况下编译源代码文件(并在同一命令行中列出引用的依赖项)?

在Linux上,当我安装了旧的csc和新的.NET内核时,我得到的时间是:

代码语言:javascript
复制
[root@li1742-80 test]# time dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  test -> /root/test/bin/Debug/netcoreapp2.0/test.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.94

real    0m7.027s
user    0m5.714s
sys     0m0.838s

[root@li1742-80 test]# time csc Program.cs
Microsoft (R) Visual C# Compiler version 2.3.0.61801 (3722bb71)
Copyright (C) Microsoft Corporation. All rights reserved.


real    0m0.613s
user    0m0.522s
sys     0m0.071s
[root@li1742-80 test]#

注意,对于相同的文件Program.cs,使用.NET核心的时间为7秒,而使用旧csc的时间为几百毫秒。

我希望能够像以前使用csc一样快速地使用.NET核心进行编译。

EN

回答 2

Stack Overflow用户

发布于 2017-09-06 10:24:56

编译器可以使用以下命令直接调用

代码语言:javascript
复制
$ /usr/local/share/dotnet/sdk/2.0.0/Roslyn/RunCsc.sh

但是,如果没有支持的项目基础结构,这个特定的命令可能不是很有帮助,因为您需要手动传入所有.NET核心或.NET标准引用程序集,这通常由.NET和NuGet处理。你会得到这样的错误:

代码语言:javascript
复制
$ /usr/local/share/dotnet/sdk/2.0.0/Roslyn/RunCsc.sh Program.cs
Microsoft (R) Visual C# Compiler version 2.3.2.61921 (ad0efbb6)
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(5,11): error CS0518: Predefined type 'System.Object' is not defined or imported
Program.cs(7,26): error CS0518: Predefined type 'System.String' is not defined or imported
Program.cs(7,16): error CS0518: Predefined type 'System.Void' is not defined or imported
票数 6
EN

Stack Overflow用户

发布于 2020-12-14 12:52:41

代码语言:javascript
复制
This is the scripts:



#!/bin/bash

#dotnethome=`dirname "$0"`
dotnethome=`dirname \`which dotnet\``
sdkver=$(dotnet --version)
fwkver=$(dotnet --list-runtimes | grep Microsoft.NETCore.App | awk '{printf("%s", $2)}')
dotnetlib=$dotnethome/shared/Microsoft.NETCore.App/$fwkver

if [ "$#" -lt 1 ]; then
    dotnet $dotnethome/sdk/$sdkver/Roslyn/bincore/csc.dll -help
    echo dotnethome=$dotnethome
    echo sdkver=$sdkver
    echo fwkver=$fwkver
    echo dotnetlib=$dotnetlib
    exit 1
fi

progfile=$1
prog="${progfile%.*}"
echo -r:$dotnetlib/netstandard.dll > /tmp/$prog.rsp
echo -r:$dotnetlib/System.dll >> /tmp/$prog.rsp
echo -r:$dotnetlib/Microsoft.CSharp.dll >> /tmp/$prog.rsp
for f in  $dotnetlib/System.*.dll; do
    echo -r:$f >> /tmp/$prog.rsp
done

dotnet $dotnethome/sdk/$sdkver/Roslyn/bincore/csc.dll -out:$prog.dll -nologo @/tmp/$prog.rsp $* 
if [ $? -eq 0 ]; then
   if test -f "$prog.dll"; then
    if ! test -f "$prog.runtime.config"; then
        echo "{
  \"runtimeOptions\": {
    \"framework\": {
      \"name\": \"Microsoft.NETCore.App\",
      \"version\": \"$fwkver\"
    }
  }
}"  > "$prog.runtimeconfig.json"
    fi
  fi
fi
echo /tmp/$prog.rsp: 
cat /tmp/$prog.rsp
rm /tmp/$prog.rsp
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46065777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档