Tuesday, March 11, 2008

Minimal indeed

Mingw, or Minimal GNU for Windows, is a really cool project. The idea is to have a gcc that will run anywhere gcc will run and can make Windows executables. It does this well, giving programmers the ability to link native Windows libraries in a non-windows environment. It differs from the standard gcc in a significant way that I discovered this evening: order of arguments is quite important. Whereas standard gcc will accept "gcc code_that_uses_math_dot_h.c -lm" and "gcc -lm code_that_uses_math_dot_h.c" as meaning exactly the same thing, mingw32-gcc is not so forgiving.

egypt@bastet ~ $ cat mintest.c
#if defined( _WIN32 )
#include <winsock2.h>
typedef int SOCKET;
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

#define PORT 1234

int main () {
struct sockaddr_in saddr;
SOCKET client;
#if defined( _WIN32 )
WSAStartup(0x101, &w);
printf("Hello world\n");
#if defined( _WIN32 )
return 0;
egypt@bastet ~ $ mingw32-gcc -lws2_32 mintest.c -o mintest.exe
/tmp/cc7dCAKg.o:mintest.c:(.text+0x29): undefined reference to `_WSAStartup@8'
/tmp/cc7dCAKg.o:mintest.c:(.text+0x41): undefined reference to `_WSACleanup@0'
collect2: ld returned 1 exit status
egypt@bastet ~ (1) $ ls -l mintest.exe
ls: cannot access mintest.exe: No such file or directory
egypt@bastet ~ $ mingw32-gcc mintest.c -lws2_32 -o mintest.exe
egypt@bastet ~ $ ls -l mintest.exe
-rwxr-xr-x 1 egypt egypt 13K Mar 11 21:50 mintest.exe*
egypt@bastet ~ $ file mintest.exe
mintest.exe: MS-DOS executable PE for MS Windows (console) Intel 80386 32-bit
egypt@bastet ~ $

The moral of the story is this: if you're having trouble getting mingw to properly link a library, put the .c as your first argument.

/me grumbles