Passing names of libraries to linker.

Rupert Wood me@rupey.net
Wed Jun 29 23:26:00 GMT 2005


Arturas Moskvinas wrote:

> I do not think this behavior (#pragma comment(lib, "library.lib"))
> is written in C or C++ standart (gcc is refering to these standarts
> not to microsoft or any other...).

GCC has plenty of extensions of its own. Virtually all compilers do. And it
does even borrow some of the Microsoft ones, e.g. dllimport/export, for
Windows compatibility.

The point of #pragma comment(lib, ...) is convenience. The MFC header file
afx.h, for example, has a large preprocessor block in it which works out
which MFC lib flavour you need from the #defines you're building with
(NDEBUG, _UNICODE, etc.) and comment-libs the right one in for you
automatically. All you need to do is #include <afx.h> and it All Just Works.
There's no reason this couldn't work for say KDE headers or ncurses on unix
say.


Angel,

No - nice though it'd be, I don't think there is.

One thing you'll discover coming to unixes from Windows is that it's very
sensitive about the order you link libraries in; if you were implementing
#pragma comment(lib) on unix, you'd probably get all the commented libraries
and put them at the front of the link order but you'd need some logic to
work out what order they should be linked in in; you can imagine it'd be
easy enough to get the same two comments in different orders in different
compilation units because the header files were included the other way
around. That not insurmountable, but there's probably plenty of nasty corner
cases if you think about it hard enough. Best leave it to someone else :-)

Rup.



More information about the Gcc-help mailing list