Use .NET SDK instead of Mono for compiling C# programs #2
Jisu-Woniu
started this conversation in
Ideas
Replies: 1 comment
-
#!/bin/bash
: "${DOTNET_ROOT:=$(dirname "$(dirname "$(dotnet --info | grep "Base Path" | awk '{ print $NF }')")")}"
csc_path=$(find "$DOTNET_ROOT" -name csc.dll -print | sort -V | tail -n1)
ref_path=$(find "$DOTNET_ROOT" -path "*packs/Microsoft.NETCore.App.Ref/*/ref" -print | sort -V | tail -n1)
read -r -a refs <<<"$(find "$ref_path" -path '*.dll' -printf "-r:%p ")"
# Compile C# source file into '.exe' file.
csc() {
dotnet "$csc_path" -nologo "${refs[@]}" "$@"
}
dotnet_csc_runtimeconfig="$HOME/.cache/csc-console-apps.runtimeconfig.json"
dotnet_runtime_version=$(dotnet --list-runtimes | grep "Microsoft\.NETCore\.App" | sort -V | tail -n1 | awk '{ print $2 }')
cat <<EOF >"$dotnet_csc_runtimeconfig"
{
"runtimeOptions": {
"framework": {
"name": "Microsoft.NETCore.App",
"version": "$dotnet_runtime_version"
}
}
}
EOF
# Run compiled C# '.exe'.
csc-run() {
dotnet exec --runtimeconfig "$dotnet_csc_runtimeconfig" "$@"
}
echo "Help:"
echo "Use 'csc <SOURCE>.cs' to compile a C# file into '.exe' file."
echo "Use 'csc-run <OUTPUT>.exe' to run a compiled C# '.exe'." |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Traditionally, online judge systems use Mono to compile and run C# programs.
This leads to several problems:
.NET SDK is widely used in modern C# development, but it does not provide an out-of-box C# file compiler.
Based on this answer from Stack Overflow, I can compile single C# file with the following script.
Beta Was this translation helpful? Give feedback.
All reactions