Jump to content
  • Advertisement
Sign in to follow this  
Mari_p

The use of #pragma to include libs

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks friends. I thought there were other problems besides the non-portable code. I like #pragma to link my libs.

Thanks for the explanations.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!