【问题标题】:Compiling C# Code with Coderunner — No Warnings Allowed使用 Coderunner 编译 C# 代码 - 不允许出现警告
【发布时间】:2012-09-23 09:41:32
【问题描述】:

我很想解决一个小问题。我知道这不是调试可能出现警告的一段代码的最佳方法,但是当我在想法之间有一点中断时,我喜欢一直调试。我刚刚发现了 mono 以及编译在 Mac OS X Mountain Lion 上运行的 C# 代码的可能性。我将它集成到CodeRunner 应用程序中,它可以正常工作。但是,如果代码中出现警告,则不起作用。

例如,我试图编译一个创建一个整数(仅此而已)的代码,但由于该警告,它没有进行调试。我收到此错误消息:

Untitled.cs(9,29): warning CS0219: The variable `test' is assigned but its value is never used
Cannot open assembly 'Compilation succeeded - 1 warning(s)
Untitled.exe': No such file or directory.

可能有人知道如何处理。我知道这不是一个必不可少的功能,但即使有一些未使用的变量,我也很想调试代码。

编译脚本文件内容:

#!/bin/bash

enc[4]="UTF8"            # UTF-8
enc[10]="UTF16"            # UTF-16
enc[5]="ISO8859-1"        # ISO Latin 1
enc[9]="ISO8859-2"        # ISO Latin 2
enc[30]="MacRoman"        # Mac OS Roman
enc[12]="CP1252"        # Windows Latin 1
enc[3]="EUCJIS"            # Japanese (EUC)
enc[8]="SJIS"            # Japanese (Shift JIS)
enc[1]="ASCII"            # ASCII


rm -rf "$4"/csharp-compiled
mkdir "$4"/csharp-compiled
#mcs is the Mono CSharp Compiler

file="$1"
length=${#file}
first=`expr $length - 3`
classname=`echo "$file" | cut -c 1-"$first"`
#echo -out:"$4"/csharp-compiled/"$classname".exe "$1"
dmcs -out:"$4"/csharp-compiled/"$classname".exe "$1"
status=$?

if [ $status -ne 0 ]
then
exit $status
fi

#echo "$4"/csharp-compiled/

currentDir="$PWD"
cd "$4"/csharp-compiled/
files=`ls -1 *.exe`
status=$?
if [ $status -ne 0 ]
then
exit 1
fi

cd "$currentDir"
for file in $files
do
mv -f "$4"/csharp-compiled/$file "$file"
done

# Otherwise output the name of the input file without extension (this should be the same as the class name)
file="$1"
length=${#file}
first=`expr $length - 3`
classname=`echo "$file" | cut -c 1-"$first"`
echo $classname".exe"
exit 0

【问题讨论】:

  • 这看起来您在代码运行器中的集成是错误的:编译器输出似乎被错误地视为文件名的一部分。您能否详细说明您是如何尝试使其发挥作用的?
  • 好的,设置如下。我启用了“语言使用编译脚本”。 I/O 编码是 UTF-8。运行命令是'mono $compiler'。语法模式是“C#”,文件扩展名是 cs。编译脚本的内容取自教程。如果您有兴趣或者可能有问题,我可以发布它们。
  • 对不起,还有两件事:我发布的设置是首选项的语言选项卡中的设置。我还用编译脚本文件的内容更新了第一篇文章。
  • 你能试试>&2吗?将其添加到 dmcs 行的末尾。它会使编译器的任何输出显示在 stderr 上,这应该确保它不会被误解。
  • 哇,它似乎在工作。现在我只收到警告和消息“编译完成”。非常感谢。小细节很重要 :) 我是 StackOverflow 的新手。是否有可能对您的评论给予任何评价?

标签: c# macos mono warnings coderunner


【解决方案1】:
dmcs -out:"$4"/csharp-compiled/"$classname".exe "$1"

dmcs 将一些消息放在标准输出上,一些放在标准错误上。 CodeRunner 期望 stdout 只包含输出文件名,没有其他内容,所以为了实现这一点,>&2 可用于将其他所有内容重定向到 stderr。

dmcs -out:"$4"/csharp-compiled/"$classname".exe "$1" >&2

【讨论】:

    【解决方案2】:

    这对我在 Sierra 上使用当前 Mono 和 CodeRunner 版本有效:

    语言编译脚本:

    file=$CR_FILENAME
    /Library/Frameworks/Mono.framework/Versions/Current/bin/mcs "$CR_FILENAME" >&2
    status=$?
    if [ $status -ne 0 ]
    then
    exit $status
    fi
    echo $file | sed -e "s/\.cs/.exe/"
    exit 0
    

    运行命令:

    PATH="/Library/Frameworks/Mono.framework/Versions/Current/bin:$PATH"; mono $compiler
    

    【讨论】:

      【解决方案3】:

      在 MS 编译器中有一种隐藏警告的方法

      Pragma Warning Preprocessor Directive

      #pragma warning disable 219
      var test = "";
      #pragma warning restore 219
      

      也许对你有帮助。

      【讨论】:

      • 感谢您的快速响应,但我不想隐藏警告。当应用程序告诉我可能有问题时没关系,但问题在于它没有调试,因为一个微小的警告。
      • 调试后可以重新开启。
      • 好的,我刚试过,它对我有用,所以它可能是一个小解决方法。非常感谢您,Sizikov 先生。我会稍微提一下这个问题。也许有人知道更好的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多