【发布时间】:2017-03-20 05:39:00
【问题描述】:
我想使用来自 Sublime 的 gcc、g++ 和 make,以便能够在 Winows 上将 c 和 c++ 代码编译为 Linux 可运行文件。我无法从 Sublime 运行 bash.exe,就像 stackoverflow 上的许多其他用户一样。
【问题讨论】:
标签: linux windows bash sublimetext windows-subsystem-for-linux
我想使用来自 Sublime 的 gcc、g++ 和 make,以便能够在 Winows 上将 c 和 c++ 代码编译为 Linux 可运行文件。我无法从 Sublime 运行 bash.exe,就像 stackoverflow 上的许多其他用户一样。
【问题讨论】:
标签: linux windows bash sublimetext windows-subsystem-for-linux
您必须将C:\Windows\System32\bash.exe 文件复制到C:\Windows\SysWOW64\ 目录。
由于WoW64 file system redirection 需要(感谢Martin!)
然后您必须使用以下代码在 Sublime Text 中创建一个新的构建系统。 (Tools -> Build System -> New Build System...)
{
"cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
"shell": true,
"working_dir": "${file_path}",
}
此代码将编译 .c 代码并运行它。输出将显示在 Sublime 的 Build Results 面板中。
如果您想使用此构建系统,请在Tools -> Build System 列表中选择它,然后点击Ctrl + B。
你可以自定义我放在那里的命令,主要是你可以使用bash -c "CommandsYouWantToRun"运行Linux命令
【讨论】:
%SystemRoot%\System32\bash?
在 WSL 2 上,建议的解决方案不起作用。这是在 WSL 2 目标上执行在 Windows 上的 Sublime Text 中编辑的脚本的解决方案。创建bash-wsl.sublime-build 文件:
{
"shell_cmd": "bash -c \"wslpath '${file}'\" | bash -s",
"shell": true,
}
【讨论】:
在 WSL2 中,我认为最好的方法是使用下面的 sublime-build 文件。
Tools -> Build System -> New Build System...) {
"shell_cmd": "ubuntu run \"g++ `wslpath '${file}'` && ./a.out<inp.in>out.in \" ",
"shell":true,
"working_dir":"$file_path",
"selector":"$file_name"
}
此代码将编译 .cpp 代码并分别使用 inp.in 和 out.in 作为输入和输出文件 (Optional, if you don't want that, then replace ./a.out<inp.in>out.in with ./a.out)。输出将显示在 Sublime 的 Build Results 面板中。
如果您想使用此构建系统,请在Tools -> Build System 列表中选择它,然后点击Ctrl + B。
【讨论】: