C++ 建筑x86_64的未定义符号(类似)

C++ Undefined symbols for architecture x86_64 (similar)

本文关键字:符号 类似 未定义 建筑 x86 C++      更新时间:2023-10-16

当我在编译代码时遇到困难时,我正在使用我的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

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