C++ “#pragma 评论”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3484434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:56:50  来源:igfitidea点击:

What does "#pragma comment" mean?

c++visual-c++macrospragma

提问by user198729

What does #pragma commentmean in the following?

#pragma comment以下是什么意思?

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

回答by KeatsPeeks

#pragma commentis 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是一个编译器指令,它指示 Visual C++ 在生成的目标文件中留下注释。然后链接器在处理目标文件时可以读取注释。

#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

#pragma comment(lib, libname)告诉链接器将“libname”库添加到库依赖项列表中,就好像您已将它添加到项目属性中一样 Linker->Input->Additional dependencies

See #pragma commenton MSDN

请参阅MSDN 上的#pragma 评论

回答by JustBoo

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 once”交易,它只出现在 MS 产品中,现在我不太确定。

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

Pragma 指令它在您将看到的表中包含“#pragma comment”。

HTH

HTH

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

例如,我怀疑 GCC 有自己的一套#pragma。

回答by bobobobo

These link in the libraries selected in MSVC++.

这些链接在 MSVC++ 中选择的库中。

回答by Shrikanth N

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 指令指定操作系统或机器特定(x86 或 x64 等)编译器选项。有多种选择。详细信息可以在https://msdn.microsoft.com/en-us/library/d9x1s805.aspx 中找到

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

#pragma comment( comment-type [,"commentstring"] )有这种格式。

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

有关不同评论类型的详细信息,请参阅https://msdn.microsoft.com/en-us/library/7f0aews7.aspx

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

#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.liband user32.libare searched by the linker and included in the final executable.

因此,在这种情况下,kernel.libuser32.lib由链接器搜索并包含在最终可执行文件中。

回答by zerocool

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 commentto send a command to the linker at link time for example

MSDN 提供的答案和文档是最好的,但我想添加一个我经常使用的典型案例,例如需要使用它#pragma comment在链接时向链接器发送命令

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

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

告诉链接器将入口点形式更改WinMain()Entry()之后CRTStartup将控制转移到Entry()