ysflight.com - Cross-Compiling 32-bit OpenGL program in 64-bit Ubuntu


2014/07/11

I'm writing this document on July 11, 2014.  But, I could not cross-compile 32-bit OpenGL program in 64-bit Ubuntu (until I found this work around, of course) only because of a known bug.  I hope someone fixes it and un-necessitate this document soon.  I saw some questions from someone who were trying to do the same, but I didn't find an answer nowhere.  Probably they are still looking for an answer.  I hope this work around helps them.

Until now, I was compiling 32-bit binary on 32-bit Ubuntu, and 64-bit binary on 64-bit Ubuntu.  I launch a different virtual machine in VirtualBox for each case.  But, it's not very efficient.  It is the same operating system.  I have been wanting to build and package both 32-bit and 64-bit version binary from one script.

There are options -m32 and -m64 of gcc, which force the compiler to spit out 32-bit and 64-bit binary respectively.  However, if I tried gcc -m32 in 64-bit Ubuntu, I was getting an error complaining predef.h not found.  Today, somehow that problem came back to my mind.  I searched for the solution and learned that what I needed was multilib libraries.

Further search indicated that not all libraries are as convenient as multilib, but I can do  sudo apt-get install lib???:i386 to get 32-bit version libraries in most cases.  That made me hopeful.  I thought it should be easy to set up cross-compiling so that I can compile both 32-bit and 64-bit binaries in 64-bit Ubuntu.

Then I hit a brick wall.  After installing multilib, gcc told me that libGL and libGLU cannot be found.  Not a big deal.  I should be able to apt-get install libgl1-mesa-dev:i386 and libglu1-mesa-dev:i386 to install 32-bit version of libGL and libGLU, right?  Unfortunately it turned out not so easy.  apt-get gave me an error and could not install 32-bit libGL and libGLU.  Search implied that it is one of the known bugs of Ubuntu (or Debian).

https://bugs.launchpad.net/ubuntu/+source/mesa/+bug/1317113

If Microsoft leaves known bugs unfixed long time, it's a shame.  Like Microsoft Office Drawing Tools have been broken for long time.  But, Linux is an open source software.  All I can do is shrug and give up, usually.

But, this time I didn't give up.  What gcc is complaining was missing 32-bit libGL and libGLU.  I have them in my 32-bit Ubuntu.  I can copy them from there.  About half-day struggle, this strategy worked.  I finally was able to build 32-bit OpenGL program in 64-bit Ubuntu and run.

My impression is that some people are having the same problem.  I summarize what I did below.  Hope this information helps some fellow OpenGL programmers.

Step 1.  Installing Mesa libraries in 32-bit Ubuntu

I am assuming you are running Ubuntu in VirtualBox.  You should be able to do the same from a natively-running Ubuntu.  You probably need to have 32-bit Ubuntu of the same version as your 64-bit Ubuntu.  I am using Ubuntu 12.04.

The goal is to copy OpenGL libraries, you need to install libgl1-mesa-dev and libglu1-mesa-dev, at least.

Then, create a shared folder of VirtualBox.  You will copy library files to the shared folder, which later will be copied into 64-bit Ubuntu.

Step 2.  Copy Mesa libraries to the shared folder in 32-bit Ubuntu

Type the following:

% locate libGL

to find where Mesa libraries are located.  It shows you libraries that starts with libGL.  So, you will also see GLU libraries. 

In my environment, I had to copy:

/usr/lib/i386-linux-gnu/libGL*
/usr/lib/i386-linux-gnu/mesa/libGL*

to the shared folder. 

I also had to copy ALSA and Pulse Audio libraries, which also were not cross-compiling friendly.  I could use the same technique for these libraries.  Of course if you only need OpenGL, you don't need ALSA and Pulse Audio.

You don't need 32-bit Ubuntu at this point.  You may want to keep it only for testing your 32-bit binary.

Step 3.  Installing multilib in 64-bit Ubuntu

Type following in 64-bit Ubuntu to install multilib packages.

% sudo apt-get install gcc-multilib
% sudo apt-get install g++-multilib

This creates a directory for 32-bit libraries.  In my environment, it was /lib32.  But, some people are saying it is /usr/lib32.  You can find by yourself.

Step 4.  Copying 32-bit library files to the 32-bit library directory.

If you haven't mounted the shared folder where 32-bit library files are copied, please do so now.  If you are running native Ubuntu, both 32-bit and 64-bit, maybe you can do it via a USB drive.  What's important is to copy 32-bit library to the appropriate directory.  In my environment, I had to type:

% sudo cp /ysshare/libGL* /lib32

/ysshare was my shared directory.

You may want to keep copied libraries in a separate location and make a symbolic link so that you can clean up the copied libraries easier if necessary later time.

Step 5.  Installing other 32-bit libraries

You also need the following packages to cross-compile an OpenGL program.

% sudo apt-get install libX11-6:i386
% sudo apt-get install libxcb-glx0:i386
% sudo apt-get install libdrm-dev:i386
% sudo apt-get install libxdamage-dev:i386
% sudo apt-get install libx11-xcb-dev:i386
% sudo apt-get install libglapi-mesa:i386
% sudo apt-get install libxext6:i386
% sudo apt-get install libxxf86vm-dev:i386

Then, all you need to do is compiling with -m32 option.  I tested with PolygonCrest and YSFLIGHT.  I could build 32-bit binaries of the both programs in 64-bit Ubuntu, which ran ok in both 32-bit and 64-bit Ubuntu.

If you mixed up something, you may see an error message whey you try to run your 32-bit OpenGL program in 64-bit Ubuntu like lib???.so.1 not found.  Then, you may have to do:

% sudo ldconfig

Probably that will fix.

So, now I am able to compile both 64-bit and 32-bit binaries of PolygonCrest from one script.  I need to do the same for MacOSX, then I can upload the program.

Comments are welcome.  Send E-Mail to: 

RETURN TO TOP PAGE

This page is link free, but please link to http://www.ysflight.com.  Please use one of following banners.