使用 extern 时未定义的引用

发布于 2024-09-17 20:31:45 字数 512 浏览 1 评论 0原文

我有以下设置(希望这不是一个简单的例子):

Ah

typedef std::map<unsigned int, float> MyClass;
extern MyClass inst;

A.cpp

MyClass inst;

Bh

#include <A.h>
void foo();

B.cpp

#include <B.h>
void foo {
    inst.myClassFunc();
}

,当我在 B.cpp 中使用 inst 时,我得到了对 inst 的未定义引用。

知道如何解决这个问题吗?

I have the following setup (hopefully this is not too bare an example):

A.h

typedef std::map<unsigned int, float> MyClass;
extern MyClass inst;

A.cpp

MyClass inst;

B.h

#include <A.h>
void foo();

B.cpp

#include <B.h>
void foo {
    inst.myClassFunc();
}

Now, when I use inst in B.cpp I get undefined reference to inst.

Any idea on how to fix this?

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

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

发布评论

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

评论(4

微暖i 2024-09-24 20:31:45

我知道这个问题很旧,但它仍然可能对某人有帮助。

全局变量(此处:MyClass inst不应该是 extern 对于定义它的编译单元(此处:A.cpp

实现此目的的一种方法是:

  • 单独的标头声明全局变量(假设global.h< /code>) 并使用这些将这个标头包含在 *cpp 中。
  • 删除定义它们的编译单元的extern关键字(例如使用#ifdef):

global.h看起来像:

#ifdef A_H_
  #define EXTERN
#else
  #define EXTERN extern
#endif

EXTERN MyClass inst;

而Ah看起来像:

#ifndef A_H_
#define A_H_

// your header content (without globals)

#endif /* A_H_ */

和 A.cpp:

#include "A.h"
#include "global.h" // after A.h inclusion, we need A_H_ definition

希望有帮助!

I know this question is old, but it still might be helpful for someone.

The global variable (here: MyClass inst) should not be extern for the compilation unit which define it (here: A.cpp)

One way to achieve this:

  • declare your global variable in a separate header (let's say global.h) and include this header in the *cpp using these.
  • remove the extern keyword for the compilation unit which define them (e.g. with #ifdef) :

global.h looks like:

#ifdef A_H_
  #define EXTERN
#else
  #define EXTERN extern
#endif

EXTERN MyClass inst;

while A.h looks like:

#ifndef A_H_
#define A_H_

// your header content (without globals)

#endif /* A_H_ */

and A.cpp:

#include "A.h"
#include "global.h" // after A.h inclusion, we need A_H_ definition

Hope it helps!

手心的海 2024-09-24 20:31:45

这只是一个简单的例子,无法说明发生了什么。然而,基于上述情况,当编译器遇到失败行时,完全有可能不知道 MyClass 中的实际内容,因此无法解析 MyClassFunc

我们需要查看 MyClass 的定义并知道它在哪里可以确定答案。

This is too bare an example to work out what's going on. However, based on the above it's entirely possible that when it hits the failing line the compiler has no knowledge of what's actually in MyClass and therefore cannot resolve MyClassFunc.

We would need to see the definition of MyClass and know where it is to answer for sure.

再浓的妆也掩不了殇 2024-09-24 20:31:45

您必须将上述文件 A.cpp 编译为

g++ -c A.cpp
g++ -c B.cpp

然后在创建可执行文件时应编写如下命令:

g++ A.o B.o

You have to compile the above mentioned file A.cpp as

g++ -c A.cpp
g++ -c B.cpp

and then while creating the executable you should write the command as follows:

g++ A.o B.o
静水深流 2024-09-24 20:31:45

从您发布的基本示例代码来看,我想说您忘记了 B.cpp 文件中的 #include

From the basic example code you posted I'd say you've forgotten to #include <B.h> in your B.cpp file.

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