SublimeText的远程编译插件

Running some small test codes on Linux from Windows can be quite tedious, especially since I have to manually execute the compilation commands on Linux. Also, using Samba on my Raspberry Pi to share the code to Windows requires me to SSH and compile it manually, which is a waste of time.
These past few days, I wrote a small Sublime Text plugin during my free time to remotely compile C/C++ code on Windows, meaning that I write the code on Windows but it actually executes on Linux. I’ve only implemented the functionality for now, and I plan to optimize it during my break after the holidays.
The code is hosted on GitHub: sublimeRemoteCompile, using some C++11 features, and the compiler must be specified during compilation.

Update: 2017.02.18

  1. Modified execution logic
    Changed UploadCurrentFolder to UploadCurrentFolderAndRun, which uploads the current file’s folder and executes the compile and run operations after the upload is complete.
  2. Added the UploadCurrentFolderAndTerminalRun mode, which uploads the current file’s folder and opens it in a new window to run.
  3. Folders uploaded using the above two modes will not be automatically deleted, with their path set as remoteTempFolder/RunTime in the ini, where RunTime is the timestamp when the upload operation was performed.
  4. When the source file is in the Samba directory, the above two upload modes will not execute.

Update: 2017.01.26

  1. Optimized the code.
  2. Introduced another mechanism: If the remote host’s directory is mapped to local via Samba, compile the source files directly in that remote directory (by converting the local mapped path to the absolute path of the remote host), without executing the upload source file step (since the file already exists on the remote host).
    For example: If the remote host has a Samba folder ~/code, which I mapped to my Z drive, when I create a new source file hw.cc under Z drive (i.e., the remote host’s ~/code) and use this plugin for remote compilation, it will directly execute the compilation operation (while it would upload first and then compile if not under Samba), thus improving runtime speed.

Usage:
In setting.ini, specify the sambaDrive option as the drive letter mapped to the remote Samba and remoteSambaPath as the path of the remote host’s Samba folder mapped to the local sambaDrive.

1
2
3
4
5
6
7
; The remote host's ~/ directory is mapped to the local Z drive

; samba mapping to local DeskDrive
sambaDrive=Z

; remote samba Path
remoteSambaPath=~/

Thus, executing “remote compile” on files in the local Z drive will not trigger uploads (neither UploadThisFile nor UploadCurrentFolder modes will execute).


Using SSH makes writing some simple test code quite enjoyable, and error messages will be captured in Sublime Text’s Panel. For complex projects, Visual Studio + VisualGDB can be utilized.
Configuration information is read from ini files, supporting seven running modes:

Updated the table according to the changes made on 2017.02.18.

Parameter Mode
panelRun Run in Sublime Text’s Panel
terminalRun Run in a new window
uploadThisFile Upload the currently opened file to the remote host’s temporary directory
UploadCurrentFolderAndRun Upload the folder of the currently opened file to the remote host’s temporary directory and execute in ST’s panel
UploadCurrentFolderAndTerminalRun Upload the folder of the currently opened file to the remote host’s temporary directory and execute in a new window
cleanUpTemp Clean up the temporary directory of the remote host
openTerminal Open an SSH connection window to the remote host

The running mode must be specified at startup.
Two parameters need to be specified during execution: the source file path and the running mode. The source file path can be obtained through ${file} in Sublime Text’s build system.

During compilation, the language will be determined based on the file suffix; C language will default to using gcc, while C++ will use g++, corresponding to standard versions c99 and c++11. It is recommended to install clang on Linux and specify the compiler as clang in setting.ini, as error messages will be much clearer. If clang is specified, it will automatically match the suffix to select clang or clang++ as the compiler.

How to use?

Download the binary compressed package from here and extract it to a local directory.
Then, add a build system in Sublime Text and fill in the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
// binary absolute path
"path": "C:\\Program Files (x86)\\SystemTools\\Tookit\\RemoteCompile",
"cmd": ["cmd", "/c","remotecompile","${file}","panelRun"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c,source.cc,source.cpp,source.c++",
"shell": true,
"variants":
[
{
"name": "TerminalRun",
"cmd": ["start", "cmd", "/k","remotecompile_clangxx","${file}","terminalRun"]
},
{
"name": "UploadThisFile",
"cmd": ["cmd", "/c","remotecompile_clangxx","${file}","uploadThisFile"]
},
{
"name": "UploadCurrentFolderAndRun",
"cmd": ["cmd", "/c","remotecompile_clangxx","${file}","uploadCurrentFolderAndRun"]
},
{
"name": "UploadCurrentFolderAndTerminalRun",
"cmd": ["start", "cmd", "/k","remotecompile_clangxx","${file}","uploadCurrentFolderAndTerminalRun"]
},
{
"name": "CleanUpTemp",
"cmd": ["cmd", "/k","remotecompile_clangxx","${file}","cleanUpTemp"]
},
{
"name": "OpenTerminal",
"cmd": ["cmd", "/k","remotecompile_clangxx","${file}","openTerminal"]
}
]
}

Fill in the path with the absolute path where you extracted the compressed package (i.e., the absolute path of this program on your computer).
Next, you can easily write some code, press Ctrl+Shift+B, and you’ll see the build system list:

Select any option to start execution.
If this is your first time using it, a setting.ini file will be generated in the directory of the binary, where you need to fill in your environment information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[RemoteCompileSSHSetting]
; remote host addr (e.g. root@192.168.137.2)
host=
; remote host port (e.g:22)
port=22
; remote host user password
password=
; sshKey (e.g:C:\\Users\\imzlp\\.ssh\\id_rsa.ppk)
sshKey=
; compiler (e.g:gcc/clang)
compiler=gcc
; e.g:-o/-o2
optimizied=-o
; std version e.g:c99/c11 or c++0x/c++11
stdver=-std=c++11
; other compile args e.g:-pthread
otherCompileArgs=-pthread
; remote host temp folder e.g:/tmp
; It is recommended that you leave the default values (/tmp)
remoteTempFolder=/tmp
; Upload SourceFile to remote host path
; auto create a name is localtime of folder
uploadTo=/tmp

It is recommended to fill in host and password or sshKey, while keeping the others as default.
Additionally, the sshKey must be a ppk key; it should be converted to ppk using puttygen (found in the SSHTools directory). Using password is advised.

panelRun

terminalRun

uploadThisFile

uploadCurrentFolder

cleanUpTemp

openTerminal

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:SublimeText的远程编译插件
Author:LIPENGZHA
Publish Date:2017/01/25 01:43
World Count:4.2k Words
Link:https://en.imzlp.com/posts/11793/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!