Archive

Posts Tagged ‘MinGW’

Compile Windows programs on Linux

September 26th, 2010 No comments

Windows?? *GASP!*

Sometimes you just have to compile Windows programs from the comfort of your Linux install. This is a relatively simple process that basically requires you to only install the following (Ubuntu) packages:

To compile 32-bit programs

  • mingw32 (swap out for gcc-mingw32 if you need 64-bit support)
  • mingw32-binutils
  • mingw32-runtime

Additionally for 64-bit programs (*PLEASE SEE NOTE)

  • mingw-w64
  • gcc-mingw32

Once you have those packages you just need to swap out “gcc” in your normal compile commands with either “i586-mingw32msvc-gcc” (for 32-bit) or “amd64-mingw32msvc-gcc” (for 64-bit). So for example if we take the following hello world program in C

#include <stdio.h>
int main(int argc, char** argv)
{
printf(“Hello world!\n”);
return 0;
}

we can compile it to a 32-bit Windows program by using something similar to the following command (assuming the code is contained within a file called main.c)

i586-mingw32msvc-gcc -Wall “main.c” -o “Program.exe”

You can even compile Win32 GUI programs as well. Take the following code as an example

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char *msg = “The message box’s message!”;
MessageBox(NULL, msg, “MsgBox Title”, MB_OK | MB_ICONINFORMATION);

return 0;
}

this time I’ll compile it into a 64-bit Windows application using

amd64-mingw32msvc-gcc -Wall -mwindows “main.c” -o “Program.exe”

You can even test to make sure it worked properly by running the program through wine like

wine Program.exe

You might need to install some extra packages to get Wine to run 64-bit applications but in general this will work.

That’s pretty much it. You might have a couple of other issues (like linking against Windows libraries instead of the Linux ones) but overall this is a very simple drop-in replacement for your regular gcc command.

*NOTE: There is currently a problem with the Lucid packages for the 64-bit compilers. As a work around you can get the packages from this PPA instead.

Originally posted on my personal website here.




I am currently running a variety of distributions, primarily Ubuntu 14.04.
Previously I was running KDE 4.3.3 on top of Fedora 11 (for the first experiment) and KDE 4.6.5 on top of Gentoo (for the second experiment).
Check out my profile for more information.
Categories: Tyler B, Ubuntu Tags: , , , ,

Programming on Linux

September 27th, 2009 No comments

Now that school as resumed I am getting to spend a lot of time with my Linux install doing day to day productive tasks. The most recent thing that I have had to deal with is programming on Linux. As part of my Computer Graphics class the professor recommended that we install Dev-C++ and GLUT (with related libraries) so that we can code some OpenGL goodness. Well seeing as Dev-C++ is a Windows only IDE that just won’t do.

Instead I opted to install the C and C++ development tools for Eclipse. This works perfectly and within minutes I had a simple “Hello, world!” program up and running. In the past I had only ever used Eclipse for Java programming, however that may be changing permanently in the future.

Next up I had to install GLUT. After a quick search in my Fedora repositories I only had the option to install freeglut listed. So I figured ‘what the heck’ and gave it a try anyway. To my surprise this worked perfectly, even when I still referenced #<GL/glut.h>. This means I can use all of this great open source software to develop the same C++ code that I can then submit to my professor to mark on his Window’s machine.

The only issue I have found is I cannot for the life of me get MinGW to compile the code to a Windows exe. Yet even barring this I must say that all in all I am very impressed!