views:

73

answers:

3

I saw the code on the top of a GLUT demo and I'm curious about its functionality.

Why would someone want to write a #pragma instead of just including the library?

+1  A: 

The #pragma is useful if you're distributing a library. Especially if you have different compiled .libs for different build settings (e.g. debug vs. release, multi-threaded C runtime vs. single-threaded, DLL vs. static library, etc). You can use #ifdefs in your code to select the correct .lib file, rather than requiring your users to set up their build environment to select the correct one.

It reduces support time because your users cannot choose the wrong .lib file.

Dean Harding
+2  A: 

This pragma allows the library author to define library imports based on a range of criteria that can be analysed at compile-time. For instance, you can link against different libs, based on whether you want to link with:

  • multithreading on or off;
  • the shared or static version of the library in question;
  • the shared or static version of the standard runtime library;
  • the debug or release library;
  • the 32-bit or 64-bit library.

With the pragma form, all this can be decided via #ifdef preprocessor tests in one of the library's header files.

Marcelo Cantos
+1  A: 

It's an MSVC-specific pragma that means the named library will be automatically included at link time. The rest of your question about "just including the library" suggests that you're not appreciating the difference between headers and libraries: the header (GL/glut.h) describes what functions the compiler can expect at link time. The library (lib/glut32.lib) provides the implementation of these functions.

As the other answers have explained, it can be convenient, but I personally consider it a terrible idea to use this because it adds another barrier to writing portable code (other platforms and compilers may not support it). This question (thanks, @martin clayton) explains why it's not a good idea for portable code.

Jack Kelly