当我想运行代码时,编译器说undefined reference to math::calc
我在StackOverflow上阅读了关于这个问题的问题和答案,但它并没有帮助我解决我的问题。
Comp.h
#include <utility>
namespace math {
typedef std::pair<double,double> Comp;
double getFirst(Comp a);
...
}
Comp.cpp
#include "comp.h"
namespace math {
double getFirst(Comp a) {
return a.first;
}
...
}
Comp
文件:每个函数返回Comp
或double
。我多次调用cal.cpp
文件中的函数
cal.h
#include "comp.h"
#include "helper.h"
namespace math {
Comp calc(const string& str);
...
}
cal.cpp
#include "eval.h"
namespace math {
Comp calc(const string& str) {
...
}
}
Cal
文件:一些函数返回comp
类型,而不仅仅是cal
函数。
helper.h
namespace math {
...
}
helper.cpp
#include "helper.h"
namespace math {
...
}
helper
文件只包含我从cal.cpp
文件调用的几个函数。每个函数都被调用了几次。
main.cpp
#include "calc.h"
int main() {
string str = " ";
pair<double,double> res = math::calc(str);
cout << res.first << " " << res.second << endl;
return 0;
}
在整个项目中,我没有使用任何类。
我包含了所有我调用的文件,除了c++原始文件。
我在每个文件中使用std命名空间,但我没有在这里写。
我完全不知道我的代码出了什么问题。
如果我还在main.cpp
中包含cal.cpp
,代码编辑器将undefined reference
表示我从helper.h
调用的每个文件。我不想在刚才提到的main
文件中包含cal.cpp
你必须编译所有项目的*.cpp文件,然后正确地链接它们(编译的结果)-链接过程取决于你正在使用的环境/IDE,只需查找它。如果你没有正确地链接它,最终的可执行文件将没有所有需要的函数定义,因此得到未定义的引用。