i386 架构的未定义符号:
我最近搬到了 Mac,并且在使用命令行编译器方面遇到了困难。我使用 g++ 进行编译,这可以很好地构建单个源文件。如果我尝试添加自定义头文件,当我尝试使用 g++ 进行编译时,我会得到 i386 架构的未定义符号。然而,这些程序在 xCode 中编译得很好。我错过了一些明显的东西吗?
尝试使用 g++ -m32 main.cpp...不知道还能尝试什么。
好吧,旧代码已编译...已将其范围缩小到我的构造函数。
class Matrix{
public:
int a;
int deter;
Matrix();
int det();
};
#include "matrix.h"
Matrix::Matrix(){
a = 0;
deter = 0;
}
int Matrix::det(){
return 0;
}
我的错误是 体系结构 x86_64 的未定义符号: “Matrix::Matrix()”,引用自: _main 在 ccBWK2wB.o 中 ld:未找到架构 x86_64 的符号 Collect2: ld 返回 1 退出状态
我的主代码
#include "matrix.h"
int main(){
Matrix m;
return 0;
}
与通常的一样
I've recently moved over to a mac, and am struggling using the command line compilers. I'm using g++ to compile, and this builds a single source file fine. if I try to add a custom header file, when I try to compile using g++ I get undefined symbols for architecture i386. The programs compile fine in xCode however. Am I missing something obvious?
tried using g++ -m32 main.cpp... didn't know what else to try.
Okay, The old code compiled... Have narrowed it down to my constructors.
class Matrix{
public:
int a;
int deter;
Matrix();
int det();
};
#include "matrix.h"
Matrix::Matrix(){
a = 0;
deter = 0;
}
int Matrix::det(){
return 0;
}
my error is
Undefined symbols for architecture x86_64:
"Matrix::Matrix()", referenced from:
_main in ccBWK2wB.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
my main code has
#include "matrix.h"
int main(){
Matrix m;
return 0;
}
along with the usual
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您有三个文件:
main()
并使用Matrix
类的源文件。为了生成包含所有符号的可执行文件,您需要编译两个 .cpp 文件并将它们链接在一起。
执行此操作的一个简单方法是在
g++
或clang++
调用中指定它们。例如:或者,如果您更喜欢使用
g++
— Apple 已经有一段时间没有更新了,而且看起来在可预见的将来也不会更新:It looks like you’ve got three files:
Matrix
class;Matrix
methods;main()
and uses theMatrix
class.In order to produce an executable with all symbols, you need to compile both .cpp files and link them together.
An easy way to do this is to specify them both in your
g++
orclang++
invocation. For instance:or, if you prefer to use
g++
— which Apple haven’t updated in a while, and it looks like they won’t in the foreseeable future:这里的情况并非如此,但可能碰巧您忘记将类名与 ::
例如:
好的格式:
foo.h
foo.cpp
错误的格式
foo.h
foo.cpp
is not the case here, but it may happen to be the you forget to put the class name with ::
for example:
a good format:
foo.h
foo.cpp
a bad format
foo.h
foo.cpp
您实际上在某处定义了 Box 构造函数吗? (如 Line.cpp)
Did you actually define the Box constructor somewhere? (like Line.cpp)