试验 C 中的全局变量和函数

发布于 2024-11-18 18:36:04 字数 897 浏览 7 评论 0原文

我试图了解全局变量和函数在 C 中的工作原理。我的程序可以使用 gcc 编译并正常工作,但不能使用 g++ 编译。我有以下文件:

globals.h:

int i;
void fun();

globals.c:

#include "stdlib.h"
#include "stdio.h"

void fun()
{
  printf("global function\n");
}

ma​​in.c:

#include "stdlib.h"
#include "stdio.h"
#include "globals.h"

void myfun();

int main()
{

  i=1;

  myfun();

  return 0;
}

最后,myfun.c:

#include "stdlib.h"
#include "stdio.h"
#include "globals.h"

void myfun()
{
  fun();
}

使用 g++ 编译时出现以下错误:

/tmp/ccoZxBg9.o:(.bss+0x0): multiple definition of `i'
/tmp/ccz8cPTA.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

有什么想法吗?我更喜欢用 g++ 编译。

I'm trying to understand how global variables and functions work in C. My program compiles and works fine with gcc, but does not compile with g++. I have the following files:

globals.h:

int i;
void fun();

globals.c:

#include "stdlib.h"
#include "stdio.h"

void fun()
{
  printf("global function\n");
}

main.c:

#include "stdlib.h"
#include "stdio.h"
#include "globals.h"

void myfun();

int main()
{

  i=1;

  myfun();

  return 0;
}

And finally, myfun.c:

#include "stdlib.h"
#include "stdio.h"
#include "globals.h"

void myfun()
{
  fun();
}

I get the following error when compiling with g++:

/tmp/ccoZxBg9.o:(.bss+0x0): multiple definition of `i'
/tmp/ccz8cPTA.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

Any ideas why? I would prefer to compile with g++.

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

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

发布评论

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

评论(4

千年*琉璃梦 2024-11-25 18:36:04

您包含 globals.h 的每个文件都将定义“int i”。

相反,输入“extern int i;”到头文件中,然后放入“int i = 1;”的实际定义在 globals.c 中。

在 globals.h 周围放置标头保护也是明智的。

编辑:回答你的问题是因为#include的工作方式有点像剪切和粘贴。它将包含文件的内容粘贴到您从中调用包含的 c 文件中。当您包含 main.c 和 myfun.c 中的“globals.h”时,您在这两个文件中定义了 int i = 1。该值是全局的,被放入可链接值表中。如果您两次使用相同的变量名,则链接器将无法判断它需要哪一个,并且您会收到所看到的错误。相反,通过在头文件的前面添加 extern,您可以告诉每个文件“int i”是在其他地方定义的。显然,您需要在其他地方定义它(并且仅在一个地方),因此在 globals.c 中定义它是非常有意义的。

希望有帮助:)

Every file you include globals.h from will define "int i".

Instead, put "extern int i;" into the header file and then put the actual definition of "int i = 1;" in globals.c.

Putting header guards around globals.h would be sensible too.

Edit: In answer to your question its because a #include works kind of like a cut and paste. It pastes the contents of the included file into the c file that you are calling include from. As you include "globals.h" from main.c and myfun.c you define int i = 1 in both files. This value, being global, gets put into the table of linkable values. If you have the same variable name twice then the linker won't be able to tell which one it needs and you get the error you are seeing. Instead by adding extern on the front in the header file you are telling each file that "int i" is defined somewhere else. Obviously, you need to define it somewhere else (and ONLY in one place) so defining it in globals.c makes perfect sense.

Hope that helps :)

椵侞 2024-11-25 18:36:04

我会在你的全局文件中添加一个包含保护

#ifndef GLOBALS_H
#define GLOBALS_H

int i;
void fun();

#endif

编辑:将你的全局变量更改为这样(使用 extern 作为其他答案描述的)

globals.h

extern  int i;
extern  void fun();

globals.c

#include "stdlib.h"
#include "stdio.h"
int i;
void fun()
{
  printf("global function\n");
}

我用它编译它

g++ globals.c main.c myfun.c

并且运行正常

I would add an include guard in your globals file

#ifndef GLOBALS_H
#define GLOBALS_H

int i;
void fun();

#endif

Edit: Change your globals to be like this (using extern as the other answer describes)

globals.h

extern  int i;
extern  void fun();

globals.c

#include "stdlib.h"
#include "stdio.h"
int i;
void fun()
{
  printf("global function\n");
}

I compiled it with

g++ globals.c main.c myfun.c

and it ran ok

无声情话 2024-11-25 18:36:04

这里有几处错误;强烈推荐的其他几件事:

globals.h:


#ifndef GLOBALS_H
#define GLOBALS_H

extern int my_global;

#ifdef __cplusplus
extern "C" {
#endif 
void fun();
#ifdef __cplusplus
}
#endif 

#endif
/* GLOBALS_H */

globals.c:


#include <stdlib.h>
#include <stdio.h>
#include "globals.h"

int my_global;

void fun()
{
  printf("global function: %d\n", my_global);
}

main.c:


#include <stdlib.h>
#include <stdio.h>
#include "globals.h"

void myfun();

int main()
{

  my_global=1;

  myfun();

  return 0;
}

void myfun()
{
  fun();
}
  1. 您应该在标头中声明“extern int myvar”,并实际在一个且唯一的一个 .c 文件中分配“int myvar”。< /p>

  2. 您应该在每个使用“myvar”的文件中包含“globals.h” - 包括分配它的文件。

  3. 特别是如果您计划混合 C 和 C++ 模块,则应使用“extern "C"”来区分非 C++ 函数。

  4. 系统头应该是“#include”;您自己的标头应使用引号 (#include "myheader.h")。

  5. 对于严格的局部变量(如循环索引)来说,像“i”这样的短变量名称可能没问题,但是当您无法避免使用全局变量时,您应该始终使用更长的描述性名称。

  6. 我为 my_global 添加了一个“printf”。

'希望有帮助!

Several things wrong here; several other things highly recommended:

globals.h:


#ifndef GLOBALS_H
#define GLOBALS_H

extern int my_global;

#ifdef __cplusplus
extern "C" {
#endif 
void fun();
#ifdef __cplusplus
}
#endif 

#endif
/* GLOBALS_H */

globals.c:


#include <stdlib.h>
#include <stdio.h>
#include "globals.h"

int my_global;

void fun()
{
  printf("global function: %d\n", my_global);
}

main.c:


#include <stdlib.h>
#include <stdio.h>
#include "globals.h"

void myfun();

int main()
{

  my_global=1;

  myfun();

  return 0;
}

void myfun()
{
  fun();
}
  1. You should declare "extern int myvar" in your header, and actually allocate "int myvar" in one and only one .c file.

  2. You should include "globals.h" in every file that uses "myvar" - including the file where it's allocated.

  3. Especially if you're planning on mixing C and C++ modules, you should use 'extern "C"' to distinguish non-C++ functions.

  4. System headers should be "#include <some_header.h>"; your own headers should use quotes (#include "myheader.h") instead.

  5. Short variable names like "i" might be OK for a strictly local variable (like a loop index), but you should always use longer, descriptive names whenever you can't avoid using a global variable.

  6. I added a "printf" for my_global.

'Hope that helps!

茶底世界 2024-11-25 18:36:04

我在将一些旧的 C 代码移植到 C++ 时遇到了这个问题。问题是它是一个连接到数据库的项目,我想将数据库移植到 c++,但不是其余的。数据库引入了一些无法移植的 C 依赖项,因此我需要与数据库和其他项目重叠的 C 代码在 g++ 和 gcc 中进行编译...

这个问题的解决方案是定义所有变量作为.h 文件中的 extern。那么当您在 gcc 或 g++ 中编译时,它将报告 .c 文件中缺少符号。因此,请编辑错误消息中的 .c 文件,并将声明插入所有需要变量的 .c 文件中。注意:您可能必须在多个 .c 文件中声明它,这就是让我困惑的原因,也是我多年来一直困扰这个问题的原因。

无论如何,这解决了我的问题,并且代码现在可以在 gcc 和 g++ 下干净地编译。

I had this problem when porting some old C code to C++. The problem was it was a project that was connected to a database, and i wanted to port the database to c++ but not the rest. The database pulled in some C dependencies that couldn't be ported, so i needed the C code that overlapped both the database and the other project to compile in g++ as well as gcc...

The solution to this problem is to define all variables as extern in the .h file. then when you compile in either gcc or g++ it will report symbols missing in the .c files. So edit the .c files in the error messages and insert the declaration into all the .c files that need the variables. Note: you may have to declare it in multiple .c files, which is what threw me and why I was stuck on this problem for ages.

Anyway this solved my problem and the code compiles cleanly under both gcc and g++ now.

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