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.
int main( int argc, char ** argv)
{
printf ("Hello ");
return (0);
}
This creates an executable hello.exe from hello.o. Alternatively, you can compile and link in one step using:
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.
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 ++ - 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.
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
return (0);
}
-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.
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:
// 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 );
#include " dllfct.h "
EXPORT void tstfunc ( void )
{
printf ("Hello ");
}
int main ()
{
tstfunc ();
return (0);
}
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
// gcc -o hello.exe hello.o -L. -ltstdll
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.
- One option is the tool, pexports which is provided in the MinGW Utilities package, mingw-utils. See the Downloads page for the Current version. If your dll has functions that use the Pascal calling convention, you'll need to use the
-o
option. - Another option is the tool, impdef. More instructions on how to create def files from dlls, a copy of impdef and more information on how to use it are available at Colin Peters' site. See the Tutorials section. Other compilers may also supply versions of the impdef program that can be used to create a .def file which will work with any compiler. If you have another version of impdef from another compiler, you may wish to try it. Some handle the Pascal calling convention better than others. Borland has a version of impdef and other compiler utilities available for download at their Borland Community web site. Their Borland C++ version 5.5 compiler includes several utilities to help convert between standard formats, their formats and Microsoft's formats.
- Another option is to use nm which comes with the MinGW distribution. This option will not work for all dlls. Problems may occur if the dll is stripped or compiled as 16 bit. To use this technique, you'll need to filter the output from nm to create a def file. This can be done by hand in an editor or automated using tools like Perl (Practical Extraction and Report Language) or grep (global regular expression print) and sed (stream editor). Even with the automated methods, you may have to make some changes by hand if the Pascal calling convention is used by the dll. See Colin Peters' site for more details on this case. (Versions of sed and grep are available from various sites including archives that host gnuish MSDOS and archives such as Virtually Un*x that contain Win32 ports of common Unix tools and from the self-hosting MinGW port distribution. The ActiveState version of Perl works well on Win32 platforms.) Here are examples of possible filtering techniques.
-
This example uses grep and sed. If you have a dll named file.dll that you wish to create a def file for named file.def, try the following:
echo EXPORTS > file.def
nm file.dll | grep ' T _ ' | sed ' s/.* T _// ' >> file.defdlltool -- def file.def -- dllname file.dll -- output - lib file.a -
This example uses Perl. Copy the following Perl script to a file called dll.pl and use it:
open (OUTFILE, " >dll.def " );
print OUTFILE " EXPORTS " ;
open (INFILE, " dll.fil " );
while ( < INFILE > )
{
if ($_ =~ /T _/)
{
$line = $_;
$line =~ s/.* T _//;
print OUTFILE $line;
}
}
close (INFILE);
close (OUTFILE);nm file.dll > dll.fil
perl dll.plA def file named dll.def will be created. You can rename this as needed. You'll also probably want to delete dll.fil when you're finished with this process.
-
-
If you don't have any of these tools on your system, you can still use nm to create a def file and edit it by hand through an editor. For example:
nm file.dll > dll.fil
find " T _ " dll.fil > dll.defT _
and erase it and anything on the line before it, leaving only the routine names in the file. - If the previous options don't work, you can still try to create a def file using the output from the objdump program (from the MinGW distribution). Here's an example.
objdump - p file.dll > dll.fil
[Ordinal/Name Pointer] Table
in dll.fil and use the list of functions following it to create your def file.
GNU Development Tools Documentation
- GNU Documentation (documentation on all GNU tools)
- GCC Manual
- GNU Linker
- GNU Binutils
- GNU Binary File Descriptor library (BFD)
- GNU Assembler
- Compiled HTML Help (various documentation for GNU tools in MS HTML Help format, provided by Jos� Fonseca).
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:
- Microsoft's on-line library.
- Windows API documentation (size: 12.8MB last updated: Tuesday, 09-Nov-1999 05:15:06 EST). Thanks to Jacob Navia for his persistence in getting Microsoft to grant him permission to redistribute this.
- Win32 API documentation in WinHelp format (6MB). It is a help file released by Microsoft in 1992 to show the differences between the Windows 3.1 and the Win32 APIs.
- WIN32.ZIP (7.9MB) contains Borland/Inprise API docs for Delphi. Note that this only contains the Windows32 API documentation, but not the C runtime library documentation.
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
- Mike Linkovich's MinGW Startup Guide
An excellant primer for getting up and running with MinGW (with an emphasis on graphics/game development). Covers some popular and useful external libraries and IDE's that work with MinGW. - Programming Win32 with GNU C and C++
A tutorial by Coin Peters, the original author of MinGW. - How to make DLLs using GCC
Examples by Mumit Khan.
A set of helper programs and examples to make DLLs in C, C++ and F77 using gcc on Mingw and Cygwin. Released v0.2.5 on Mar 13, 1999. Also contains some pointers to DLL related information available from Microsoft's on-line archives. - How to add compiled HTML help to a MinGW application
A tutorial by Thomas Messenger.
It is very easy for applications that already have a menu. And it is not difficult for any WinMain window style program.
Developing Modules for Specific Software Packages
- Want to build Java JNI's with GNU compilers?
Might want to look at the README.jni file first, and download java-jni-examples.zip. Updated: April 22, 1999. - Want to build Excel-callable DLLs with GNU compilers?
Might want to look at the README.excel-dlls file first, and then download excel-dlls.zip. - Want to build Netscape Client Plug-ins with GNU compilers?
Might want to look at the README.nsplugin file first, and then download nsplugin-examples.zip. April 8, 1999.
Cross-compilation and Using with Other Tools
- -mno-cygwin: Building Mingw executables using Cygwin.
Some insights and recommendations on building Mingw applications using the Cygwin development tools. - Build cross compiler for MinGW
Instructions on how to build cross and Canadian-cross development tools for MinGW. Feb 17, 1999.