2015-01-06 58 views
0

我在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) 

任何想法,我做錯了什麼嗎?

謝謝。

+2

在同一編譯命令中指定所有.cpp文件。或者,或者用'-c'編譯生成目標文件並單獨鏈接。 –

+0

爲什麼包含鏈接問題的來源? –

+0

因爲這是我第一次用C++鏈接/編譯任何東西,我想徹底:) – Chef1075

回答

1

您正在將文件構建爲單獨的程序,而不是作爲需要鏈接的獨立源文件。當你沒有指定任何特殊標誌時,gcc編譯將程序作爲一個單獨的實體鏈接,而不需要任何外部文件。

基本上有兩種解決方案:建立兩個文件一起:

 
$ g++ BinModel01.cpp Main04.cpp 

上面的命令將編譯器都源文件,然後將它們連接成可執行文件a.out

或者你可以單獨編譯源文件到目標文件,然後鏈接目標文件鏈接在一起:

 
$ g++ -c BinModel01.cpp 
$ g++ -c Main04.cpp 
$ g++ BinModel01.o Main04.o 

選項-c告訴GCC只編譯源文件,並生成一個目標文件,可稍後將用於鏈接。

如果您只有幾個源文件並且不關心編譯尚未更新的文件,那麼第一種方法是可以的。當你有更多的文件時你經常使用第二種方法,而你只想編譯實際更改的文件。這也是使用Make等工具時所使用的方法。

+0

工程!謝謝 :) – Chef1075