I've had to do a bit of cross-platform development for various projects over the years. For most of my development, my IDE of choice is Eclipse on Linux. To build in Windows, I create a new Visual Studio project and manually import the source files. This works reasonably well for small, one-off projects, but it's a pain for larger projects that require regular modifications. One issue is that new files added to the Eclipse project must manually be imported to the Visual Studio project. Ideally, all changes made to the project in the Linux environment should be automatically realized in the Windows environment. Eclipse is a cross platform IDE, so this should be a snap, right? This article explores how to create one Eclipse project that can be used to compile on Windows using the Visual Studio compiler or on Linux using GCC, and share the source code via SVN. We'll also explore using a cross compiler on Linux to build a Windows executable. First, here's a bit on the environment.
Installation Woes
In both environments, I'm running 64-bit Eclipse Indigo with the plugins CDT, Subclipse, and SVNKit (included with Subclipse). Take a look at Figures 1 and 2 for version information. I previously tried Subclipse version 1.8 but couldn't get it to work with SVNKit. For Visual Studio, I'm using the 2010 Express Edition.
Installing Subclipse in Eclipse on Windows was problematic. Several times, I attempted to install by pointing to the URL, but the installation would pause indefinitely at around 30% complete. Downloading the zip file (site.1.6.18.zip) from the Subclipse website finally did the trick. However, I had to de-select the "SVN Revision Graph" option under "Subclipse" before the installation would work; the installer complained that the feature couldn't be found.
Once SVN was installed, a configuration step had to be completed under Linux and Windows: We need to tell the IDE which SVN library to use. By default, it tries to use the JavaHL library, which we didn't install. To change from JavaHL to SVNKit, browse to Window→Preferences. In the Preferences screen, expand Team and select SVN. Under "SVN Interface," select SVNKit and you'll see a screen similar to Figure 3.
Now the IDE is setup on Windows and Linux, and we can create a project. All the example projects will use the C++ source code shown in Listing One:
Listing One: A simple C++ program as an example.
#include <iostream> include <list> using namespace std; int main() { list<int> lst; lst.push_back(3); lst.push_back(1); lst.push_back(4); lst.push_back(1); lst.push_back(5); lst.push_back(9); lst.push_back(2); lst.push_back(6); lst.push_back(5); lst.push_back(3); lst.sort(); cout << "sorted list: "; for (list<int>::iterator itr = lst.begin(); itr != lst.end(); itr++) cout << (*itr) << " "; cout << endl; return 0; }
I first create a project under Windows and go through the process of configuring the Visual Studio toolchain. (We'll later look at porting the project to Linux, followed by creating an app under Linux and porting to Windows.)