Statically linking in MinGW(3.4) with (GCC v4.2.1-dw2)

956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Jan 06, 2008 at 03:49

Hi All,

As you can see from the title of this thread I’m having a lot of problems compiling a simple SDL application (actually it doesn’t do anything!) using the -static switch. Now the environment that I’m using for this is MinGW v3.4 downloaded from here (link). In addition the version of GCC that comes with this distribution is (4.2.1-dw2). I’m not really sure that that information is relevant but I just put it there just in case.

Here is the application that I’m trying to statically compile:

#include <sdl\sdl.h>

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);   
    SDL_Quit(); 
    
    return(0);
}

and here is the command line:

gcc -otest.c test.c -O2 -Wall -s -static -lSDLmain -lSDL -lmingw32

depending on where I place the -static switch I get anything from 1 to many errors. Please note however that when I omit the -static switch the program compiles no problem.

some are listed below.

c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libmingw32.a(main.o):main.c:(.text+0xa4): undefined reference to `_WinMain@16'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x40): undefined reference to `_timeKillEvent@4'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x4f): undefined reference to `_timeEndPeriod@4'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x6b): undefined reference to `_timeBeginPeriod@4'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x9e): undefined reference to `_timeSetEvent@20'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x186): undefined reference to `_timeGetTime@0'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_systimer.o):SDL_systimer.c:(.text+0x1c5): undefined reference to `_timeBeginPeriod@4'
...
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_wingl.o):SDL_wingl.c:(.text+0xe21): undefined reference to `_ChoosePixelFormat@8'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_wingl.o):SDL_wingl.c:(.text+0xf09): undefined reference to `_DescribePixelFormat@16'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_dx5yuv.o):SDL_dx5yuv.c:(.text+0x3ae): undefined reference to `_IID_IDirectDrawSurface3'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_dx5events.o):SDL_dx5events.c:(.text+0x17d5): undefined reference to `_IID_IDirectInputDevice2A'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_dx5events.o):SDL_dx5events.c:(.data+0x4): undefined reference to `_GUID_SysKeyboard'
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.2.1-dw2/../../..\libSDL.a(SDL_dx5events.o):SDL_dx5events.c:(.data+0x1c): undefined reference to `_GUID_SysMouse'
collect2: ld returned 1 exit status

Any help on this is much appreciated.

Thanks,

netwalker.

10 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jan 06, 2008 at 05:29

The undefined external errors indicate that the Windows libraries are not being linked with the application. Add the -mwindows option to the command line to correct this.

956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Jan 06, 2008 at 13:02

@”Reedbeta”

The undefined external errors indicate that the Windows libraries are not being linked with the application. Add the -mwindows option to the command line to correct this.

Sorry Reedbeta but unfortunately that didn’t work. I know now that the -mwindows switch adds applicable libraries to the executable. But that doesn’t appear to be the case here since the error message(s) that I’m getting back are exactly the same as those previously shown.

again here is the new command line that I’m using:

gcc -otest.c test.c -O2 -Wall -s -static -lSDLmain -lSDL -lmingw32 -mwindows

Thanks.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jan 06, 2008 at 13:48

The -static option in GCC is designed to prevent accidental linkage with dynamic libraries if they exist. For example, GCC will convert the library option “-lSDL” to either “libSDL.a” or “libSDL.so” and load the first one it finds, but when you specify the -static option it will only look for “libSDL.a”. Now SDL can be compiled and statically linked, so that’s not your problem. Windows on the other hand is only available as a dynamically linkable object (obviously ;).

Having said that, just remove the -static option and you’ll be fine. You don’t need to specify it to produce a statically linked executable. It’s a flimflam feature if you ask me.

8fd4a055522ce713cde7dd1cb4083cb2
0
martinsm 101 Jan 06, 2008 at 14:13

There are some undefined references to WinAPI functions.
Link additionally winmm and gdi32 libraries. Specify additional options:

-lwinmm -lgdi32
956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Jan 06, 2008 at 14:25

@TheNut

GCC will convert the library option “-lSDL” to either “libSDL.a” or “libSDL.so” and load the first one it finds, but when you specify the -static option it will only look for “libSDL.a”. Now SDL can be compiled and statically linked, so that’s not your problem. Windows on the other hand is only available as a dynamically linkable object.

Okay. I’ve just checked the lib directory of my MinGW installation and I have all static libraries installed therefore the -static switch shouldn’t be a problem. However since you mention that windows is in a dll, I used this command line and got the same issues.

gcc -otest.exe test.c -O2 -Wall -mwindows -static -lmingw32 -lSDLmain -ISDL

I’m assuming here that the static option doesn’t apply until the -lmingw32 switch.@”TheNut”

Having said that, just remove the -static option and you’ll be fine.

Yes I don’t get any issues when I omit this switch however I just want to avoid the whole issue of having to distribute my application with all the relevant libraries. I just want to build it and forget about it. Thanks.

956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Jan 06, 2008 at 14:33

@martinsm

There are some undefined references to WinAPI functions.
Link additionally winmm and gdi32 libraries. Specify additional options: -lwinmm -lgdi32

No sorry, those don’t seem to do anything either. But thanks for the assistance. Now I come to look further at the output, why is SDL looking for functions in DirectX and the CD-rom functions, am I missing something here?. This has got me absolutely stumped. :(

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jan 06, 2008 at 16:55

@netwalker

Yes I don’t get any issues when I omit this switch however I just want to avoid the whole issue of having to distribute my application with all the relevant libraries. I just want to build it and forget about it. Thanks.

Well, the Windows libraries are the only ones being dynamically linked here and obviously you don’t need to distribute those as all users will already have them.

956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Jan 06, 2008 at 18:34

@Reedbeta

Well, the Windows libraries are the only ones being dynamically linked here and obviously you don’t need to distribute those as all users will already have them.

True. However what about SDL?. I basically want to take control from my users i.e. I don’t want to rely on them having the correct SDL and associated libraries on their system. Of course I could distribute them with the application, but that to me just seems silly when I could statically link in SDL and libraries and just forget about having to worry about what is on my users system. I’m I being stupid for wanting this?.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jan 06, 2008 at 20:29

No, it’s just that you can specify specifically the path to libSDL.a on the command line instead of using -static -lSDL. So you don’t really need the -static to do static linking, since it seems to be causing so much trouble.

00e329222a5c56d70606e7956b7f3007
0
JasonWoof 101 Apr 05, 2008 at 14:31

Got it!

I’ve been having the same problem more or less, and found a solution by re-ordering the gcc switches. Apparently when cross compiling for windows the order of the parameters for gcc matters. I put the following switches at the END of the commandline for gcc:

-lmingw32 -lSDLmain -lSDL -lwinmm -lgdi32 -mwindows -lm

Here’s the full command in case there’s anything else useful in there:

i586-mingw32msvc-gcc -O3 -I/usr/i586-mingw32msvc/include -I/usr/lib/gcc/i586-mingw32msvc/4.2.1-sjlj/include -I. koch.o sdl.o gui.o -o koch-gui -L/usr/i586-mingw32msvc/lib -lmingw32 -lSDLmain -lSDL -lwinmm -lgdi32 -mwindows -lm