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: How to link with third party libraries using gcc


It sounds like the solution could range from something as simple as
including the ".lib" files on the gcc command line on up to complicated
stuff I've never heard of like "decorations", "toolchains", and "calling

I tried the simple suggestion. I copied the ".lib" files to the directory
where my program is located. I then entered the following command:

gcc myprog.c library1.lib library2.lib  -o myprog

I got "undefined reference to ..." errors. One for each library function in
my program. 

Next I removed the ".lib" files from the gcc command string as follows:

gcc myprog.c -o myprog

I got exactly the same errors as I did with the ".lib" files included.
Apparently gcc has no idea what the ".lib" files are.

Since I have no comprehesion of "decorations", "toolchains", and "calling
conventions", or how to find out what that means, I wonder if there's
another explanation. I've got a sinking feeling about this. As if I'm going
where no man has gone before.

Thanks for trying, Brian. I can tell you know what you're talking about.
Unfortunately I'm still completely in the dark.

Brian Dessent wrote:
> km4hr wrote:
>> I thought Windows shared libraries were indentified by a ".dll"
>> extension.
>> However I have a commercial product installed that has a C programming
>> interface. It's libraries have a ".lib" extension. I am totally lost as
>> to
>> how to link my program to these files.
> A .lib file is just an ar archive (.a) file with a different name.  You
> can rename it if you want, but you don't have to.  Just give the name of
> the file on the link command line.  It should also work to specify -lfoo
> to find foo.lib in the search path.  You shouldn't have to futz around
> with converting anything or creating a def file, I don't know why the
> users guide says any of that (but see below.)
> Semantically, a .lib can either be an import library or a static
> library; in either case the file format is the same, but in the former
> it is an aide for linking against a DLL, in the latter it is just like
> static linking on POSIX.  On Cygwin import libraries are typically named
> libfoo.dll.a whereas static archives are just named libfoo.a, but this
> is just a convention.  Some import libraries are named libfoo.a too, for
> example all of w32api.  The ld manual explains the search order for all
> these various extensions when you specify -lxxx:
>           libxxx.dll.a
>           xxx.dll.a
>           libxxx.a
>           xxx.lib
>           cygxxx.dll (*)
>           libxxx.dll
>           xxx.dll
> Note that the last three entail direct-DLL linking without use of an
> import library.  Make sure you read the whole win32 section of the ld
> manual for details of the significance of this.
> What you really have to understand when trying to use these .lib files
> is not about filenames or file formats or making .def files or any of
> that.  All you really need to determine is: 1) what calling convention
> (cdecl or stdcall) does the library use and 2) how are the symbol names
> decorated?  Then just make these match with your toolchain.  Usually the
> two are related, but they are really separate concepts.  For 1) you have
> to get your headers right.  For 2) you create an import library that
> contains the necessary aliases, or if your compiler already uses the
> same decorations as the library, you can just link against the DLL
> directly without an import lib at all.
> Brian
> --
> Unsubscribe info:
> Problem reports:
> Documentation:
> FAQ:         

View this message in context:
Sent from the Cygwin Users mailing list archive at

Unsubscribe info:
Problem reports:

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