g++非常简单的示例中未定义的引用
请帮助解决以下有关 C++ 和 g++ 编译和链接的菜鸟问题。本质上,我在两个不同的文件中有两个类,并且可以编译它们,但是当我尝试链接时,一个类看不到另一个类的方法,即使我链接了两个类。在这种情况下,目标文件的顺序没有帮助。
该问题似乎与带有参数的非默认构造函数有关。
我在以下简单代码中提炼并重现了该问题:
文件:a.cpp:
#include <iostream>
class A
{
public:
int my_int;
A(int i) {
my_int = i;
std::cout << "A";
}
};
文件:a.hpp:
#ifndef __A_H_
#define __A_H_
class A
{
public:
A(int i);
};
#endif
文件 b.cpp: >
#include <iostream>
using namespace std;
#include <a.hpp>
class B
{
public:
int my_int;
B(int i) {
my_int = i;
A a(i);
cout << "B\n";
}
};
int main(int argc, char* argv[])
{
B b(5);
cout << "hello world: ";
cout.flush();
return 0;
}
我用来构建的命令:
g++ -c -I. a.cpp
g++ -c -I. b.cpp
g++ -o c_test a.o b.o
或者,我已经尝试过以下每一个:
g++ -o c_test b.o a.o
g++ -I. -o c_test a.cpp b.cpp
g++ -I. -o c_test b.cpp a.cpp
我在上述任何链接场景中遇到的错误:
b.o: In function `B::B(int)':
b.cpp:(.text._ZN1BC1Ei[B::B(int)]+0x1c): undefined reference to `A::A(int)'
collect2: ld returned 1 exit status
感谢提前获得任何见解。
(抱歉,如果这是重新发布的内容 - 我以为我发布了它但没有看到它......)
Please help with the following noob question about C++ and g++ compilation and linking. Essentially I have 2 classes in 2 different files, and can compile them but when I attempt to link, one class can't see the methods of the other, even though I am linking both. Order of object files does not help in this case.
The problem seems related to a non-default constructor that takes a parameter.
I have distilled and reproduced the problem in the following simple code:
File: a.cpp:
#include <iostream>
class A
{
public:
int my_int;
A(int i) {
my_int = i;
std::cout << "A";
}
};
File: a.hpp:
#ifndef __A_H_
#define __A_H_
class A
{
public:
A(int i);
};
#endif
File b.cpp:
#include <iostream>
using namespace std;
#include <a.hpp>
class B
{
public:
int my_int;
B(int i) {
my_int = i;
A a(i);
cout << "B\n";
}
};
int main(int argc, char* argv[])
{
B b(5);
cout << "hello world: ";
cout.flush();
return 0;
}
Commands I use to build:
g++ -c -I. a.cpp
g++ -c -I. b.cpp
g++ -o c_test a.o b.o
Alternately, I've tried each of these:
g++ -o c_test b.o a.o
g++ -I. -o c_test a.cpp b.cpp
g++ -I. -o c_test b.cpp a.cpp
Error I get in any of above link scenarios:
b.o: In function `B::B(int)':
b.cpp:(.text._ZN1BC1Ei[B::B(int)]+0x1c): undefined reference to `A::A(int)'
collect2: ld returned 1 exit status
Thanks in advance for any insight.
(sorry if this is a re-post -- I thought I posted it and don't see it...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
事实并非如此。从技术上讲,您遇到的是 ODR 违规,这大致意味着
a.cpp
和b.cpp
中的A
必须是同样的事情。事实并非如此。此外,构造函数在 a.cpp 中隐式
内联
,因此不需要发出其代码。将
a.cpp
更改为将修复该错误。
It doesn't work that way. What you've come across is technically an ODR violation, which roughly means that
A
in botha.cpp
andb.cpp
must be the same thing. It isn't.Moreover, the constructor is implicitly
inline
in a.cpp and therefore its code needn't be emitted.Changing
a.cpp
towill fix the error.
您
a.cpp
违反了单一定义规则并完全重新定义了A
。您只想在源文件中定义该函数:此外,您可能希望将函数标记为显式,以避免
int
在各种不需要的情况下被视为A
上下文。You
a.cpp
is violating the one definition rule and redefiningA
entirely. You just want to define the function in your source file:Also you may want to mark the function explicit to avoid
int
s being treated asA
's in a variety of unwanted contexts.在
a.cpp
中,您应该#include "a.hpp"
,然后将构造函数简单地定义为A::A(int i) { ... }
。通过在class
主体内使用构造函数代码编写class A
的完整定义,您可以将构造函数隐式定义为内联函数,这就是为什么目标文件中没有它的定义。In
a.cpp
, you should#include "a.hpp"
and then define the constructor simply asA::A(int i) { ... }
. By writing a whole definition ofclass A
with the constructor code within theclass
body, you're implicitly defining the constructor as an inline function, which is why there's no definition for it in the object file.您有两个不同的类(一个包含
myint
,一个不包含),都称为class A
。你不能那样做。将a.cpp
更改为:将
a.hpp
更改为:谢谢,按照你的方式,如果有人这样做,编译器会做什么:
怎么可能知道
类 A
除了构造函数之外还有其他成员吗?它怎么知道尺寸呢?You have two different classes (one containing
myint
, one not) both calledclass A
. You can't do that. Changea.cpp
to:And change
a.hpp
to:Thank about it, the way you have it, what would the compiler do if someone did this:
How could it know that
class A
has a member other than the constructor? How could it know the size?您违反了单一定义规则,因为您的程序中有两个不同的单独
A
类。 A.cpp 文件的简单通用实现应如下所示:“ah”包含类型的正确定义:
然后在 B 的实现中,您可能的意思是:
You are breaking the One Definition Rule, as you have two distinct separate
A
classes in your program. The simple common implementation of your A.cpp file should look like:With "a.h" containing the proper definition of the type:
And then in the implementation of B, you probably meant:
正确的做法是:
a.hpp
a.cpp
b.cpp - 保持不变
The right thing to do is:
a.hpp
a.cpp
b.cpp - remains the same