So, as far as I can tell, there wasn't another issue for this that was recent enough to be compatible with new major updates.
I am programing on a Mac using XCode 4.
I need to write a program for Windows.
How would I do this, and is it the same exact code just compiled differently?!?
Any and all help would be appreciated, I have no idea what I'm doing here.
Thanks!
While the systems are very different, it's possible to write code that compiles on both, just not any code, you'll need to design it carefully; this is called "portable code".
Say you already have your code written; then you can use MINGW (e.g. brew install mingw-w64) for cross-compiling for Windows, you can even test the resulting .exe on Mac OS X using WINE; also clang can be used to compile for Windows, but I never tried that.
Here's an example Makefile from a real project:
toolchain = mingw
#toolchain = wine
mingw- = i586-mingw32msvc-
mingw-cc := ${mingw-}gcc
mingw-cxx := ${mingw-}c++
mingw-rc := ${mingw-}windres
wine-cc = winegcc
wine-cxx = wineg++
wine-rc = wrc
CC := ${${toolchain}-cc}
CXX := ${${toolchain}-cxx}
RC := ${${toolchain}-rc}
LDFLAGS = -mwindows -mno-cygwin
LIBS = -lodbc32 -lole32 -loleaut32 -lwinspool -luuid
app-objs = app.o \
app_resources.o \
input.o \
user_manual.o \
hints.o
all: app.exe
clean:
${RM} ${app-objs} app.exe
app.exe: ${app-objs}
${CC} ${LDFLAGS} -o $@ ${app-objs} ${LIBS}
.c.o:
${CC} -c ${CFLAGS} -o $@ $<
.rc.o:
${RC} ${RCFLAGS} -o $@ $<
.SUFFIXES: .rc .res
.PHONY: all clean
BTW, the example comes from a project originally built with MS Visual Studio by someone else, then years later (2006 IIRC) ported by me to MINGW and cross-compiled from Debian GNU/Linux (but would be identical for Mac OS X), using WINE as a SDK; this was meant to work on FreeBSD and NetBSD, but it was never tested. It's just an example, but most languages do have some way to cross-compile which is more or less equivalent to that.
Now, the really tricky part is the code base, a way to develop an application natively in your Mac and have it also compile for Windows is to use a well-supported portable framework, like Qt, which guarantees you the same API across many OSes.
Still, some bits about the target platforms might be slightly different and may need to be corrected, but tend to be minor changes.
So, in short, by using portable frameworks, it becomes just a matter of setting up your existing build system, whatever it is, to use the right cross-compiling tools, providing said frameworks, and it will compile more or less in the same way as it does for a native build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With