The use of #pragma to include libs

Started by
3 comments, last by Mari_p 18 years, 2 months ago
Hi there. Just a basic question: - Why to include the libraries by the code (as shown below) is not considered a good practice?
#pragma comment( lib, "d3d9.lib" )
Thanks in advance.
Advertisement
I don't see anything wrong with it. Except of course it is non-portable, but that doesn't matter so much if your code is non-portable anyway, or you use some #ifdef guards to select it on supporting compilers.
How is it any more non-portable than adding the dependencies to the project properties? The project file format is specific to the IDE you're using anyway. Personally, I prefer to use the #pragma, as it makes it easier to see what libraries are being included just by looking at the code.
I dont think there's really any problem with it. Frankly, I like to link my libs with #pragma anyway. Adding libs to link to is as easy as adding a line of code, rather than navigating through the project properties and adding the additional dependancy.

In terms of it not being portable, what they mean is another compiler isn't going to understand #pragma comment(lib, "libname") - That's specific to MSVC. Unlike adding it to the project properties, when the user compiles it that on another compiler that way, it won't run into the pragma and as such wont error out because it doesn't know the directive. But if you stick it inbetween some #ifdef's to check whether or not it's being compiled with MSVC nullifies the need to worry.

#if defined(_MSC_VER)  #pragma comment(lib, "d3d9.lib")  #pragma comment(lib, "d3dx9.lib")#else  // please link to d3d9.lib and d3dx9.lib when you link the program.#endif

The above is a sure fire way to make sure it doesn't attempt that on a compiler other than MSVC.
Thanks friends. I thought there were other problems besides the non-portable code. I like #pragma to link my libs.

Thanks for the explanations.

This topic is closed to new replies.

Advertisement