macos 架构 i386 的未定义符号:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5818270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:00:27  来源:igfitidea点击:

Undefined symbols for architecture i386:

c++macoslinkerg++terminal

提问by maccard

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?

我最近搬到了 mac,并且正在努力使用命令行编译器。我正在使用 g++ 进行编译,这可以很好地构建单个源文件。如果我尝试添加自定义头文件,当我尝试使用 g++ 进行编译时,我会得到架构 i386 的未定义符号。然而,这些程序在 xCode 中编译得很好。我错过了一些明显的东西吗?

tried using g++ -m32 main.cpp... didn't know what else to try.

尝试使用 g++ -m32 main.cpp ... 不知道还能尝试什么。



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

我的错误是架构 x86_64 的未定义符号:“Matrix::Matrix()”,引用自:ccBWK2wB.old 中的 _main:找不到架构 x86_64 collect2 的符号:ld 返回 1 个退出状态

my main code has

我的主要代码有

#include "matrix.h"
int main(){
    Matrix m;

    return 0;
} 

along with the usual

与平常一起

回答by

It looks like you've got three files:

看起来你有三个文件:

  • matrix.h, a header file that declares the Matrixclass;
  • matrix.cpp, a source file that implements Matrixmethods;
  • main.cpp, a source file that defines main()and uses the Matrixclass.
  • matrix.h,一个声明Matrix类的头文件;
  • matrix.cpp,一个实现Matrix方法的源文件;
  • main.cpp,一个定义main()和使用Matrix类的源文件。

In order to produce an executable with all symbols, you need to compile both .cpp files and link them together.

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

An easy way to do this is to specify them both in your g++or clang++invocation. For instance:

一种简单的方法是在您的g++clang++调用中指定它们。例如:

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++- Apple 已经有一段时间没有更新了,而且在可预见的未来看起来它们不会更新:

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

回答by ademar111190

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.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

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");
}

回答by Ken Aspeslagh

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

你真的在某处定义了 Box 构造函数吗?(如 Line.cpp)