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?
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?
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 #ifdef
s 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.
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:
With the pragma form, all this can be decided via #ifdef
preprocessor tests in one of the library's header files.
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.