Willus.com Home   |   Archive

Willus.com's MinGW/Gnu C Tips

MinGW Tips
Overview
Starter Links
Compile Flags
Fast Math Funcs
DLLs
Globbing
Console
Binary Stdout
Predefined Macros
Show All Tips
 
  Overview
This page has some useful links for the MinGW C compiler and lists some useful information/tips that I've learned about using MinGW. Click on any of the links in the left (blue) or right (green) tables.

Starter Links and References
There are several good links to MinGW documentation at the MinGW site. Advanced users can quickly find answers to many technical questions by searching through the MinGW users mail archive. For introductions, try Colin Peters' Programming Win32 with GNU C and C++ page, and Mike Linkovich's MinGW Startup Guide. Other good references are Matt Daws' All about MinGW page (Matt has a very interesting set of pages in general) and Mumit Khan's GNU Win32 Related Projects page for integrated development environments (IDE's) and other tools that work with Cygwin32. A development environment for MinGW can be obtained from the folks at bloodshed.net.

Compile Flags
I do a lot of numeric (double precision floating point) programming, and my experience is that the -O3 -ffast-math compile flag combination consistently yields just about the best result. Be warned that -ffast-math takes some math shortcuts and does not follow all IEEE error-handling conventions, so if you use this flag, you should verify your results, especially if you need very high accuracy. My experience is that it is well worth the speed boost to use this flag. I have seen on the MinGW users mail archive that -Os (optimize for small code size) can also yield the best results in some cases, perhaps because it allows the code to fit better into the CPU's L1 or L2 cache. I also prefer to use the -Wall flag to report all warnings. This is an excellent practice.

Some Fast Math Functions
When MinGW's pow function became 10x slower in release 3.0 and caused some of my codes which used it heavily to become much slower, I started investigating ways to implement some faster math functions. I first patched the 3.0 pow() function to go back to how it was in 2.0, but then I decided to be more aggressive. The floating point unit in most modern Intel and AMD CPU's (e.g. Pentiums and Athlons) has many built-in transcendental functions such as sine, cosine, arc-tangent, etc. These built-ins are automatically used by the Microsoft C run-time library DLL which MinGW links to by default, but making calls to the DLL typically incurs significant overhead. You can use the header file here to in-line some of these functions for faster performance on Pentiums and Athlons. It requires use of the -ffast-math compile flag. I took some of the code from Chapter 14 (pp. 807-808) of the Art of Assembly Language link below. Note that the exp() and atan2() in-line versions are actually slower on a 64-bit Opteron compile (SuSE Linux 8.0). Also note that these in-line functions do not do any error checking or trapping of any kind.

NOTE! My in-line pow() function now returns correct results if the first argument is zero (Rev 1.01).

NOTE 2! GCC v4.0 will include a more complete set of fast math intrinsics for x87-compatible processors, including fsincos.

x87inline.h   |   x87test.c   |   Art of Assembly
In-line Assy How-To   |   In-line Assy Linux Docs   |   Gnu C In-line Assy docs

Results:    PIII   |   P4 Xeon   |   Opteron (32-bit)   |   Opteron (64-bit)


MinGW and Win32 DLLs
A very cool feature in MinGW is that you can put DLL files directly in the compile/link command (just like .o files) to be linked into your program without the need to create library stub files. For more about how to do that, try Colin Peters' DLL Page (from Colin Peters' Win32 Programming Page). Colin Peters started MinGW. Also, regarding function name mangling (decorating) in MinGW, try Wu YongWei's Page (local copy here).

No Globbing
By default compile, if you run a MinGW compiled command-line utility and pass it a wildcard argument such as *.c, it acts exactly as a unix utility and looks for every file ending in .c in your current file directory, replacing the *.c argument with the name of every one of those files so that your program never actually sees the *.c. To prevent this "globbing," put CRT_noglob.o (in the MinGW library directory) at the beginning of your link list when linking.

No Console Window
To make sure your application doesn't open a console window, use the -mwindows flag when linking.

Binary stdout
If you want the output from the stdout FILE stream to be binary (no translation of CR/LF chars):

  #ifdef __MINGW32__
  /* Required header file */
  #include <fcntl.h>
  #endif
...
  #ifdef __MINGW32__
  /* Switch to binary mode */
  _setmode(_fileno(stdout),_O_BINARY);
  #endif


Seeing all predefined macros
Create a file test.c that has one line in it:

  int main(void) {}

Then compile with this command:

  gcc -dM -E test.c

Even easier (I got an e-mail tip on this)

  gcc -dM -E -

(Type in the above, press ENTER, then, in Unix, press CTRL-D, or from the Windows CMD line, press CTRL-Z or F6 followed by the ENTER key.)

(These work with any gcc port.)

 
MinGW Links
MinGW Home
Users Mail Archive
MinGW Bug List
Gnu C Home

Willus.com Links
Willus.com Home
Win32 Compilers
Win32 Software
Compiler Benchmark

About willus.com This page last modified
Sunday, 02-Jan-2005 15:15:51 EST
News