How to develop on macOS and compile on linux efficiently
25 Apr 2020
I write web program with c++ in my macbook, deploy in customer's linux server. So, I must compile my code in a linux virtual machine, then put the binary file to my customer's server via sftp. It's a trouble for me, I write a shell program to make it automatic.
How I do before
I have a virtual machine run CentOS7 in my MBP, if I need to complie my code, I follow the steps below:
1. Start virtual machine
2. Put code files to vm via sftp
3. Login the vm and execute make command
4. Put binary files to target server via sftp on vm
5. Login target server and deploy the project
Sometimes I need to execute many times one day, for fix bug, update program and customer's wonderfull needs, and I have to switch between three system(my macOS, linux vm, target server). It become a nightmare for repeated labor.
Automatically compile on Linux
This can be achieved through a shell program, and I need to solve 3 problems:
How to login Linux server without password
It's not really passwordless, just that it's not necessary to enter the password every time. SSH password-less login is the one of the best way to automate tasks(TecMint).
Step 1: Delete server in know_hosts
Open know_hosts file
Step 2: Create Generating RSA Key on macOS
The terminal will ask for enter file path and password, just press Enter key without entering.
Step 3: Create .ssh directory on server
.ssh directory may already exist on the server, so use the parameter "-p" after mkdir.
The terminal will output some information and let you confirm, enter yes and press Enter key, then enter ssh login password.
Step 4: Upload Generating RSA Key to server and set permissions
How to upload/download files to/from a server via sftp
Step 1: Define server's informations and work directory
Step 2: Define upload function
Sometimes I need to upload only partial directories or files, which can be judged the parameters.
Step 3: Define download function
Only download deploy directory from virtual machine.
How to compile on linux
When upload all files to virtual machine, the Makefile is also in it, just execute make command.
All the shell program codes
There is two server, the vritual machine for compile, the customer's server for deploy. I define vserver, vuser, vport, and vrootpaththe for virtual machine, server, user, port, and rootpath for customer's server. Both them hava the same project name.
How to use
I have two way to use it, in terminal or in vim.
In terminal
The shell named manage, if I want only upload all files to virtual machine, I can execute command
Other functions can execute the following commands:
In vim
I am used to programming with vim, and I don't like switch between vim and terminal. I configured the
There are such lines in my Makefile:
When I want to compile on macOS and test the program, I execute
Yeah! ML, are you crooked, it's means Make on Linux!