“外部”的使用C 中的存储类说明符

发布于 2024-08-12 19:22:08 字数 341 浏览 2 评论 0原文

以下 extern 说明符的示例用法如何表现。

我们在文件 one.c 和 Two.c 中都有一个全局变量 int x 我们想在 Three.c 中使用它们,因此在 Three.c 中将此变量声明为

extern int x;

当我们编译和链接这些文件时会发生什么?

我的答案是:所有这些文件的编译应该成功,但是由于 x 的多个声明,链接器应该在链接时标记错误。 C++ 中的行为会有什么不同吗?

在 C 和 C++ 中,有什么方法可以同时从两个文件引用 int x (在 Three.c 中)吗? 在 C++ 中,我想我们可以使用命名空间来实现这一点。正确的?

How does the following example usage of extern specifer behave.

We have a global variable int x both in files one.c and two.c
We want to use these in three.c so have declared this variable in three.c as

extern int x;

What would happen when we compile and link these files?

My answer is: compilation of all these files should succeed, however the linker should flag an error at linking, due to multiple declarations of x.
Would there be any difference in behavior in C++ ?

Is these any way to refer to int x (in three.c) simultaneously from both files, in C and C++.
In C++, I guess we can use namespaces to acheive this. Right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一百个冬季 2024-08-19 19:22:09

请记住,您不能 extern 全局静态变量..!!

Remember that you can NOT extern a global static variable.. !!

残月升风 2024-08-19 19:22:08

默认情况下,全局变量具有外部链接,这意味着它们可以被其他源文件(或“翻译单元”)使用。如果您使用 static 关键字声明全局变量,它们将具有内部链接,这意味着它们将无法被其他源文件使用。

对于具有外部链接的变量,不能有多个同名变量,否则链接器会抱怨。不过,您可以有两个同名的变量,只要至少一个变量具有内部链接,当然您不能在同一个源文件中引用这两个变量。

extern 声明只是对编译器说“这是在另一个翻译单元中定义的外部链接的某个变量的名称”,允许您引用该变量。

C++ 完全相同,除了添加了命名空间。如果将全局变量放在命名空间内,那么它们可以具有相同的名称,而不会出现链接器错误,前提是它们位于不同的命名空间中。当然,对这些变量的所有引用都必须引用全名 namespace::var_name,或者使用 using 声明来建立本地命名空间上下文。

C++ 也有匿名命名空间,这与 C 中的全局变量使用 static 关键字完全等效:在匿名命名空间内声明的所有变量和函数都具有内部链接。

因此,为了回答您原来的问题,您是对的 - 编译会成功,但链接会失败,因为变量 x 具有外部链接的多个定义(具体来说,来自翻译单元 one.ctwo.c)。

two.c 中,无法同时引用两个变量 x。您需要在一个或两个模块中重命名 x,或者切换到 C++ 并在命名空间中放置至少一个 x

By default, global variables have external linkage, which means that they can be used by other source files (or "translation units"). If you instead declare your global variables with the static keyword, they will have internal linkage, meaning they will not be usable by other source files.

For variables with external linkage, you can't have multiple variables with the same name, or the linker will complain. You can have two variables with the same name, though, as long as at least one has internal linkage, and of course you can't reference both of them in the same source file.

An extern declaration is just saying to the compiler "here is the name of some variable with external linkage defined in another translation unit," allowing you to refer to that variable.

C++ is exactly the same, except for the addition of namespaces. If global variables are put inside a namespace, then they can have the same name without linker errors, provided they are in different namespaces. Of course, all references to those variables then have to either refer to the full name namespace::var_name, or use a using declaration to establish a local namespace context.

C++ also has anonymous namespaces, which are entirely equivalent to using the static keyword for global variables in C: all variables and functions declared inside an anonymous namespace have internal linkage.

So, to answer your original question, you are right -- compilation would succeed, but linking would fail, due to multiple definitions of the variable x with external linkage (specifically, from the translation units one.c and two.c).

From three.c, there is no way to refer simultaneously to both variables x. You'll need to rename x in one or both modules, or switch to C++ and put at least one x inside a namespace.

蒗幽 2024-08-19 19:22:08

在 C 中,您可以这样做:

// one.c
static int x;
int *one_x = &x;

// two.c
static int x;
int *two_x = &x;

// three.c
extern int *one_x;
extern int *two_x;

现在您可以明确引用文件 one.c 中的 x 或文件 中的 x来自文件 two.c 的 Three.c

然而,这可能有点超出其价值。也许您应该为全局变量想出更具描述性的名称,而不是尝试绕过 C 的单一全局命名空间的理论方法。

In C, you could do this:

// one.c
static int x;
int *one_x = &x;

// two.c
static int x;
int *two_x = &x;

// three.c
extern int *one_x;
extern int *two_x;

Now you can refer unambiguously to the x in file one.c or the x in file two.c from the file three.c.

However, this might be a bit more effort than it's worth. Perhaps you should be coming up with more descriptive names for your global variables instead of toying around with theoretical ways to circumvent C's single global namespace.

层林尽染 2024-08-19 19:22:08

为了避免生成重复的符号,您应该在单个头文件(.h 文件)中声明 extern int x;,然后拥有将使用 x #include 的所有 .c 文件code> 该头文件,并在 一个 .c 文件中定义或初始化 int x;

To avoid generating duplicate symbols, you should declare extern int x; in a single header file (a .h file), and then have all .c files which will use x #include that header file, and define or initialize int x; in one of the .c files.

你穿错了嫁妆 2024-08-19 19:22:08

您可能对此问题的答案感兴趣。

摘要:链接器可能会也可能不会链接文件失败。这取决于变量的初始化。如果变量在不同的文件中有不同的初始化,那么肯定会失败。

You might be interested by the answers to this question.

Summary: the linker may or may not fail to link the file. It depends on the initialization of the variable. It will definitely fail if the variable has different initializations in different files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文