体系结构 x86_64 的 C++ undefined symbol (类似)

标签 c++

<分区>

当我在编译代码时遇到困难时,我正在使用我的 mbp 在线练习随机问题。 我确实在网上搜索了类似的问题,但这是我想出的,但仍然无法编译。

我有错误信息

 c++     test.cpp   -o test 

 Undefined symbols for architecture x86_64: 
 "matrix::mSubtraction(float*, float*)", referenced from:
       _main in test-fb9462.o   "matrix::mMultiplication(float*, float*)", referenced from:
       _main in test-fb9462.o   "matrix::mShow(float*)", referenced from:
       _main in test-fb9462.o   "matrix::mAddition(float*, float*)", referenced from:
       _main in test-fb9462.o   "matrix::mDivision(float*, float*)", referenced from:
       _main in test-fb9462.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

测试.cpp

#include <iostream>
#include "matrix.h"

using namespace std;

int main() 
{
    float matrix_a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    float matrix_b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    matrix m;

    m.Cols = 3;
    m.Rows = 3;

    cout <<  "Matrix A :\n";
    m.mShow((float *)matrix_a);
    cout << "Matrix B :\n";
    m.mShow((float *)matrix_b);

    cout << "Matrix Addition :\n";
    m.mAddition((float *)matrix_a,(float *)matrix_b);
    m.mShow((float *)m.ans);

    cout << "Matrix Subtraction :\n";
    m.mSubtraction((float *)matrix_a,(float *)matrix_b);
    m.mShow((float *)m.ans);

    cout << "Matrix Multiplication :\n";
    m.mMultiplication((float *)matrix_a,(float *)matrix_b);
    m.mShow((float *)m.ans);

    cout << "Matrix Division :\n";
    m.mDivision((float *)matrix_a,(float *)matrix_b);
    m.mShow((float *)m.ans);

    return 0;
}

矩阵.cpp

#include <iostream>

void matrix::mShow(float *A) {
    for(int i = 0; i < matrix::Cols; i++) {
        for (int j = 0; j < matrix::Rows; j++) {
            printf("%f  \n",*((A+i*3)+j));
        }
    }
}

void matrix::mAddition(float *A, float *B) {
    for(int i = 0; i < matrix::Cols; i++) {
        for (int j = 0; j < matrix::Rows; j++) {
            matrix::ans[i][j]= *((A+i*3) + j) + *((B+i*3) + j);
        }
    }
}

void matrix::mSubtraction(float *A, float *B) {
    cout << "After matrix addition" << endl;
    for(int i = 0; i < matrix::Cols; i++) {
        for (int j = 0; j < matrix::Rows; j++) {
            matrix::ans[i][j]= *((A+i*3) + j) - *((B+i*3) + j);
        }
    }
}

void matrix::mMultiplication(float *A, float *B) {
    cout << "After matrix addition" << endl;
    for(int i = 0; i < matrix::Cols; i++) {
        for (int j = 0; j < matrix::Rows; j++) {
            matrix::ans[i][j]= *((A+i*3) + j) * *((B+i*3) + j);
        }
    }
}

void matrix::mDivision(float *A, float *B) {
    cout << "After matrix addition" << endl;
    for(int i = 0; i < matrix::Cols; i++) {
        for (int j = 0; j < matrix::Rows; j++) {
            matrix::ans[i][j]= *((A+i*3) + j) / *((B+i*3) + j);
        }
    }
}

矩阵.h

#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <iostream>

using namespace std;

class matrix {

private:

public:

    int Cols;
    int Rows;
    float ans[3][3];
    void mShow(float *);
    void mAddition(float *,float *);
    void mSubtraction(float *,float *);
    void mMultiplication(float *,float *);
    void mDivision(float *,float *a);

};

#endif

谢谢你们好心地帮助我。

最佳答案

c++ test.cpp -o test

这行不通,因为您还需要链接 matrix.cpp 的机器/目标代码。尝试:

c++ -c matrix.cpp
c++ -c test.cpp
c++ -o test matrix.o test.o

或者,为您的编译器提供适当的选项。

关于体系结构 x86_64 的 C++ undefined symbol (类似),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50267111/

相关文章:

c++ - 声明结构表时出现 std::bad_alloc 错误

c++ -++operator 在这个 void uart_puts(unsigned char * t) { while(*t) uart_putch(*t++) 中做了什么?

c++ - noexcept operators ->() 和 operator*() 用于迭代器?

Qt 中默认控件的 C++ 自定义事件

c++ - 你能解释一下这段代码吗?

c++ - 使用 Freetype 和 OpenGL 模糊文本

c++ - 从 C++ 插件调用 QML 中的 JS 函数

c++ - 在两个容器 C++ 中搜索匹配项目

c++ - C++ 中的性能比较(普通函数调用与 for_each+mem_fun 与 lambda 表达式)

c++ - 函数返回类型会影响重载的选择吗?