I got compile error as following while compiling a C++ program on Mac 10.11.4 using g++ command g++ -std=c++11 main.cpp -o test
.
But the Xcode can successfully build and run. I couldn't find out the error and I don't know whether the Xcode built it correctly. Could somebody help me out?
Undefined symbols for architecture x86_64:
"KM::execute_bfs()", referenced from:
_main in main-34d254.o
"KM::init(std::__1::vector<std::__1::vector<float, std::__1::allocator<float> >, std::__1::allocator<std::__1::vector<float, std::__1::allocator<float> > > >*, long)", referenced from:
_main in main-34d254.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
There are at all three files. "main.cpp", "km.cpp" and "km.h".
I put the class function in "km.cpp" and the definition of class in "km.h".
In "main.cpp", code is like:
long nv;
vector<vector<float>> weight;
int readin() {
...
for (int x = 0; x < n; x++) {
vector<float> tp;
tp.resize(nv, 0.0);
weight.push_back(tp);
}
}
int main() {
KM km;
km.init(&weight, nv);
km.execute_bfs();
}
In "km.cpp", I define all the member functions as:
long KM::init(vector< vector<float> > *input_weight, long input_max_v_num) { ... }
bool KM::bfs(long x) {...}
long KM::execute_bfs() {
...
bfs(x);
...
}
And in "km.h", my code is like:
class KM {
public:
vector< vector<float> > *weight;
long max_v_num;
float lx[MAX_NUM], ly[MAX_NUM];
long init(vector< vector<float> > *input_weight, long input_max_v_num);
bool bfs(long x);
long execute_bfs();
};
I ignore some details and display what I think may matter in the previous codes.
The error happens while compiling main.cpp on Mac 10.11.4 using g++ command g++ -std=c++11 main.cpp -o test
but works well using Xcode.
The error shows that I haven't defined the KM::execute_bfs() and KM::init(), but I have actually done this, as the previous code shows. Is there anybody who could help?