简体   繁体   English

如何外部声明一个外部“C”变量

[英]How to extern declare an extern “C” variable

I wants to export a global variable from dll I define the global variable like below.我想从 dll 导出一个全局变量,我定义如下全局变量。 Assume this variable is defined in A.cpp假设这个变量是在 A.cpp 中定义的

extern "C" __declspec(dllexport) int A;

Meanwhile, within the dll, another source file B.cpp wants to use and modify this value.同时,在 dll 中,另一个源文件 B.cpp 想要使用和修改这个值。 I am wonderring how to declare the variable within B.cpp before using it.我想知道如何在使用之前在 B.cpp 中声明变量。

extern "C" int A; ? 

If in this case, how does compile recognize between declare and definition?如果在这种情况下,编译如何识别声明和定义?

extern extern "C" int A; ?

It's definitely ill-formed.它肯定是不正确的。

Per [basic.def]/2.2, a declaration of an object at namespace scope is a definition unless:根据 [basic.def]/2.2,在命名空间 scope 中声明 object 是一个定义,除非:

it contains the extern specifier (9.2.1) or a linkage-specification 19 (9.11) and neither an initializer nor a function-body ,它包含extern说明符 (9.2.1) 或链接规范19 (9.11),既不包含初始化程序,也不包含函数体
... ...
19 Appearing inside the brace-enclosed declaration-seq in a linkage-specification does not affect whether a declaration is a definition. 19链接规范中出现在大括号括起来的声明序列中不会影响声明是否是定义。

Thus:因此:

extern "C" int A; is a declaration.是一个声明。

extern "C" int A = 0; is a definition.是一个定义。

The below defines A and B , and declares C : the effect is the same as it would be without the extern "C" block, except that the entities declared have C linkage instead of C++ linkage.下面定义AB ,并声明C :效果与没有extern "C"块的情况相同,只是声明的实体具有 C 链接而不是 ZF6F87C9FDCF8B3C3F07ZF93F1 链接。

extern "C" {
    int A;
    extern int B = 0;
    extern int C;
}

extern extern "C" int A; ?

extern "C" can be used as a block: extern "C"可以用作块:

extern "C"
{
    extern int A;
}

The declaration needs to be in a header file like Ah or other code files cannot use it.声明需要在 header 文件中,例如 Ah 或其他代码文件不能使用它。 The dllexport is especially useless without a header file.如果没有 header 文件,dllexport 尤其无用。 You probably want some kind of DLLEXPORT macro so you can define it to dllexport and dllimport as necessary.您可能需要某种 DLLEXPORT 宏,因此您可以根据需要将其定义为 dllexport 和 dllimport。 See pretty much any Windows DLL code.查看几乎所有 Windows DLL 代码。

Then in a cpp file you include the header.然后在一个 cpp 文件中包含 header。 That allows your code to use the variable which is declared extern .这允许您的代码使用声明为extern的变量。

In one of your cpp files you include the header AND then define the variable using the same type and name and no extern.在您的一个cpp 文件中,您包含 header 然后使用相同的类型和名称定义变量,而不使用外部变量。 The linker will then put the data storage for it into the same module as the rest of that cpp file and all other uses of the name get linked to that definition.然后,linker 会将其数据存储放入与该 cpp 文件的 rest 相同的模块中,并且该名称的所有其他用途都链接到该定义。

However, just like private member variables in C++ it is a bad idea to expose global variables in a DLL.然而,就像 C++ 中的私有成员变量一样,在 DLL 中公开全局变量是个坏主意。 It is much better to hide access to them behind function calls.最好在 function 调用后面隐藏对它们的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM