«

»

Jan 08

Print this Post

Developing for Linux under OS X in XCode

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.

Requirements

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.
  • OSXFuse
    • Requirement for the next point
  • SSHFS
    • 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:

ssh username@ip

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:

172.16.68.128 vmcode

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

Warning!

If you are using a real box instead of a VM, I recommend you configure ssh public keys instead of storing your password in plain text. You SHOULD NOT use the same password as of your Mac User
This was pointed by reader Daid, and I explained him I used it in plain text because I’m connecting to this VM, used only for compiling purposes and not accessible from outside, so I’m not that worried about it.

Building remotely

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.

Important!

Any change you make to these files will be reproduced in the remote slave system (our VM), because SSHFS will be syncing in real-time.

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

Conclusion

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.

About the author

Ricardo Dias

Ricardo Dias was born in Entroncamento, Portugal and is studying Electronic and Telecommunications Engineering at University of Aveiro. He is a researcher in robotics and is currently the Team Leader of CAMBADA@MSL team. As a hobby, he is also a freelancer Stage Lighting Designer.

Permanent link to this article: http://ricardo-dias.com/2012/01/08/developing-for-linux-under-os-x-in-xcode/

17 comments

9 pings

Skip to comment form

  1. Neoma Huisinga

    Hey very cool blog!! Man .. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I am happy to find numerous useful information here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

  2. Steven Van Bael

    I’m curious what features you have in XCode that you are missing in other native linux IDEs?

    1. Ricardo Dias

      Hi Steven.

      Thanks for your comment. Well, that’s a matter of personal choice. You could question me why I’m using SSHFS istead of FTPS? Or why someone uses VIM insead of Geany or Nano? It’s up to you to decide.
      I started developing in Linux using the VM, but I was experiencing a lot of annoying bugs, which caused me a drop in productivity in terms of time. And, my friend, as you know, time is money ;) I just present here the solution I found for my problems. If it helps someone I’ll be happy, but I’m not trying to convince you that it’s better to program in XCode rather than any other IDE.

      I hope I answered your question.

      Best regards,
      Ricardo Dias

  3. Daid

    You should really use public/private keys instead of storing your password in a script file.

    1. Ricardo Dias

      Hi Daid.

      Thank you for your comment. That’s a good point when you are concerned about security. I used it in plaintext because I’m using a VM, which is used only for compiling purposes, not accessible from outside, so I’m not that worried about it. However, that’s a good point when you use a real box, especially you have important piece of information there.

      Again, thanks for your comment ;) I’m adding a notice in the article for those using a real machine.

      Best regards,
      Ricardo Dias

  4. uno

    Who in their right mind would switch from e.g. Eclipse to XCode. Eclipse have more plugins, better usability and much better support for teamwork, and it works on Mac; Windows, Linux,…

    1. Ricardo Dias

      Hi Uno.

      First of all, thanks for your comment.
      If you read the article, you will notice you can use whatever IDE you want! The XCode part is just a complementary tutorial to the SSHFS trick.
      I also use Eclipse for some applications. ;)
      However, in this article, I wanted to show how you can use a non-cross-platform application to develop for a different platform.

      Best regards,
      Ricardo Dias

  5. Simon Elliston Ball

    Excellent article.

    You should also checkout vagrant, which does the same thing with the filesystem the other way up (explanation at My blog).

    With this method, it’s harder to tear down the test machine without losing your working copy. With the vagrant method, all the working code is on the OS X system, and shared to the VM.

    I’m sure there are plenty of other similar ways of doing this neater with things like CloudFoundry as well.

    1. Ricardo Dias

      Hello Simon.

      First of all, thank you for your reply.
      I’m glad that you posted another alternative of achieving the same results, because depending on your needs, I believe you can take advantages and disadvantages from using one or another way.

      I’m going to edit the article, mentioning you and your suggestion.
      Again, thank you very much.

      Best regards,
      Ricardo Dias

  6. Stephen

    Hey there,
    I was thinking of getting a mac to develop iPhone apps and wanted to keep using Ubuntu to develop desktop apps. Being able to work in one OS would be awesome.

    P.S. Your attitude and responses in the comments are amazing. Keep it up.

    1. Ricardo Dias

      Hi Stephen.

      Thank you for your reply ;)
      I hope you found here a solution for your development requirements. I’m happy to know all this writing helped at least one person. :)

      Best Regards,
      Ricardo Dias

  7. Onernaxia

    Hello! Just want to say thank you for this interesting article! =) Peace, Joy.

  8. vc

    Hi,
    i do have some problems… i cant fix it somehow :D

    linux debian

    i created “codedir” in root

    on mac /users/myname/Documents/sshlinux folder

    !/bin/bash
    echo password | ./sshfs root@vmcode:/codedir ~/Users/myname/Documents/sshlinux -o ********_stdin

    “shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
    sh: mountDir: No such file or directory

    it just dont work …
    i am blind right ? :D

    1. vc


      mountDir: line 1: !/bin/bash: No such file or directory
      mountDir: line 2: ./sshfs: No such file or directory

      :D

      1. Ricardo Dias

        Hello “VC”.

        First of all, thanks for your comment.

        Please verify if you have the number sign before the exclamation mark.
        Also, “sshfs-static-leopard” is a file that needs to be at the same directory as “mountDir” and must be executable (chmod +x) – download here: http://code.google.com/p/macfuse/wiki/MACFUSE_FS_SSHFS

        Double check if your script has exactly the same contents as mine.

        Best regards,
        Ricardo Dias

  9. Charles

    is it possible to do this slightly in reverse? I have Ubuntu 12.10 as my main OS on my aging Macbook now, but Have a working OS X 10.8 Virtual Machine running Virtualbox.

    1. Ricardo Dias

      Hey Charles

      As long as you have network connectivity and sshfs built for your system, it should work :)

      Best regards,
      Ricardo

  1. Using a Mac and XCode as a Linux development platform - Hack a Day

    [...] Dias] wrote in to tell us about his guide for developing Linux applications on a Mac. He really enjoys the development environment provided by XCode, and it doesn’t take much to [...]

  2. Using a Mac and XCode as a Linux development platform | ro-Stire

    [...] Dias] wrote in to tell us about his guide for developing Linux applications on a Mac. He really enjoys the development environment provided by XCode, and it doesn’t take much to [...]

  3. Using a Mac and XCode as a Linux development platform « Gadgets « The Depot of Talk

    [...] Dias] wrote in to tell us about his guide for developing Linux applications on a Mac. He really enjoys the development environment provided by XCode, and it doesn’t take much to [...]

  4. Using a Mac and XCode as a Linux development platform | My Blog

    [...] Dias] wrote in to tell us about his beam for building Linux applications on a Mac. He unequivocally enjoys a growth sourroundings supposing by XCode, and it doesn’t take most to [...]

  5. Linux News » Developing for Linux under OS X in XCode

    [...] Complete Story [...]

  6. Using a Mac and XCode as a Linux development platform | CisforComputers

    [...] Dias] wrote in to tell us about his guide for developing Linux applications on a Mac. He really enjoys the development environment provided by XCode, and it doesn’t take much to [...]

  7. OS X for the Coding. Linux for deploying. | simon elliston ball

    [...] just came across an excellent article on setting up Xcode, and a generally mac environment to point at an SSHFS disk within a VMWare [...]

  8. Using a Mac and XCode as a Linux development platform -Via Hack a Day | OverView-Effect

    [...] Dias] wrote in to tell us about his guide for developing Linux applications on a Mac. He really enjoys the development environment provided by XCode, and it doesn’t take much to make [...]

  9. Linux Under Mac OS | HACKOLOG - Amazing Hacks and Mods

    [...] following article shows how a user can develop a linux application under the MAC OSX platform. Read on to know more on [...]

Leave a Reply

Your email address will not be published. Required fields are marked *