MinGW - Documentation

Table of Contents


Installing MinGW

For now, installation information can be found on the Download page.

Back to index


Compiling and Building with MinGW

How to create a console application

Here's an example. The following is a code sample for a simple C program. Cut and paste it into a file named hello.c to try it out.

#include <stdio.h>

int main(int argc, char **argv)
{
  printf ("Hello\n");
  return (0);
}
           
If you want to create a console mode executable hello.exe from a c file called hello.c, try the following:
gcc -c hello.c
           
This compiles hello.c into an object file, hello.o
gcc -o hello hello.o
           
This creates an executable hello.exe from hello.o. Alternatively, you can compile and link in one step using:
gcc -o hello hello.c 
           

The following is a code sample for a simple C++ program. Cut and paste it into a file named hello.cpp to try it out.

#include <iostream>
int main(int argc, char **argv)
{
 std::cout << "Hello" << std::endl;
 return (0);
}
           

For the C++ program, use the following to compile and link:

g++ -c hello.cpp
g++ -o hello hello.o
           

How to create a windows application?

Here's an example. The following is a code sample for a simple Windows program. Cut and paste it into a file named hello.c to try it out.

   #include <windows.h>

   int WINAPI WinMain (HINSTANCE hInstance, 
                        HINSTANCE hPrevInstance, 
                        PSTR szCmdLine, 
                        int iCmdShow) 
   {
      MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
      return (0);
   }
               

If you want to create a Windows executable hello.exe, from a c file called hello.c, try the following:

   gcc -c hello.c
               
This compiles hello.c into an object file, hello.o
   gcc -o hello hello.o -mwindows
               
This creates an executable hello.exe from hello.o The -mwindows switch is needed to create Windows executables instead of console applications. It assures the appropriate Windows libraries are linked in for you. To get a console screen along with a standard windows application, add the -mconsole flag as well as -mwindows.

If you have resources from a resource file (.rc) that also need to be added to your executable, you'll need to compile the resource file as well as your other source files and include the compiled resources when linking to create the executable. Here's an example that shows how to compile and link in a resource file named resfile.rc.

   windres -o resfile.o resfile.rc
   gcc -o hello hello.o resfile.o -mwindows
               

How to create a dll

Here's an example. Cut and paste the following into a file named dllfct.h:

   #ifdef BUILD_DLL
   // the dll exports
   #define EXPORT __declspec(dllexport)
   #else
   // the exe imports
   #define EXPORT __declspec(dllimport)
   #endif

   // function to be imported/exported
   EXPORT void tstfunc (void);
               

Cut and paste the following into a file named dllfct.c:

   #include <stdio.h>
   #include "dllfct.h"

   EXPORT void tstfunc (void)
   {
      printf ("Hello\n");
   }
               

Cut and paste the following into a file named hello.c:

   #include "dllfct.h"

   int main ()
   {
      tstfunc ();
      return (0);
   }
               

To create the dll and an executable that uses it, try the following:

   gcc -c hello.c
   gcc -c -DBUILD_DLL dllfct.c
   gcc -shared -o tst.dll -Wl,--out-implib,libtstdll.a dllfct.o
   gcc -o hello.exe hello.o -L./ -ltstdll
               

For more information on dlls, see http://www.nanotech.wisc.edu/~khan/software/gnu-win32/dllhelpers.html

How to create a def file for a dll

There are several methods that can be tried in order to create a definition file (.def) when one is not supplied.

Back to index


GNU Development Tools Documentation

Back to index


Win32 API Documentation

MinGW uses the runtime libraries distributed with the OS, but the API documentation is not supplied with the OS and is not re-distributable. If you don't own a copy of Microsoft development tools or MSDN subscription, you can still access the API documentation from the following places:

Back to index


Tips, Howtos, Contributed Documentation, etc.

** NOTE **

Some of the information in this section is grossly out of date, and currently here for historical purposes only. In particular, more recent and relevant information about "-mno-cygwin" and using MinGW within a Cygwin development environment can be found on the FAQ page.

Introductions and Tutorials


Developing Modules for Specific Software Packages


Cross-compilation and Using with Other Tools

Back to index