Search code examples
c++windowspragma

Why and when do we need to use #pragma


I was learning a winsock client server program and came up through #pragma comment(lib,"ws2_32.lib"). Understood its need here. But what are the other instances I can use it and why do I need to use it?


Solution

  • #pragma 's are implementation defined compiler commands.

    That means, each compiler could treat or support pragmas in a different way.

    They are used for example for generating user defined warnings or errors as with

    #pragma warning WARNINGMSG

    or #pragma error ERRORMSG

    or as include guard with #pragma once at the top of a headerfile.

    What #pragma comment is used for, you can find nice explained here:

    What does "#pragma comment" mean?

    But after all, I would advise you to avoid pragmas as far it is possible, because as they are almost all, implementation defined, your code will be limited in portability if you use them.