[英]“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.