c# - 构建系统将项目目录中的每个 *.CS 文件编译成一个可执行文件

标签 c# build sublimetext3

我开始使用 Sublime Text(通过 Dropbox 在 Linux 和 Windows 中同步 Packages 文件夹),并且我正在创建一些没有 Visual Studio 的小型 C# 项目。

我可以在 Windows 中使用 csc 或在 linux 中使用 gmcs 编译单个文件,并且已经从命令行(在 sublime 之外)在 linux 中编译了多个文件gmsc *.cs,它巧妙地解决了依赖关系并创建了一个以源代码中“最顶层”类命名的可执行文件。

reading the docs 对 Sublime Text 构建配置文件进行了一些摸索之后,而且了解不多,我的问题是:

How should I configure Sublime Text (via JSON file) to build every *.cs file inside a project/directory into one single executable?

这些项目,至少目前来说,非常简单,我只是将文件分开以使事情井井有条。

最佳答案

对于 Windows,请考虑 an approach to doing what you want on the Sublime Text forums .虽然它是针对 ST2 发布的,但我发现它在 ST3 中也能正常工作。

我认为您会发现发帖人的具体方法比在阅读构建系统文档后尝试从头开始找到解决方案更容易遵循和适应。

这是 .sublime-build 文件的内容 - 1) 为我的 Windows 环境设置的 cmd 路径和 2 ) $file 替换为 *.cs 以编译当前文件目录中的所有 .cs 文件:

{
   "cmd": ["C:\\Bin\\Scripts\\sublimetext_csharp_builder.bat", "*.cs", "${file/\\.cs/\\.exe/}"], // NOTE: path set for my Windows environment
   //"cmd": ["\\path\\to\\sublimetext_csharp_builder.bat", "$file", "${file/\\.cs/\\.exe/}"],
   "working_dir": "${project_path:${folder:${file_path}}}",
   "file_regex": "^(...*?)[(]([0-9]*),([0-9]*)[)]",
   "selector": "source.cs"
}

你可以找到the related sublimetext_csharp_builter.bat file on GitHub .下面我只是1) 删除了它对 Visual Studio 批处理文件设置环境变量的依赖,只要 csc% 中解析,这实际上是不必要的PATH% 或您使用它的完整路径和 2) 引用它到生成的 exe 的路径,我发现这对于我使用的路径是必要的 (因为它包含空格):

:: Assumptions:
:: - Sublime Text has set the working directory and both the source and executable files
::   are in that directory
:: - The script is only capable of handling simple C# apps that do not reference 3rd-party
::   libraries

:: Inputs from Sublime Text
:: %1 - The full path and filename of the source file to build
:: %2 - The name of the executable file
@SET SRC_FILE="%1"
@SET EXE_NAME="%2"

csc %SRC_FILE% /nologo /debug:full /platform:x86
@IF errorlevel 1 GOTO end

:: Execute compiled binary if build was successful.
@ECHO.
@ECHO Executing %EXE_NAME%:
@ECHO.
:: NOTE: path quoted to accommodate spaces in it
@"%EXE_NAME%"
@%EXE_NAME%

:end

关于c# - 构建系统将项目目录中的每个 *.CS 文件编译成一个可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24254674/

相关文章:

c# - 在 poly2tri 中的 PolygonPoints 上附加额外数据

c# - 使用 InvokeCommandAction 传递 Source 和 Property,而不使用 MultiBindings

c# - 如何从 WinRT WebView 控件获取重定向 URL

c++ - 如何设置 Makefile 以使用 Visual-Studio 风格的输出目录进行构建?

sublimetext3 - 崇高的片段不起作用

C# 控制台如何工作?

build - 计算机硬件网站

iphone - iPhone 版本的 "Show Package Contents"显示空白 PNG?

haml - 如何让 HAML 语法样式在 Sublime Text 中工作?

regex - Sublime Text 中的非捕获组正则表达式不起作用