In this mini-article I will show you how I develop applications for Linux in XCode 4. At the moment I’m using XCode 4.2.1, in OSX Lion, with Ubuntu 10.04 running on a virtual machine.
Although the article is related to XCode, after reading and trying out for yourself, you will notice that you can use whatever IDE you like. Here I’m just showing you how you can code in a OSX-only IDE and deploy in Linux.
You will need some tools before we start:
- VMWare or similar virtual machine application for Mac
- Used to run our Linux system, and that’s where we will compile and run the target application.
You can also use a physical computer instead. However, you will need network connectivity from mac to that computer because the communication between this linux System and our Mac is done by SSH.
- Requirement for the next point
- It will be used to mount a remote directory in our Mac, and access it as if it was an external hard disk drive
Once you have all these, we are ready to go, but take some seconds to make sure you can compile the application in your slave Linux system. This will ensure that any problems you may encounter are not related to your slave machine or the code you are trying to compile.
Accessing slave machine using SSH
Now you will need the slave machine’s IP address, typing ifconfig in its Terminal.
As you can see in the image above, in my case I’ll be using 172.16.68.128 as the slave’s IP Address.
Then we make sure we can access it from your Mac typing the next command in Mac Terminal:
If we are asked for the password, then we got a valid SSH connection and we can move on…
Adding an alias for the Linux Slave IP
Because IPs can change sometimes, even inside Local Area Network, I added an alias for this IP, so that I could use it in scripts instead of the IP address itself. When it changes, I only have to modify one single file. You can do the same, by editing the hosts file, so grab the Mac Terminal and type as you see:
sudo vim /etc/hosts
Add a new line in the end, pressing I to enter insert mode:
Now press ESC to exit “INSERT mode” and type :wq (yes, including the colon). If you have any problems using vim to edit this file, you can try nano instead – just replace “vim” with “nano” in the first command of this section. Now, you can use the virtual hostname “vmcode” instead of the real IP (which will be a lot easier to remember).
Setup script files
Now we need to setup two script files. I called them mountDir and sshbuild. The first one will mount the remote directory in our Mac and the latter is used to remotely compile the application.
Firstly pick a directory for these scripts, where you need to place the sshfs-static-leopard file, because it will be needed for the mountDir script.
Mounting Directory Script
Using your favorite text editor, create a new file called “mountDir” (no extension), and paste the following code:
#!/bin/bash echo password | ./sshfs-static-leopard username@vmcode:codedir ~/path/to/mount/dir -o password_stdin
I’m explaining some of the variables in the command, that you should change to fit your needs:
- username and password are the credentials for the user in the slave Linux machine
- codedir is the directory in slave machine you want to mount in Mac
- ~/path/to/mount/dir is the path to the Mac’s directory where you want to mount the slave’s directory
The SSH command accepts a second argument, that is the command you want to execute right after the remote login. Save and create another file called “sshbuild” with the following content:
#!/bin/bash ssh username@vmcode 'cd codedir; make'
Note that username and codedir must match the ones you used before.
Setting up XCode
Before we move to XCode, you first need to mount the directory: open the Terminal, cd to your scripts dir, and run sh mountDir. Now you should see the contents of remote dir in your Mac’s ~/path/to/mount/dir.
Start XCode, then click File > New > New Workspace… and chose a location for the workspace file (I used the same as the scripts). You should see a window with three different spaces, they should have texts in the middle, from left ro right, saying “No Files”, “No Editor” and “No Selection”. Open a Finder window in the location ~/path/to/mount/ and then drag the subfolder that has a name starting with “OSXFUSE” to the “No Files” space in XCode.
A popup will show up. Firstly, XCode will prompt you about your build tool. You should type the complete path to the sshbuild script, starting on root, on the “Build Tool” field.
After clicking “Next”, you will be presented something like the image below, you should select the second option, which is “Create folder references for any added folders”.
Basically you are adding a reference to that directory, so instead of working in a local copy, you will edit the remote files directly. Now that you have all the directories in the slave’s codedir in the left panel of XCode, you can now open, edit and save any of your files using XCode. Now hit Command+B to run the build tool, and if everything goes well, you should see a message box saying Build Successful.
Running the Application
As you are developing for Linux, you should preferably debug under Linux. However, I often use an Application called X11, taht can be found in your Applications/Utilities folder. Type this command under X11 console window and the application should run in your Mac as if it was on Linux, even if it needs to open windows:
ssh -X username@vmcode cd codedir ./application
Here I shown you how I develop applications for Linux using the resourceful XCode and all it’s features, without quitting Mac OSX. I hope you found it useful and it helps you by increasing your productivity. Leave a reply, share your thoughts and suggestions and happy coding
– edit 11/01/2012 –
After reading this article, Simon Elliston Ball posted a reply with a link to his blog suggesting a different solution – vagrant – that let’s you achieve the same results, but storing your files on Mac instead of the Linux system. I believe there are some advantages of doing this way, so you may choose the solution that best fits your needs. Thank you Simon.