MacBook Pro上C++编译报错:Undefined symbols for architecture x86_64
Hey there, let's tackle this linker error and clean up some code issues so your C++ matrix program works properly!
1. Fix the Immediate Linker Error
The undefined symbols error you're seeing means the linker can't find the implementations of your matrix class methods (like mSubtraction, mMultiplication, etc.). This happens because you only compiled test.cpp—you forgot to include matrix.cpp in your compile command.
Run this command instead:
c++ test.cpp matrix.cpp -o test
This tells the compiler to compile both source files and link them together, so all the class method implementations are available to the linker.
If you're using a Makefile, update your target to include both .cpp files:
test: test.cpp matrix.cpp c++ test.cpp matrix.cpp -o test
2. Fix Code Issues in Your Implementation
Even after fixing the linker error, your code has a few bugs that will cause unexpected behavior. Let's fix those:
a. Mismatched Function Declaration in matrix.h
In matrix.h, the mDivision function is declared as:
void mDivision(float *,float *a);
But in matrix.cpp, you use float *B as the second parameter. While this doesn't break linking, it's inconsistent. Update the header to match the implementation:
void mDivision(float *, float *); // OR void mDivision(float *, float *B);
b. Incorrect Access to Non-Static Class Members
In matrix.cpp, you're using matrix::Cols and matrix::Rows like static variables, but they're instance members (each matrix object has its own Cols and Rows). Replace those with this->Cols or just Cols (since member functions can directly access instance variables):
// Instead of matrix::Cols, use this->Cols for(int i = 0; i < this->Rows; i++) { for (int j = 0; j < this->Cols; j++) { printf("%f ", *(A + i * this->Cols + j)); } printf("\n"); }
c. Hardcoded Matrix Size
You're using 3 in calculations like (A+i*3)—this ties your code to 3x3 matrices. Replace the hardcoded 3 with this->Cols so your code works for any matrix size you set via Cols and Rows.
d. Wrong Debug Messages
Your mSubtraction, mMultiplication, and mDivision functions all print "After matrix addition"—that's a copy-paste error. Update them to match the operation:
// In mSubtraction cout << "After matrix subtraction" << endl; // In mMultiplication cout << "After matrix multiplication" << endl; // In mDivision cout << "After matrix division" << endl;
e. (Optional) Fix Matrix Multiplication Logic
Right now, your mMultiplication does element-wise multiplication (multiplying corresponding elements). If you intended to implement actual matrix multiplication (row-by-column dot product), you'll need to rewrite that function. Here's a quick example:
void matrix::mMultiplication(float *A, float *B) { cout << "After matrix multiplication" << endl; // Initialize ans to 0 first for(int i = 0; i < this->Rows; i++) { for (int j = 0; j < this->Cols; j++) { this->ans[i][j] = 0; for(int k = 0; k < this->Cols; k++) { this->ans[i][j] += *(A + i * this->Cols + k) * *(B + k * this->Cols + j); } } } }
3. Test the Fixed Code
After applying these changes, recompile with the command I mentioned earlier, and your program should compile and run correctly.
内容的提问来源于stack exchange,提问作者Johnny Yeng

