声明extern"C"是什么?对于可变的意思? [英] What does declaration extern "C" for variable mean?

查看:311
本文介绍了声明extern"C"是什么?对于可变的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明extern"C"表示变量是什么意思?

What does declaration extern "C" for variable mean?

extern "C" BOOL (WINAPI* const _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &ExtRawDllMain;



更新:
是内部变量声明还是外部变量声明?



Update:
Is it declaration of internal variable or external variable?

推荐答案

http://answers.yahoo.com/question/index?qid=20080326074018AAarBJ9 [

extern "C"防止C++编译器修改名称,使函数具有C链接.例如,请参见:位于StackOverflow的外部"C" [
extern "C" prevents the C++ compiler of mangling the name, making the function have C linkage. See, for instance: extern "C" at StackOverflow[^].
:)


这意味着该函数的代码是用C编写(更重要的是,编译).
现在您要从C ++使用它,就C和C ++而言,就编译器而言,函数定义与WINAPI *const RawDllMain(HINSTANCE, DWORD, LPVOID)相同,但是由于C ++中的扩展允许函数重写,因此链接器的符号名称为不同.

在C中,根据使用的约定,符号名称将为RawDllMain_RawDllMain.
在C ++中,参数和返回类型被拼合"到名称中,从而使C ++名称为?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z.使用结构或类等自定义类型时,更容易发现编码后的变量类型.

如果然后要从DLL调用函数RawDllMain,则要搜索的函数名称取决于您的语言,C还是C ++.
如果您使用的是GetProcAddress,那么如果您没有使用extern "C",则需要搜索功能?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z.

也有其他用途.
如果您使用的是用C编写的第三方库,并且想从C ++使用它,则可能需要包装
It means that the code for that function is written (and more importantly, compiled) in C.
Now that you want to use it from C++ the function definition is the same as far as the compiler is concerned WINAPI *const RawDllMain(HINSTANCE, DWORD, LPVOID) in both C and C++, however due to extensions in C++ that allow for function overriding, the symbol name for the linker is different.

In C, the symbol name will be either RawDllMain or _RawDllMain depending on the convention used.
In C++, parameter and return types are "mangled" into the name, making the C++ name ?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z. The encoded variable types are easier to spot when using custom types like structs or classes.

If you were then to call the function RawDllMain from the DLL, the function name that you would search for depends on your language, C or C++.
If you are using GetProcAddress then you would need to search for the function ?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z if you did not use extern "C".

There are other uses for this too.
If you are using a 3rd party library that is written in C and you want to use it from C++ then you may need to wrap
extern "C" {
#include "the_lib\lib.h"
}


出于相同的原因,该库的包含内容也是如此.但是,这些库通常会自动执行此操作.


around the includes for that library for the same reasons. Often these libraries would automatically do this however.


这篇关于声明extern"C"是什么?对于可变的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆