Saturday 29 September 2012

Using C++ Eclipse IDE for editing code locally and compiling it remotely on linux


In several companies where I worked the target platform for the application was Linux/Solaris but the desktop PC was running Windows. So I was not able to compile code in the IDE running on desktop.

Some people run IDE on Linux server using Windows PC as the XServer in this case. But I don’t like this approach as it is quite slow for me. Another people use terminal based editors like vim or emacs. But I could not get used to them. Here I describe the way I configure my environment.

In brief, I use Eclipse + Cygwin + Unison.

How does it work?

I have an svn copy of the project on my Windows desktop. I have eclipse running on Windows and I use it to browse and edit code in this local svn copy. I configured eclipse to launch an external command on each file save. The external command is unison, which synchronizes the local svn copy with a copy of the same project on remote Linux server. It takes about 1-2 seconds for unison to figure out that you changed just a few files since last run and copy the changed files over ssh to the server. Eclipse runs this command in background so even when I save files each 5 mins, I don’t notice that. I use Cygwin for bash scripting the long command arguments, so in eclipse the command looks like [bash sync.sh]

Also I use custom Eclipse make command which looks like [ssh server “cd svn/location; make”].
Eclipse feeds the output of this command to console and it is able to parse errors and warnings from this output and highlight the relevant lines in code.

So I am compiling code remotely, but use eclipse locally without compromises on IDE speed.

How to install?

1. Install Eclipse IDE for C++ from http://www.eclipse.org/downloads/

2. Install Cygwin from http://cygwin.com/install.html. That is download setup.exe, set all by default (unless you want some changes) and choose in addition openssh, bash, svn and libiconv2 packages.


4. Checkout svn view using either Cygwin command line:

$ svn co svn://server/path/trunk projectX

OR using Eclipse svn plugin Subclipse inside the IDE.

5. I usually configure include paths for C++ project to be able to browse through system or boost libraries header files. So I copy include files from linux to windows and setup the paths in Eclipse.


6. Prepare the synchronization and make scripts

sync.sh:
#!/bin/bash
/path/to/unison –auto –batch H:/svn/projectX ssh://server/path/to/projectX

make.sh:
#!/bin/bash
name=$1
shift
ssh server “cd ~/path/to/projectX/$name/build; make –j $*” 2>&1 | sed ‘s#/home/$USER/path/to##g’ | iconv –c –t ascii

I had to use iconv to convert to ascii encoding as eclipse does not show well some of the symbols of gcc ouput.
sed in make.sh is used to make eclipse possible to identify the file in the gcc output to project files.
My project consists of several apps each one residing in each projectX subfolder, so I configured several make commands and I use make.sh in the following way:

make.sh dir target
e.g. make.sh encoder tests

You must launch unison command for the first time manually without –auto flag. Unison will ask you what files you want to copy to remote server and if you answer to permanently ignore some files it will put them to
%USER_WIN_HOME%/.unison/default.prf

My default.prf file looks like the following:
ignore = Regex .*/CMakeFiles
ignore = Regex .*/build/.*

You also need to copy unison on target linux server and make sure it is in your unix $PATH.

7. Configure Eclipse

7.1 Properties for projectX: C/C++ Build : uncheck “Use default build command”

7.2 Properties for projectX: Builders -> Add New builder -> Program
 
Check items for
- Launch in background
- During auto builds
Other items if desired

7.3 Uncheck all builders except CDT and sync files. For CDT builder uncheck all operations except “During manual builds”
CDT builder is required to launch make targets which we configure on the next step.


7.4 Setup automatic builds on file save

Now when you save the file sync files builder will launch unison in background.

7.5 Window -> Show view -> Make Target:  New target



Now you can launch the compilation from eclipse.

Enjoy!

______________________________
H: in all examples is a virtual drive configured using Windows subst command:
subst H: C:\Users\vadim\work
which I put in Startup windows auto-startup folder.

Saturday 3 March 2012