[英]“Undefined symbols for architecture x86_64” trying to include function from another file
我试图在另一个程序event_rate.cpp中包含一个名为kinematicfactor的函数,该函数用kinematic_factor.cpp编写。
kinematic_factor.cpp:
#include "kinematic_factor.hpp"
double kinematicfactor (double md) {
double mt = 17.6924; // Mass of Fluorine [GeV/c^2]
double r = 4*mt*md/pow(mt+md, 2);
return r;
}
kinematic_factor.hpp:
#ifndef kinematic_factor_hpp
#define kinematic_factor_hpp
double kinematicfactor (double md);
#endif /* kinematic_factor_hpp */
event_rate:
#include "event_rate.hpp"
#include "kinematic_factor.hpp"
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << kinematic_factor(1.0) << endl;
}
据我所能检查的,这就是我应该做的,但是我得到一个错误:
Undefined symbols for architecture x86_64:
"kinematicfactor(double)", referenced from:
_main in event_rate-1d17f7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我究竟做错了什么?
编辑:我试过
#include "kinematic_factor.cpp"
而且有效。 但是我听说包含cpp文件(而不是标头文件)对学习者来说是一个坏习惯……我该怎么办?
您如何编译此程序,我能够正确编译相同的程序。 我怀疑这应该是链接问题。
您需要将源代码一起编译才能进行编译和链接。
g++ kinematic_factor.cpp event_rate.cpp -o event_rate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.