Linking problems

Colin Peters colin@bird.fu.is.saga-u.ac.jp
Wed Sep 17 21:11:00 GMT 1997


Fredrik Eldh[SMTP:lazermasken@hotmail.com] 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
-- colin@bird.fu.is.saga-u.ac.jp - 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
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list