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

비밀번호

댓글 작성자
 




... [46]  47  48  49  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12472정성태1/3/20219400디버깅 기술: 178. windbg - 디버그 시작 시 스크립트 실행
12471정성태1/1/20219872.NET Framework: 992. C# - .NET Core 3.0 이상부터 제공하는 runtimeOptions의 rollForward 옵션 [1]
12470정성태12/30/202010059.NET Framework: 991. .NET 5 응용 프로그램에서 WinRT API 호출 [1]파일 다운로드1
12469정성태12/30/202013649.NET Framework: 990. C# - SendInput Win32 API를 이용한 가상 키보드/마우스 [1]파일 다운로드1
12468정성태12/30/202010265Windows: 186. CMD Shell의 "Defaults"와 "Properties"에서 폰트 정보가 다른 문제 [1]
12467정성태12/29/202010168.NET Framework: 989. HttpContextAccessor를 통해 이해하는 AsyncLocal<T> [1]파일 다운로드1
12466정성태12/29/20208195.NET Framework: 988. C# - 지연 실행이 꼭 필요한 상황이 아니라면 singleton 패턴에서 DCLP보다는 static 초기화를 권장파일 다운로드1
12465정성태12/29/202011311.NET Framework: 987. .NET Profiler - FunctionID와 연관된 ClassID를 구할 수 없는 문제
12464정성태12/29/202010160.NET Framework: 986. pptfont.exe - PPT 파일에 숨겨진 폰트 설정을 일괄 삭제
12463정성태12/29/20209223개발 환경 구성: 520. RDP(mstsc.exe)의 다중 모니터 옵션 /multimon, /span
12462정성태12/27/202010813디버깅 기술: 177. windbg - (ASP.NET 환경에서 유용한) netext 확장
12461정성태12/21/202011632.NET Framework: 985. .NET 코드 리뷰 팁 [3]
12460정성태12/18/20209325기타: 78. 도서 소개 - C#으로 배우는 암호학
12459정성태12/16/20209743Linux: 35. C# - 리눅스 환경에서 클라이언트 소켓의 ephemeral port 재사용파일 다운로드1
12458정성태12/16/20209234오류 유형: 694. C# - Task.Start 메서드 호출 시 "System.InvalidOperationException: 'Start may not be called on a task that has completed.'" 예외 발생 [1]
12457정성태12/15/20208789Windows: 185. C# - Windows 10/2019부터 추가된 SIO_TCP_INFO파일 다운로드1
12456정성태12/15/20209066VS.NET IDE: 156. Visual Studio - "Migrate packages.config to PackageReference"
12455정성태12/15/20208588오류 유형: 693. DLL 로딩 시 0x800704ec - This Program is Blocked by Group Policy
12454정성태12/15/20209164Windows: 184. Windows - AppLocker의 "DLL Rules"를 이용해 임의 경로에 설치한 DLL의 로딩을 막는 방법 [1]
12453정성태12/14/202010147.NET Framework: 984. C# - bool / BOOL / VARIANT_BOOL에 대한 Interop [1]파일 다운로드1
12452정성태12/14/202010318Windows: 183. 설정은 가능하지만 구할 수는 없는 TcpTimedWaitDelay 값
12451정성태12/14/20209504Windows: 182. WMI Namespace를 열거하고, 그 안에 정의된 클래스를 열거하는 방법 [5]
12450정성태12/13/202010198.NET Framework: 983. C# - TIME_WAIT과 ephemeral port 재사용파일 다운로드1
12449정성태12/11/202010608.NET Framework: 982. C# - HttpClient에서의 ephemeral port 재사용 [2]파일 다운로드1
12448정성태12/11/202012232.NET Framework: 981. C# - HttpWebRequest, WebClient와 ephemeral port 재사용파일 다운로드1
12447정성태12/10/202010369.NET Framework: 980. C# - CopyFileEx API 사용 예제 코드파일 다운로드1
... [46]  47  48  49  50  51  52  53  54  55  56  57  58  59  60  ...