tags:

views:

161

answers:

3

In Microsoft visual c++ compiler, you can specify linker options using

 #pragma comment(lib, "MSVCRT") //links with the MVCRT library

see this page

I find this feature very useful because linker errors are common and i want to just place all the linker options in my source code instead of specifying them to the compiler.

question: Is there a way to do this in gcc (or dev-cpp or codeblocks ide)?

Thanks in advance.

+1  A: 

In short, GCC does not support specifying libraries to link in the source code.

If your IDE handles the compilation and linking process, you can probably add references in your IDE and have it worry about passing the correct options to gcc for each unit.

Matti Virkkunen
+2  A: 

GCC doesn't support this because to link correctly, the order in which you link your objects matters.

See also my answer and others in the question "#pragma comment(lib, “xxx.lib”) equivalent under Linux?"

Mark Rushakoff
The link order article you linked to says that the link order matters, basically, only for older linkers, since most modern linkers search all linked libraries anyway, independent of order. So it's IMO a rather questionable reason not to support this. At least the Boost people use it, and they're typically not associated with bad C++ programming :)
OregonGhost
A: 

Given that link options and library names vary very much from system to system, I am quite glad to have them separated from my source code files and thus can keep the source code system independent.

Then the build system can decide how to build on what system. Much cleaner approach overall, I'd say.

ndim
What is clean in having to include a library two times (include file and lib)? I like the Boost approach. Just include the include file for a library you want to use and that's it.
OregonGhost
On some systems, you need to explicitly specify `-lm` at link time to get math functions, on some systems you do not (or even must not). That is not clean, but real. The cleanest way to deal with stupid stuff like that? Outside of my source code files, definitely.
ndim
Reality is not necessarily clean, that's true. However, still the Boost way is in my opinion by far the easiest one, because you basically can't do much wrong. That's a good thing.
OregonGhost