Specify the libraries for the linker to use


How do I specify the libraries to be searched by the linker?

  • MinGW supports libraries named according to the "<name>.lib" and "<name>.dll" conventions, in addition to the normal "lib<name>.a" convention common on *nix systems. To include libraries named according to any of these conventions, simply add an associated "-l<name>" specification to the compiler command, ensuring it is placed after the name of the module in which the reference appears.
  • Note that, if the library is not found in any of the default library search paths, you may also need to insert an appropriate "-L<dir>" switch to specify its location; (it is recommended that you place the "-L<dir>" switch before the "-l<name>" specification which requires it).
  • Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; if you have a library named according to the aberrant "lib<name>.lib" convention, it will not be found by an "-l<name>" specification -- if you cannot rename the library, you must use the form "-llib<name>" instead.
  • Finally, please do not fall into the all-too-common trap of placing any "-l<name>" specification before the name of any source or object module which requires it. Where the source file bar.c has dependencies on the library archive libfoo.a, the command:
    $ gcc -lfoo bar.c

    is incorrect, and will surely lead to "undefined reference" errors. The correct form for this command is:

    $ gcc bar.c -lfoo

    Also, please do not be misled into the belief that this is a peculiarity of MinGW; it is a common requirement of the GNU linker, on all platforms which use it, and indeed of UNIX linkers in general, that libriaries must be specified after the names of all dependent modules. Please resist any temptation to report failure to support the former (incorrect) usage as a MinGW bug, for it is no such thing. If any other user attempts to convince you that the incorrect form works perfectly well on GNU/Linux (for example), then please politely refer them to the GCC Manual, and to the GNU Binutils Manual, point out that such usage is definitively incorrect, and advise them that they may have been getting away with such incorrect usage because some versions of the ELF platform linker, (of which GNU/Linux is an example), will forgive the error, specifically in the case of shared libraries; (such linkers will punish the error equally strenuously on every platform, in the case of statically linked libraries).

This wiki is not a forum for discussion of usage issues. Please use the mingw-users@lists.osdn.me list instead. We do not allow creation of comments by anonymous or untrusted users, on any page.
Site Status

Site maintenance performed successfully, site online Dec 13th 12:00 AM Eastern.