架构i386的未定义符号: [英] Undefined symbols for architecture i386:

查看:114
本文介绍了架构i386的未定义符号:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近移动到一个mac,并努力使用命令行编译器。我使用g ++来编译,这将构建一个单一的源文件罚款。如果我尝试添加一个自定义头文件,当我尝试编译使用g ++我得到未定义的符号为架构i386。然而,程序在xCode中编译得很好。我缺少一些明显的东西?



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






好吧,旧代码编译...已经缩小到我的构造函数。

  class Matrix {
public:
int a;
int deter;

Matrix();
int det();
};

#includematrix.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 h
int main(){
Matrix m;

return 0;
}

h2_lin>解决方案

看起来您有三个文件:




  • matrix.h,a声明 Matrix 类;

  • matrix.cpp,一个实现 Matrix 方法;

  • main.cpp是一个定义 main()并使用 Matrix 类。



为了生成一个带有所有符号的可执行文件, .cpp文件并将它们链接在一起。



一个简单的方法是在 g ++ clang ++ 调用。例如:

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

或者,如果你喜欢使用 g ++ - 苹果有一段时间没有更新,看起来像在可预见的未来不会:

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


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

解决方案

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

这篇关于架构i386的未定义符号:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆