编译三个C ++文件。链接错误

时间:2015-01-06 18:21:37

标签: c++ macos compilation

我在OSX Yosemite,我有三个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)

知道我在这里做错了吗?

感谢。

1 个答案:

答案 0 :(得分: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。等工具时使用的方法。