【问题标题】:Build system to compile every *.CS file in the project directory into one executable构建系统将项目目录中的每个 *.CS 文件编译成一个可执行文件
【发布时间】:2014-06-17 01:59:45
【问题描述】:

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

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

reading the docs 时用Sublime Text 构建配置文件摸索了一下,并不太了解,我的问题是:

我应该如何配置 Sublime Text(通过 JSON 文件)以将项目/目录中的每个 *.cs 文件构建到一个可执行文件中?

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

【问题讨论】:

  • 您的问题是如何通过单次调用 CSC 或其他方式来编译多个 CS 文件(“csc /?”应该对前者有所帮助 - 只需指定所有文件)...
  • @AlexeiLevenkov 是的,我想执行与gmcs *.cs; mono *.exe 相同的操作,这可以在 bash 脚本中成功运行,但是我不想运行脚本,而是将其放入 .build-system 文件中,它应该是这样的(但我不知道如何)。
  • 我明白了 - 你的问题是关于我不知道的事情,所以我不明白。对不起。

标签: c# build sublimetext3


【解决方案1】:

对于 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

【讨论】:

  • @heltonbiker:此外,Jin Weijie 的blog postrelated code on GitHub 对这种方法进行了一些详细说明 - 一旦您获得了满足您需求的上述简单方法,也可能值得一试。
猜你喜欢
  • 2021-12-30
  • 2011-03-02
  • 2014-02-13
  • 2017-01-12
  • 2015-05-26
  • 2019-07-11
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多