Jump to content


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


10 replies to this topic

#1 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 06 January 2008 - 03:49 AM

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.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 06 January 2008 - 05:29 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 06 January 2008 - 01:02 PM

"Reedbeta" said:

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.

#4 TheNut

    Senior Member

  • Moderators
  • 1701 posts
  • LocationCyberspace

Posted 06 January 2008 - 01:48 PM

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.
http://www.nutty.ca - Being a nut has its advantages.

#5 martinsm

    Member

  • Members
  • PipPip
  • 88 posts

Posted 06 January 2008 - 02:13 PM

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


#6 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 06 January 2008 - 02:25 PM

TheNut said:

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" said:

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.

#7 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 06 January 2008 - 02:33 PM

martinsm said:

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. :(

#8 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 06 January 2008 - 04:55 PM

netwalker said:

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#9 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 06 January 2008 - 06:34 PM

Reedbeta said:

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?.

#10 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 06 January 2008 - 08:29 PM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#11 JasonWoof

    New Member

  • Members
  • Pip
  • 1 posts

Posted 05 April 2008 - 02:31 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users