我正在尝试在另一个程序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文件(而不是头文件)对于学习者来说是一种不好的做法......我该怎么办?
答案 0 :(得分:0)
你是如何编译这个程序的,我能够正确地编译相同的程序。我怀疑它应该是链接问题。
您需要一起编译源代码以进行编译和链接。
g++ kinematic_factor.cpp event_rate.cpp -o event_rate