189

What does #pragma comment mean in the following?

#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
user198729
  • 61,774
  • 108
  • 250
  • 348

5 Answers5

220

#pragma comment is a compiler directive which indicates Visual C++ to leave a comment in the generated object file. The comment can then be read by the linker when it processes object files.

#pragma comment(lib, libname) tells the linker to add the 'libname' library to the list of library dependencies, as if you had added it in the project properties at Linker->Input->Additional dependencies

See #pragma comment on microsoft.com

AJM
  • 1,317
  • 2
  • 15
  • 30
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
  • 3
    Is it MSVC only or GCC / Intel Compiler / Clang has something similar? Is there an option to add a folder path (Like `Linker -> General -> Additional Library Directories`)? – Royi Sep 18 '18 at 20:44
5

I've always called them "compiler directives." They direct the compiler to do things, branching, including libs like shown above, disabling specific errors etc., during the compilation phase.

Compiler companies usually create their own extensions to facilitate their features. For example, (I believe) Microsoft started the "#pragma once" deal and it was only in MS products, now I'm not so sure.

Pragma Directives It includes "#pragma comment" in the table you'll see.

HTH

I suspect GCC, for example, has their own set of #pragma's.

bigbounty
  • 16,526
  • 5
  • 37
  • 65
JustBoo
  • 1,723
  • 9
  • 9
  • 7
    You misunderstood the question. He's not asking what pragmas are, and referring to them as comments instead of directives. He's asking specifically what `#pragma comment` means. – Rob Kennedy Aug 14 '10 at 18:03
  • 3
    Awkward here, it's actually a directive for the linker. – Hans Passant Aug 14 '10 at 18:56
  • @Hans: Hmm, so in this case we'd call it a "linker directive"? – JustBoo Aug 14 '10 at 19:09
  • 3
    No, it's still a compiler directive. It's telling the compiler to leave a note for the linker to include that library. – HerrJoebob Jun 27 '18 at 17:20
  • The link in this answer now redirects to https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?redirectedfrom=MSDN&view=msvc-170 - but the edit queue is full and I can't update with the new URL just now. – AJM Jan 20 '23 at 09:36
4

The answers and the documentation provided by MSDN is the best, but I would like to add one typical case that I use a lot which requires the use of #pragma comment to send a command to the linker at link time for example

#pragma comment(linker,"/ENTRY:Entry")

tell the linker to change the entry point form WinMain() to Entry() after that the CRTStartup going to transfer controll to Entry()

zerocool
  • 3,256
  • 2
  • 24
  • 40
1

These link in the libraries selected in MSVC++.

bobobobo
  • 64,917
  • 62
  • 258
  • 363
1

Pragma directives specify operating system or machine specific (x86 or x64 etc) compiler options. There are several options available. Details can be found in https://msdn.microsoft.com/en-us/library/d9x1s805.aspx

#pragma comment( comment-type [,"commentstring"] ) has this format.

Refer https://msdn.microsoft.com/en-us/library/7f0aews7.aspx for details about different comment-type.

#pragma comment(lib, "kernel32") #pragma comment(lib, "user32")

The above lines of code includes the library names (or path) that need to be searched by the linker. These details are included as part of the library-search record in the object file.

So, in this case kernel.lib and user32.lib are searched by the linker and included in the final executable.

Shrikanth N
  • 652
  • 3
  • 17