变量重定义,嵌入式c

发布于 2024-11-15 17:27:55 字数 425 浏览 3 评论 0原文

我正在开发一个嵌入式 c 项目,并且遇到全局变量重新定义的问题。

我已将函数声明和定义分成几个 .h 和 .c 文件。其中许多函数需要访问全局变量,这些变量在 global_vars.h 中定义。因此,在头文件的开头,#ifndef、#define 部分中,“global_vars.h”。如果我不这样做,正如您可能想象的那样,我会收到一堆未定义的变量错误。

但是,即使 global_vars.h 具有 #ifndef _GLOBAL_VARS_H_ #define... #endif,我也会收到所有全局变量的重新定义错误。我的猜测是,当链接器尝试链接各个目标文件时,它会看到由于所有“blah_blah.h”文件中的#include“global_vars.h”而导致的重新定义。不过,据我了解,#ifndef... 可以解决这个问题。

有什么我忽略的吗?

提前致谢

I'm working on an embedded c project and am having issues with global variable redefinition.

I've split up the function declarations and definitions into a handful of .h and .c files. Many of these functions need access to global variables, which are defined in global_vars.h. Therefore, at the beginning of the header files, inside the #ifndef, #define section, "global_vars.h". If I don't, as you might imagine I get a bunch of undefined variable errors.

However, even though global_vars.h has the #ifndef _GLOBAL_VARS_H_ #define... #endif, I get redefinition errors for all the global variables. My guess is that when the linker tries link the various object files, it sees the redefinition due to the #include "global_vars.h" in all the "blah_blah.h" files. It was my understanding, though, that the #ifndef... takes care of this issue.

Is there something I'm overlooking?

Thanks in advance

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

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

发布评论

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

评论(3

硬不硬你别怂 2024-11-22 17:27:55

链接器永远不会在 global_vars.h 文件中看到任何内容,除非——坏消息! -- 一些全局变量实际上是在该文件中定义的。 global_vars.h 应该只保存这些全局变量的声明,而不是(好吧,几乎从不)它们的定义。

在 global_vars.h 中,您应该有如下声明:

extern int some_global;

您不允许有:

int some_global;

如果您在 global_vars.h 中有定义,那么,是的,它们将在链接时被多重定义,因为每个 #includes global_vars 的 .c 文件.h 对每个定义的变量都有自己的定义。

当然,extern 全局变量的所有定义都必须位于some .c 文件中。通常哪个 .c 文件并不重要。通常,所有全局变量定义都位于名为(惊讶!)global_vars.c 的文件中。

因此,请确保 global_vars.h 中没有任何全局变量定义,这样您就会处于良好状态。

The linker never sees anything in the global_vars.h file, ever, unless -- bad news! -- some of the globals are actually defined in that file. global_vars.h should hold only declarations of those global variables, never (well, almost never) their definitions.

In global_vars.h, you should have declarations like:

extern int some_global;

You are not allowed to have:

int some_global;

If you have definitions in global_vars.h then, yes, they'll be multiply defined at link time because each of the .c files that #includes global_vars.h will have its own definition of each defined variable.

All of the definitions of the extern globals must be in some .c file, for sure. Usually it doesn't matter which .c file. Often all of the global-variable definitions are in a file called (surprise!) global_vars.c.

So make sure there aren't any global-variable definitions in global_vars.h and you'll be in good shape.

街角迷惘 2024-11-22 17:27:55

在 H 文件中定义全局变量不是一个好主意。如果您在 C 或 C++ 文件中执行此操作,并将 H 文件包含在其他模块中,并将这些全局变量作为外部变量,那就更好了。
就像这样>>>

我的模块 c 文件

unsigned short int AGLOBAL = 10; // definer and initializer

void MyFunc(void)
{
  AGLOBAL+=1; // no need to include anything here cause is defined above
  // more .....

}

我的 H 文件 globals.h

// this is to include only once
#ifndef MYH
#define MYH
extern unsigned short int AGLOBAL; // no value in here!

#endif

其他模块 c 文件

#include globals.h

char SomeOtherFunc(void)
{
  AGLOBAL+=10; // ok cause its defined by globals.h
  // do more....
}

Is not a good idea to define globals in an H file. Better if you do that in a C or C++ file and you include and H file in other modules with those globals as externals.
Like this>>>

My module c file

unsigned short int AGLOBAL = 10; // definer and initializer

void MyFunc(void)
{
  AGLOBAL+=1; // no need to include anything here cause is defined above
  // more .....

}

My H file globals.h

// this is to include only once
#ifndef MYH
#define MYH
extern unsigned short int AGLOBAL; // no value in here!

#endif

Other module c file

#include globals.h

char SomeOtherFunc(void)
{
  AGLOBAL+=10; // ok cause its defined by globals.h
  // do more....
}
半枫 2024-11-22 17:27:55

首先我要说的是 extern 关键字适用于 C 变量(数据对象)和 C 函数。基本上 extern 关键字扩展了 C 变量和 C 函数的可见性。也许这就是它被命名为 extern 的原因。

将 extern 与 C 函数一起使用。默认情况下,C 函数的声明和定义前面带有“extern”。这意味着即使我们在 C 函数的声明/定义中不使用 extern,它仍然存在。
例如,当我们写作时。

int foo(int arg1, char arg2);

开头有一个 extern 被隐藏,编译器将其视为如下。

extern int foo(int arg1, char arg2);

C 函数的定义也是如此(C 函数的定义意味着编写函数体)。因此,每当我们定义一个 C 函数时,extern 就会出现在函数定义的开头。由于声明可以进行任意多次,而定义只能进行一次,因此我们可以注意到,函数的声明可以在多个 C/H 文件中或在单个 C/H 文件中多次添加。但我们注意到该函数的实际定义仅一次(即仅在一个文件中)。由于 extern 将可见性扩展到整个程序,因此只要函数的声明已知,就可以在整个程序的任何文件中的任何位置使用(调用)这些函数。 (通过知道函数的声明,C 编译器知道函数的定义存在,并继续编译程序)。
这就是关于 C 函数的 extern 的全部内容。

  1. 声明可以进行任意多次,但定义只能进行一次。

  2. “extern”关键字用于扩展变量/函数()的可见性。

  3. 因为默认情况下函数在整个程序中都是可见的。函数声明/定义中不需要使用 extern。它的使用是多余的。

  4. 当 extern 与变量一起使用时,它仅被声明而不被定义。

  5. 作为例外,当使用初始化声明 extern 变量时,它也被视为该变量的定义。

So let me start with saying that extern keyword applies to C variables (data objects) and C functions. Basically extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.

Use of extern with C functions. By default, the declaration and definition of a C function have “extern” prepended with them. It means even though we don’t use extern with the declaration/definition of C functions, it is present there.
For example, when we write.

int foo(int arg1, char arg2);

There’s an extern present in the beginning which is hidden and the compiler treats it as below.

extern int foo(int arg1, char arg2);

Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program).
So that’s all about extern with C functions.

  1. Declaration can be done any number of times but definition only once.

  2. “extern” keyword is used to extend the visibility of variables/functions().

  3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.

  4. When extern is used with a variable, it’s only declared not defined.

  5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.

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