我想在Sublime Text中使用gcc、g++和make来编译C和C ++代码,以便在Windows上生成Linux可执行文件。 但是我无法在Sublime Text中运行bash.exe,与其他许多Stack Overflow用户一样。
您需要将C:\Windows\System32\bash.exe
文件复制到C:\Windows\SysWOW64\
目录中。这是因为WoW64文件系统重定向所需(感谢Martin!)
然后您需要在Sublime Text中创建一个新的构建系统,其中包含以下代码。(工具 -> 构建系统 -> 新建构建系统...
)
{
"cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
"shell": true,
"working_dir": "${file_path}",
}
这段代码将编译 .c 代码,然后运行它。输出将显示在 Sublime 的构建结果面板中。
当您想要使用此构建系统时,请在 工具 -> 构建系统
列表中选择它,然后按下 Ctrl + B
。
您可以自定义我放置的命令,主要是您可以使用 bash -c "CommandsYouWantToRun"
运行 Linux 命令。
在WSL2中,我认为最好的方法是使用下面的sublime-build文件。
工具 -> 构建系统 -> 新建构建系统...
) {
"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 作为输入和输出文件(可选,如果不需要,请用 ./a.out 替换 ./a.out<inp.in>out.in
)。输出结果将显示在 Sublime 的 Build Results 面板中。
当您想使用此 Build System 时,请在 工具 -> 构建系统
列表中选择它,然后按下 Ctrl + B
。
这篇MS开发博客推荐使用wsl
作为从Windows调用WSL命令行的默认方式。因此,原则上你的sublime构建配置应该如下所示:
{
"shell_cmd": "wsl -- <your-linux-build-command>",
"shell": true,
}
wslpath
。例如:{
"shell_cmd": "wsl -- cmake --build \\$(wslpath '$project_path\\build')",
"shell": true,
}
\
来避免Sublime的变量替换。bash-wsl.sublime-build
文件:{
"shell_cmd": "bash -c \"wslpath '${file}'\" | bash -s",
"shell": true,
}