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

Compiling three C++ files. Linking error

我在 OSX 优胜美地,我有三个要编译的 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)

知道我做错了什么吗?

谢谢。

您正在将文件构建为单独的程序,而不是需要 linked 的单独源文件。当您不指定任何特殊标志时,gcc link 程序编译为没有任何外部文件的单个实体。

基本上有两种解决方案:将两个文件一起构建:

$ g++ BinModel01.cpp Main04.cpp

以上命令将编译两个源文件,然后link将它们一起编译成可执行文件a.out

或者您可以将源文件分别编译成目标文件,然后link将目标文件一起编译:

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

选项 -c 告诉 GCC 只编译源文件,并生成一个目标文件,稍后可用于 linking。

如果您只有几个源文件并且不关心编译未更新的文件,第一种方法是可以的。第二种方法常用于文件比较多,只想编译真正改动过的文件的情况。也是在使用Make等工具时使用的方法。