C99 on Linux and Windows.

By | September 28, 2017

  I recently decided to use C for a few projects mainly just for the practice, using an array of open source libraries (GLFW, GLad, OpenDE) it occurred to me that in *theory* as C is probably one of the more portable “native” languages, then it should be possible to make code that will compile to either a Linux executable or a Windows one.  What’s more it shouldn’t need lots of #ifdef clauses scattered about the code making it an less than easy to read code base (we’re not talking about simple include guards here!)

While I have tried similar simple experiments using MSYS2, this time it would be an excuse to use the Linux subsystem for Windows, there’s plenty of short articles out there that will guide you through the relatively simple process of getting Ubuntu’s kit working on Windows 10.  Hopefully you’ll probably use Windows more often than I do, so you won’t have the four hour wait I did for Windows 10 to update itself…

I was pleasantly surprised to find that with a little to and fro I had my C code compiled and working.  I did have to make one minor change, in one function I’d written ages ago I was using the uint type which while that worked with Linux, didn’t under windows – and as it happens it looks like uint isn’t part of c99.  Rather than rewrite it (a simple find and replace), I just made a quick typedef fix.

Fair enough changes in a Makefile, but I was again pleasantly surprised at the rather minor nature of the changes needed

CC=gcc
CXX=g++
KZ=kazmath/kazmath
FLAGS= -g -c -std=gnu99 -Iinclude -I$(KZ) -Iode-0.15.2/include
LIBS= ode-0.15.2/ode/src/.libs/libode.a lib/libkazmath.a -lglfw -lGL -lm -ldl -lpthread

At the beginning of the Linux Makefile I set up some simple variables – (I manually compile just part of kazmath into a library containing just the bits I tend to use.)  I use g++ to link everything as OpenDE contains C++ code (handily its got a nice C API).

Here’s the only changes I needed to Makefile.win …

CC=x86_64-w64-mingw32-gcc 
CXX=x86_64-w64-mingw32-g++
KZ=kazmath/kazmath
FLAGS= -g -c -std=gnu99 -Iinclude -I$(KZ) -Iode-0.15.2/include
LIBS= ode-0.15.2/ode/src/.libs/libode.a lib/libkazmath.a lib/libglfw3dll.a -lopengl32 -Wl,-subsystem,windows

Obviously there are different compiler names, but other than that its all very straight forward, one thing to note is you need to suppress the console window that will appear in addition to your graphics window.

-Wl,-subsystem,windows

I had thought that maybe I wouldn’t need to recompile OpenDE or kazmath (both having been compile to object code with the same(ish?!) compiler) but that wasn’t the case.  Sadly I didn’t have time to get threading working with the windows version of OpenDE – but I’m sure it’s not insurmountable.

My over all impression on Ubuntu’s packaging of Bash for Windows is on the whole positive, it allows you to build stuff and test it (something you might struggle to do with 64bit exe’s on wine…) It isn’t quite as efficient as Bash on Linux but that’s not entirely a surprise.

I did look at doing the same from Linux (even if you couldn’t test the result) but stalled at linking to opengl – I could probably get it working but I’d still have to find a windows machine for testing, so I’m not too sure I’m bothered.

 

Leave a Reply

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