views:

1631

answers:

2

I have a static library file called libunp.a, I do know I could use gcc -lunp xx to link to the library.

I could use #pragma comment(lib,"xxx.lib") to tell the microsoft c/c++ compiler to include the library,how could I do it under linux/gcc?

thanks.

+1  A: 

There doesn't seem to be any mention of any equivalent pragmas in the GCC manual's page on pragmas.

One reason I saw for GCC not supporting linking in source code was that sometimes, correct linking depends on link order; and this would require you to make sure that the linking order happens correctly no matter the order of compilation. If you're going to go to that much work, you may as well just pass the linker arguments on the command line (or otherwise), I suppose.

Mark Rushakoff
Same applies for some, if not all Windows compilers. But yes, such pragmas are __bad__
Mads Elvheim
"correct compilation depends on link order." -- No, as the linked article states, correct LINKING depends on link order.
Windows programmer
+1  A: 

Simple; you can't. GCC has no such equivalent. Specify -l as a gcc parameter, create a linker script, call ld, call 911 or whatever.

Not that such a pragma even makes sense. Libraries should be specified during the linking step. Such information simply doesn't belong inside a translation unit. A translation unit can be preprocessed, compiled and assembled even without a linking stage. The toolchain used by Visual Studio allows this because it is braindead and always performs linking.

You might want to save yourself some tedious typing and create a MakeFile for your project: GNU Make Manual

Mads Elvheim
During recent decades it has become popular for programmers to design .h files and .lib files to have some kind of relationship with each other. In such a case it DOES make sense for an .h file to contain a pragma telling the linker to link the corresponding .lib file. Sure gcc doesn't have it, but that doesn't mean it doesn't make sense.
Windows programmer
Then you assume that libraries don't require any additional paths nor flags during linking. This is mostly true on Windows, hence why the pragma 'works' in the Visual Studio environment. Other gcc pragmas are at least consistent across different flavors of Linux/Solaris/BSD as long as you use the same gcc version. Paths are however, not. Which is why it makes complete sense to seperate the build stage in configure scripts or makefiles. It has nothing to do inside translation units. Try to port software sometime ;-)
Mads Elvheim
@Mads: The fact that Unix is a fractured platform that requires separation of build and link phases, and all the pain that goes with it, is hardly a justification for trashing the way Windows does it, which is much easier to deal with and allows library implementors to take that pain away from their users. The idea that the Unix approach is harder but better is pure irrational masochism.
Marcelo Cantos
Windows is shameful as a development platform. Please don't defend it, it doesn't deserve it, and it certainly doesn't need it.
Matt Joiner