gcc for Windows

Introduction

Most Unix and Linux users know the GNU Compiler Collection (gcc). This software compiler system is being used to build a lot of software, including modern operating systems such as Linux and BSD, which means it is well tested and maintained. Additionally, gcc is available under GPL v3 – the most popular free software license. This gives everyone the right to examine and modify its source code and also download it free of charge.

gcc can provide many benefits to developers targeting the Windows platform. In contrary to Visual Studio and most other compiler tools for Windows, gcc is Open Source Software completely free of charge and supported by a large community. Projects targeting multiple operating systems will also save time by using just one compiler system, and if you already know gcc you don’t have to spend time learning new compilers.

Compiling for 64-bit Windows

There are multiple ports of gcc to Windows, but MinGW (Minimalist GNU for Windows) seems to get the most traction. But currently MinGW can only build 32-bit binaries. In order to be able to target both 32-bit (x86) and 64-bit (x64) Windows versions, we will use a flavour of it called MinGW-w64 (aka. mingw64). In addition to the compiler binaries, the package contains headers (e.g. stdlib.h, strings.h, etc.) and libraries necessary to build applications.

Hello World

It is time to get our hands dirty and compile a C-version of Hello World. We will create one for 32-bit Windows and one for 64-bit Windows. The build platform used is a 32-bit Windows XP, but any version of Windows newer than Windows 2000, including Windows Vista and Windows 7 should give the same results. 64-bit versions of Windows will work as well.

  1. Go to the MinGW-w64 download page. We need two toolchains – one for targeting win32 and another for targeting win64. New packages are frequently uploaded, but we just pick the newest personal  build (I had some bad experiences with the automated builds). Open “Toolchains targetting Win32″ and “Toolchains targetting Win64″, followed by “Personal Builds”. At the time of writing, we can open for example “sezero_20110510″ and pick “mingw-w32-bin_i686-mingw_20110510_sezero.zip” and “mingw-w64-bin_i686-mingw_20110510_sezero.zip“, but also see MinGW-w64 package naming conventions.
  2. Unpack the first package to “c:\mingw32″ and the second to “c:\mingw64″.
  3. Open a text editor (e.g. Notepad), paste in the famous world greeting and save it under “c:\tmp\hello.c”.
    #include <stdio.h>
    int main()
    {
      printf("Hello World!\n");
      return 0;
    }
  4. Open a Command Prompt, cd to “c:\tmp” and compile with the following commands. See the related cross-compilation post for an explanation of the compiler names.
    "c:\mingw32\bin\i686-w64-mingw32-gcc.exe hello.c -ohello-w32.exe"
    "c:\mingw64\bin\x86_64-w64-mingw32-gcc.exe hello.c -ohello-w64.exe"
  5. On 32-bit windows (aka. x86), you can only run “hello-w32.exe”, while you can actually run both on 64-bit Windows (aka. x64) because of an emulation layer called Windows 32-bit On Windows 64-bit. It is however much more efficient to run native binaries, that is, “hello-w64.exe” on 64-bit Windows versions.

Gcc compile and run of Hello World on Windows.

Build Environment

When building larger projects, we usually want a Unix-like shell in order to run additional tools like configure, make and automake. On Windows, we can use MSYS (Minimal SYStem) to get a shell and the basic commands often used in build-scripts, like grep and sed. This means we can compile code on Windows that will run on Windows, which seems like the natural way to go.

However, note that with the MinGW-w64 suite, it is possible to compile code on Linux that will run on Windows. This is also known as cross-compilation, and will greatly increase the build speed but also allow to use any native Linux shell, rendering MSYS unnecessary. In another post, we consider how to cross-compile Windows binaries on Linux.

Furthermore, real-world applications depend on common libraries. There is a related article on how to compile and use the OpenSSL library on Windows.

mingw-w64-bin_i686-mingw_20101003_sezero.zipmingw-w64-bin_i686-mingw_20101003_sezero.zip
This entry was posted in GCC Windows and tagged , , , , , , , , . Bookmark the permalink.

18 Responses to gcc for Windows

  1. Pingback: Compile for Windows on Linux | Techblog

  2. Paul Hetherington says:

    Looking for GCC that will cross compile for ARM Cortex A8 (AM3517) running Windows CE6.

    Does it exist?

  3. Toyin says:

    Hello,
    I have been battling with compiling a Unix (Linux) code to run on my Visual C++ 6.0. I use a windows XP machine. I also installed codeblocks IDE (Lovedev) which i heard is cross platform but things never worked out.
    The code is publicly available code which i want to use to learn some concepts (see attached).
    Can you please advise me on a step-by-step process on what to do.
    Thanks

    • author says:

      Hi Toyin,

      Sorry for the late response, but I do not have any experience with the IDEs you mention. You can use standard GNU build tools (autoconf, automake, configure, make, libtool, etc.) with MinGW-w64, which probably the easiest way to get Unix tools compiled for Windows.

      This entails passing the –host parameter to configure. I can cover how to set up a proper build environment for MinGW on Unix and build for example OpenSSL if that is of interest to anyone?

      Thanks.

  4. Pingback: Bookmarks for September 6th through September 7th « Mark's life

  5. Pingback: OpenSSL for Windows | BlogCompiler

  6. nilesh vora says:

    not working on windows 7 32 bit…..please give me full clarification….send me a mail

  7. wisementor says:

    Download GCC binaries for different target architectures at http://www.comsytec.com

  8. leelavathy says:

    Got stuck up in compiling natives for 32 bit and 64 bit versions. This post is a great help for me …
    Thank you.

  9. Michel says:

    Thanks for the example above, but it is not general enough. It doesn’t explain how to invoke the linker separately (to link a program made from several source files, for example). Also the program above is a console program, launched from cmd.exe. The example above doesn’t say how to compile a program with a Windows interface (in graphic mode). There is no such documentation in the package. Do you know please a website that explain how to use this software package ?

    • author says:

      Michael,

      (I assume 32-bit Windows compilation below and don’t prefix the full path of the compiler for brevity. The sources are pasted in-line at the bottom.)

      You can ask gcc to create object files only by using the -c option. For example,

      i686-w64-mingw32-gcc.exe -c message.c
      i686-w64-mingw32-gcc.exe -c main.c

      This will create message.o and main.o. You can link these together by invoking gcc again by issuing the following.


      i686-w64-mingw32-gcc.exe message.o main.o -o myapp.exe

      How to open message boxes and interface with the Windows GUI was out of the scope of this article, but there is nothing stopping you from doing that. The sources below will create a message box if you compile them as outlined above.

      However, writing a GUI application is a huge topic of itself, and I suggest you look at the Microsoft documentation directly if you intend to do that.

      If your application is going to be more than a few windows, you should consider using a GUI builder that will generate the code for you.


      // main.c
      #include "message.h"

      int main(int argc, char **argv)
      {
      MessageBoxFromOtherObject("Show this, please");
      return 0;
      }

      // --- message.h
      void MessageBoxFromOtherObject(const char *message);

      // --- message.c
      #include "message.h"
      #include

      void MessageBoxFromOtherObject(const char *message)
      {
      MessageBox(NULL, message, "My message box", MB_OK);
      }

      I hope all this helped you in to get the right direction.

      • Michel says:

        Thank you for your answer, but, maybe because I didn’t explain well, it’s not exactly the answer to my question. Also, I will let down the second part of my question about Windows, given it is better to only ask one thing per message. :)

        I said in my previous message that the example you gave wasn’t general enough:
        gcc foo.c
        is good if there is only one source file. So you suggest me to link several modules this way:
        gcc foo1.o foo2.o -ofoo.exe
        It works. But imagine I have 100 of such source files. The cmd.exe line will not be long enough to accept such a long command line.

        So, again the example above isn’t general enough. I mean it doesn’t solve EVERY situation. The only thing that works UNIVERSALLY, whatever the number of source files, whatever the length of the input line is to invoke the compiler and the linker separately, like in the example below:

        gcc -c foo1.c
        gcc -c foo2.c
        gcc -c foo3.c

        ld foo1.o foo2.o foo3.o -o foo.exe

        But if I do that, I get the following errors:
        ld: i386 architecture of input file `a.o’ is incompatible with i386:x86-64 outpu
        t
        a.o:a.c:(.text.startup+0×7): undefined reference to `___main’

        c:\Users\Michel\GNU Compilers\X86\P01>

        What did I do wrong ? Why does it work when ld is invoked through gcc (without -c) and it doesn’t when I invoke gcc and ld separately ?

  10. Ranjan Barman says:

    I am new to Linux. I need gcc compiler. where can i get it?

    • author says:

      Hi Ranjan,

      If you want to just do native compilation, you can get gcc from your distribution’s package manager.
      For example, on Ubuntu you can install gcc with “sudo apt-get install gcc”.

Leave a Reply

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


+ three = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>