Subject: RE: Linking problems
From: [email-masked] (Colin Peters)

17 Sep 1997 21:26:04 -0700
Message-ID: <01BCC432.7FDB0400.cygnus.gnu-win32@gbird0>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
To: "'Fredrik Eldh'" <[email-masked]>
Cc: "'GNU-Win32'" <[email-masked]>

Fredrik Eldh[SMTP:[email-masked]] wrote:
>I'm a newbie when it comes to Gnu-Win32 and I'm having trouble when I 
>try to link any Win32 program. The linker says I have 'undefined 
>references' to all the Win32 API functions. I use the following command 
>line to compile and link: "gcc program.c".
>
>Now, please don't tell me to go look this up in an FAQ or something, 
>I've already done that and it didn't make me wiser.

It's pretty simple actually, and it should be a FAQ. GNU-Win32 requires
that you explicitly link the import libraries for whatever Win32 API
functions that you are going to use, with the exception of kernel32,
which is linked automatically (because the startup and/or built-in code
uses it). For example, to use graphics functions (GDI) you must link
with gdi32 like this:

  gcc -o foo.exe foo.o bar.o -lgdi32

or (compiling and linking in one step):

  gcc -o foo.exe foo.c bar.c -lgdi32

The regular Cygnus setup allows you to use the option -mwindows on
the command line to include a set of the basic libraries (and also
make your program a GUI program instead of a console program),
including user32, gdi32 and, IIRC, comdlg32.

A few notes:

 - Do not ever include -lkernel32 on your link line (unless you are
   invoking ld directly, in which case I share your pain).

 - Do not include the same import library twice on your link line.

 - Put import libraries last on your link line, or at least after
   all the object files and static libraries that reference them.

The first two are related to problems the linker has currently when
import libraries are referenced twice. Tables get messed up and
programs crash randomly. The last point has to do with the fact that
GCC processes the files listed on the command line in sequence and
will only resolve references to libraries if they are given after
the file that makes the reference.

Anyway, hope that helped.

Colin.

PS. Actually GCC is not being any different from MSVC or other
    Win32 compilers about this. Just open up the build options
    dialog and look for the list of libraries to link. By default
    you'll find a whole mess of import libraries including all
    the ones I mentioned above (at least the last time I looked).

-- Colin Peters - Saga Univ. Dept. of Information Science
-- [email-masked] - finger for PGP public key
-- http://www.fu.is.saga-u.ac.jp/~colin/index.html
-- http://www.geocities.com/Tokyo/Towers/6162/

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".