我在OSX優勝美地,我有三個C++文件,我試圖編譯。編譯三個C++文件。鏈接錯誤
BinModel01.cpp
#include <iostream>
#include <cmath>
using namespace std;
double RiskNeutProb(double U, double D, double R)
{
return (R-D)/(U-D);
}
double S(double S0, double U, double D, int n, int i)
{
return S0 * pow(1+U, i) * pow(1+D, n-i);
}
int GetInputData(double& S0, double& U, double& D, double& R)
{
// Entering data
cout << "Enter S0: "; cin >> S0;
cout << "Enter U: "; cin >> U;
cout << "Enter D: "; cin >> D;
cout << "Enter R: "; cin >> R;
cout << endl;
// making sure that 0 < S0, -1 < D < U, -1 < R}
if (S0 <= 0.0 || U <= -1.0 || D <= -1.0 || U <= D || R <= -1.0)
{
cout << "Illegal data ranges" << endl;
cout << "Terminating program" << endl;
return 1;
}
// Checking for arbitrage
if (R >= U || R <= D)
{
cout << "Arbitrage esists" << endl;
cout << "Terminating program" << endl;
return 1;
}
cout << "Input data checked" << endl;
cout << "there is no arbitrage" << endl << endl;
return 0;
}
Main04.cpp
#include "BinModel01.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double S0, U, D, R;
if (GetInputData (S0, U, D, R)==1) return 1;
// Compute risk-newutral probability
cout << "q = " << RiskNeutProb(U, D, R) << endl;
// Compute stock price at node n = 3, i =2
int n = 3; int i = 2;
cout << "n = " << n << endl;
cout << "i = " << i << endl;
cout << "S(n, i) = " << S(S0, U, D, n, i) << endl;
return 0;
}
和BinModel.h
#ifndef BinModel01_h
#define BinModel01_h
// Computing risk-neutral probability
double RiskNeutProb(double U, double D, double R);
// computing the stock price at node n, i
double S(double S0, double U, double D, int n, int i);
// Inputting, displaying and checking model data
int GetInputData(double& S0, double& U, double& D, double& R);
#endif
當我去到終端我開始用G ++編譯器,我得到一個錯誤:
g++ BinModel01.cpp
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
於是我試圖編譯不同的文件
g++ Main04.cpp
Undefined symbols for architecture x86_64:
"GetInputData(double&, double&, double&, double&)", referenced from:
_main in Main04-af9499.o
"RiskNeutProb(double, double, double)", referenced from:
_main in Main04-af9499.o
"S(double, double, double, int, int)", referenced from:
_main in Main04-af9499.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
任何想法,我做錯了什麼嗎?
謝謝。
在同一編譯命令中指定所有.cpp文件。或者,或者用'-c'編譯生成目標文件並單獨鏈接。 –
爲什麼包含鏈接問題的來源? –
因爲這是我第一次用C++鏈接/編譯任何東西,我想徹底:) – Chef1075