"Undefined symbols for architecture x86_64" 试图包含来自另一个文件的函数
"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 文件(而不是头文件)对学习者来说是一种不好的做法...我该怎么办?
你是怎么编译这个程序的,我能正确编译同样的程序。我怀疑这应该是 linking 问题。
需要编译源码一起编译和link.
g++ kinematic_factor.cpp event_rate.cpp -o event_rate
我正在尝试在另一个程序 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 文件(而不是头文件)对学习者来说是一种不好的做法...我该怎么办?
你是怎么编译这个程序的,我能正确编译同样的程序。我怀疑这应该是 linking 问题。
需要编译源码一起编译和link.
g++ kinematic_factor.cpp event_rate.cpp -o event_rate