This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: WinMain in an own static lib -> _WinMain@16 undefined reference ?! ;.(


On Wed, 3 Mar 2004, G.-B. Hauck wrote:

> Hi Gurus !
>
> I have a problem with cygwin gcc (gcc version 3.3.1 cygwin-special) - i
> think that i just don't understand "ld" - so maybe someone can help me
> ....
>
> For my "Application-Framework" i need to put WinMain(...) in a lib - but
> it doesnt work - please take a look on a minimalistic example:
>
> <snip--------------------------------------------------------/>
> /* winmain.cc  */
> #include <windows.h>
> extern "C" void NewEntry();
> extern "C" int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hPI,
>         LPSTR lpCmdLine,int nCmdShow)
> {
>         NewEntry();
>         return 0;
> }
>
> <snip--------------------------------------------------------/>
> /* test.cc */
> #include <windows.h>
> extern "C" void NewEntry()
> {
>         MessageBox(NULL,"test","test",MB_OK);
> }
>
> ------------
> making test.exe with:
> g++ -c winmain.cc
> g++ -c test.cc
> g++ -mwindows -mno-cygwin -o test.exe winmain.o test.o
>
> is ok -> running test.exe gives you a wonderfull MessageBox ...
>
>
> BUT:
> g++ -c winmain.cc
> g++ -c test.cc
> ar -rs libmaintest.a winmain.o
> g++ -mwindows -mno-cygwin -o test.exe test.o -L./ -lmaintest
>
> /usr/lib/gcc-lib/i686-pc-mingw32/3.3.1/../../../../i686-pc-mingw32/lib/libmingw32.a(main.o)(.text+0x9b):main.c: undefined reference to `_WinMain@16'
> collect2: ld returned 1 exit status
>
> Why ld can't find _WinMain@16 - nm -g libmaintest.a gives:
> winmain.o:
>          U _NewEntry
> 00000000 T _WinMain@16
>
> Calling the linker with my lib "before" test.o, i.e.
>         g++ -mwindows -mno-cygwin -o test.exe -L./ -lmaintest test.o
> gives the same ...
>
> I'm hanging around for two days with this ;-(
>
> TIA and Thank You, Spasibo, Danke, Merci ....
> Georg

Georg,

The way ld works is that all the symbols in object files get included
unconditionally, whereas only the undefined symbols from the libraries get
included.  Also, ld searches forward for undefined symbols.

To resolve _WinMain@16 with a symbol from your library, it needs to come
*after* the library that needs it (/usr/i686-pc-mingw32/lib/libmingw32.a).
Unfortunately (for you), gcc/g++ puts all the default libraries *last*
when it calls ld.  You need to override that order.

IOW, I succeeded in linking your app using the following command:

g++ -mwindows -mno-cygwin -o test.exe test.o -lmingw32 -L./ -lmaintest

You are welcome, pozhalujsta, es ist nichts[*], je vous en prie.
	Igor
[*] this one's automatically translated, sorry. :-)
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]