192

What does #pragma comment mean in the following?

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

5 Answers 5

223

#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

1
  • 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, 2018 at 20:44
6

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.

5
  • 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. Aug 14, 2010 at 18:03
  • 3
    Awkward here, it's actually a directive for the linker. Aug 14, 2010 at 18:56
  • @Hans: Hmm, so in this case we'd call it a "linker directive"?
    – JustBoo
    Aug 14, 2010 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, 2018 at 17:20
  • The link in this answer now redirects to learn.microsoft.com/en-us/cpp/preprocessor/… - but the edit queue is full and I can't update with the new URL just now.
    – AJM
    Jan 20 at 9: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()

1

These link in the libraries selected in MSVC++.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.