Category Archives: MingW

MingW: UNIX incompatible & compile verbose

Building Mingw executables using Cygwin

One particular vexing source of error is when you include a file that
exists for Cygwin, but not for Mingw32; the compilation goes ok, but
the compiler is really using the *WRONG* include file and you get link
time errors.

Let’s say you have some code that uses the POSIX/BSD “times” function
which is not part of the ANSI standard and does not exist under Mingw32
runtime.

  #include
  #include
  int main () {
      struct tms time_info;
      times (&time_info);
      printf ("user_time = %d, system_time = %d\n",
              time_info.tms_utime, time_info.tms_stime);
      return 0;
  }

Now build the “times” program:

  $ gcc -c -mno-cygwin times.c
  $ gcc -o times -mno-cygwin times.o
  times.o(.text+0x34):times.c: undefined reference to `times'
  collect2: ld returned 1 exit status

Notice that the compilation goes just fine, but you get an undefined
reference to the “times” function.

What happens is that Cygwin first looks in the mingw32 include directory
the <sys/times.h> file and it can’t find it; it then looks at the default
directory and does find it and uses it and everything is fine at this
point. However, at link time, the Mingw32 runtime library lacks this
function and you get an undefined error. Sometimes these are so confusing
that you may get sidetracked and not look at the real source of the
problem — your own code!

So, how do you diagnose these errors? Whenever you get such errors, use
the -v (verbose) and -H (show include files) options when compiling
to see where include files are coming from. Then use -v option when
linking to see what libraries are being linked in.

Another source of error is creating Mingw DLLs using dllwrap. The current
version of dllwrap does not know the -mno-cygwin option and will
incorrectly add Cygwin libraries when creating DLLs. The workaround is
quite simple: add the --target=i386-mingw32 option instead. In the future,
dllwrap will simply translate the -mno-cygwin option to the --target
option and it will just work.

Rekursives make mit getrenntem Object-Verzeichnis

TOPLEVEL = .
SUBDIRS = \
    obj

all : all_sub Aufgabe9

include $(TOPLEVEL)/Makefile.inc

STATIC_LIBRARY = obj/lib.a \
                 obj/planet/lib.a

Aufgabe9 : $(STATIC_LIBRARY)
	$(CC) -o $@ obj/main.o $(STATIC_LIBRARY) $(LDFLAGS)
CC       = g++
AR       = ar
CFLAGS   = `sdl-config --cflags` -I/usr/glew/include -I$(TOPLEVEL)/include -D_REENTRANT -Wall -std=gnu++0x
LDFLAGS  = `sdl-config --libs` -L/usr/glew/lib -lopengl32 -lglu32 -lglew32 -lSDL_ttf
OBJECT   = $(SOURCES:%.cxx=%.o)

# CFLAGS  = -IC:\MinGW\msys\1.0\libsdl\include\SDL -D_GNU_SOURCE=1 -Dmain=SDL_main -I$(TOPLEVEL)/include -D_REENTRANT -Wall
# LDFLAGS = -LC:\MinGW\msys\1.0\libsdl\lib -lmingw32 -lSDLmain -lSDL -mwindows -lopengl32 -lglu32 -lSDL_ttf

all_sub:
	if test "$(SUBDIRS)" != ""; then \
		for subdir in $(SUBDIRS); do \
			(cd $$subdir; make all); \
		done; \
	fi;

clean: clean_sub
	rm -f *.o *.a $(CLEANFILES)

clean_sub:
	if test "$(SUBDIRS)" != ""; then \
		for subdir in $(SUBDIRS); do \
			(cd $$subdir; make clean); \
		done; \
	fi;

$(LIBRARY): $(OBJECT)
	$(AR) rcu $(LIBRARY) $(OBJECT)
#	$(AR) rcs $(LIBRARY) $(OBJECT)

%.o: $(TOPLEVEL)/$(CURRENT)/%.cxx
	$(CC) -c $(CFLAGS) $<

.SILENT: all_sub clean_sub
CURRENT  = src
TOPLEVEL = ..
LIBRARY  = lib.a

SUBDIRS = \
    planet

SOURCES = \
	main.cxx \
	GLSL.cxx \
	textfile.cxx \
	Vector.cxx \
	Matrix.cxx \
	Keyboard.cxx \
	Settings.cxx \
	Game.cxx \
	Camera.cxx \
	Light.cxx \
	Texture.cxx \
	TextureFactory.cxx \
	World.cxx \
	text.cxx \
	Debug.cxx \
	3ds.cxx \
	Ship.cxx \
	Shot.cxx \
	Billboard.cxx \
	Explosion.cxx \
	DisplayList.cxx

include $(TOPLEVEL)/Makefile.inc

all: all_sub $(LIBRARY)
CURRENT  = src/planet
TOPLEVEL = ../..
LIBRARY  = lib.a

SUBDIRS = 

SOURCES = \
	Planet.cxx \
	Ring.cxx \
	Sun.cxx \
	Earth.cxx \
	EarthMoon.cxx \
	Jupiter.cxx \
	Mars.cxx \
	Mercury.cxx \
	Neptun.cxx \
	Pluto.cxx \
	Saturn.cxx \
	Saturnring.cxx \
	Uranus.cxx \
	Venus.cxx

include $(TOPLEVEL)/Makefile.inc

all: all_sub $(LIBRARY)

Kompiliere SDL, SDL_ttf, freetype und glew mit MingW

SDL-1.2.14.tar.gz
SDL_ttf-2.0.11.tar.gz
freetype-2.4.8.tar.gz
glew-1.7.0.tgz
$ cd SDL-1.2.14
$ ./configure --prefix=/usr/libsdl --enable-stdio-redirect=no

$ cd freetype-2.4.8
$ ./configure --prefix=/usr/freetype2

$ cd SDL_ttf-2.0.11
$ ./configure --prefix=/usr/libsdl

$ cd glew-1.7.0
$ sed -i 's/GLEW_DEST ?= \/usr/GLEW_DEST ?= \/usr\/glew/g' Makefile
$ make install.all
export PATH=$PATH:/usr/libsdl/bin:/usr/freetype2/bin:/usr/glew/bin

Erste Windows Programmierung unter MingW

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

MingW Konsole: Sprache festlegen

$ gcc -o cmd cmd.c 
cmd.c:20:12: Fehler: In Konflikt stehende Typen für »WinMain«
winbase.h:1251:14: Anmerkung: Vorherige Deklaration von »WinMain« war hier
$ export LANG=en_US
$ gcc -o cmd cmd.c 
cmd.c:20:12: error: conflicting types for 'WinMain'
winbase.h:1251:14: note: previous declaration of 'WinMain' was here