i386 架构的未定义符号:

发布于 2024-11-04 01:05:13 字数 698 浏览 0 评论 0原文

我最近搬到了 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 技术交流群。

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

发布评论

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

评论(3

记忆で 2024-11-11 01:05:13

看起来您有三个文件:

  • matrix.h,一个声明 Matrix 类的头文件;
  • matrix.cpp,实现 Matrix 方法的源文件;
  • main.cpp,一个定义 main() 并使用 Matrix 类的源文件。

为了生成包含所有符号的可执行文件,您需要编译两个 .cpp 文件并将它们链接在一起。

执行此操作的一个简单方法是在 g++clang++ 调用中指定它们。例如:

clang++ matrix.cpp main.cpp -o programName

或者,如果您更喜欢使用 g++ — Apple 已经有一段时间没有更新了,而且看起来在可预见的将来也不会更新:

g++ matrix.cpp main.cpp -o programName

It looks like you’ve got three files:

  • matrix.h, a header file that declares the Matrix class;
  • matrix.cpp, a source file that implements Matrix methods;
  • main.cpp, a source file that defines main() and uses the Matrix 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++ or clang++ invocation. For instance:

clang++ matrix.cpp main.cpp -o programName

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:

g++ matrix.cpp main.cpp -o programName
吝吻 2024-11-11 01:05:13

这里的情况并非如此,但可能碰巧您忘记将类名与 ::

例如:


好的格式:

foo.h

class Foo{
public:
    Foo();
    void say();
private:
    int x;
};

foo.cpp

Foo::Foo(){
    this->x = 1;
}

void Foo::say(){
    printf("I said!\n");
}

错误的格式

foo.h

class Foo{
public:
    Foo();
    void say();
private:
    int x;
}

foo.cpp

Foo::Foo(){
    this->x = 1;
}

//I always mistake here because I forget to put the class name with :: and the xcode don't show this error.
void say(){
    printf("I said!\n");
}

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

class Foo{
public:
    Foo();
    void say();
private:
    int x;
};

foo.cpp

Foo::Foo(){
    this->x = 1;
}

void Foo::say(){
    printf("I said!\n");
}

a bad format

foo.h

class Foo{
public:
    Foo();
    void say();
private:
    int x;
}

foo.cpp

Foo::Foo(){
    this->x = 1;
}

//I always mistake here because I forget to put the class name with :: and the xcode don't show this error.
void say(){
    printf("I said!\n");
}
世界和平 2024-11-11 01:05:13

您实际上在某处定义了 Box 构造函数吗? (如 Line.cpp)

Did you actually define the Box constructor somewhere? (like Line.cpp)

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