Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

C# 6.0 이상의 소스 코드를 Visual Studio 설치 없이 명령행에서 컴파일하는 방법

.NET Framework이 설치된 시스템의 경우 다음과 같은 경로에서 csc.exe C# 컴파일러를 찾을 수 있습니다.

[32비트 운영체제의 csc.exe 위치]
32비트: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

[64비트 운영체제의 csc.exe 위치]
32비트: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
64비트: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe

실행해 보면 알겠지만,

C:\temp>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
Microsoft (R) Visual C# Compiler version 4.7.2053.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

warning CS2008: No source files specified
error CS1562: Outputs without source must have the /out option specified

.NET Framework과 함께 기본 제공되는 C# 컴파일러는 C# 5.0까지만 지원합니다. 즉, C# 6.0 이후의 문법을 사용한 소스 코드를,

using System;
using static System.Console; // C# 6.0의 using static 구문 사용

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            WriteLine("Hello World");
        }
    }
}

빌드하면 이렇게 컴파일 오류가 발생합니다.

Program.cs(2,7): error CS1041: Identifier expected; 'static' is a keyword
Program.cs(2,14): error CS1518: Expected class, delegate, enum, interface, or struct

이 문제를 간단하게 해결하려면, Visual Studio 2017 Community 무료 버전을 설치하면 됩니다. 또는, C# 6.0 이상의 구문을 빌드할 수 있는 Rosyln C# 컴파일러를 직접 구해도 됩니다. 방법은 http://go.microsoft.com/fwlink/?LinkID=533240 링크에서 소개하고 있는 데로, 우선 Nuget 패키지 관리자(nuget.exe)를 다운로드해야 합니다.

Available NuGet Distribution Versions
; https://dist.nuget.org/index.html

nuget.exe (v6.2.1)
; https://dist.nuget.org/win-x86-commandline/v6.2.1/nuget.exe

설치할 필요 없이 nuget.exe 단일 파일로 제공되기 때문에 c:\temp와 같은 폴더에 내려받으면 됩니다.

이후, 명령행에서 다음과 같이 실행해 주면 해당 폴더 하위에 최신 버전의 C# 컴파일러가 구성됩니다.

C:\temp> nuget install Microsoft.Net.Compilers

가령, 현재(2022-07-25) 날짜 기준으로 최신 컴파일러 버전이 "4.2.0"이므로 다음과 같은 폴더가 생성되었습니다.

C:\temp\Microsoft.Net.Compilers.4.2.0

그리고 위의 폴더 하위에는 "build", "tools"가 있고 우리가 원하는 C# 컴파일러는 "tools"에 csc.exe로 포함되어 있습니다. 따라서, C# 6.0 이상의 구문을 포함하는 소스 코드를 컴파일하고 싶다면 다음과 같이 실행하시면 됩니다.

C:\temp> .\Microsoft.Net.Compilers.4.2.0\tools\csc.exe program.cs
Microsoft (R) Visual C# Compiler version 4.2.0-4.22262.19 (46c8f4f5)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\temp> program
Hello World




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/25/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13602정성태4/20/2024220닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/2024257닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/2024298닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024442닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024436닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/2024504닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/2024854닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/2024985닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241033닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241052닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241209C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241169닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241073Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241143닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241197닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신파일 다운로드1
13587정성태3/27/20241157오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241304Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241096Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241050개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241158Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
13582정성태3/19/20241421Windows: 260. CPU 사용률을 나타내는 2가지 수치 - 사용량(Usage)과 활용률(Utilization)파일 다운로드1
13581정성태3/18/20241589개발 환경 구성: 707. 빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
13580정성태3/15/20241138닷넷: 2231. C# - ReceiveTimeout, SendTimeout이 적용되지 않는 Socket await 비동기 호출파일 다운로드1
13579정성태3/13/20241494오류 유형: 899. HTTP Error 500.32 - ANCM Failed to Load dll
13578정성태3/11/20241631닷넷: 2230. C# - 덮어쓰기 가능한 환형 큐 (Circular queue)파일 다운로드1
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...