gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:29:53 24 4
gpt4 key购买 nike

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

知道我在这里做错了什么吗?

谢谢。

最佳答案

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

基本上有两种解决方案:同时构建两个文件:

$ g++ BinModel01.cpp Main04.cpp

上述命令将编译两个源文件,然后将它们链接到可执行的 a.out 文件中。

或者您可以将源文件单独编译成目标文件,然后将目标文件链接在一起:

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

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

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

关于c++ - 编译三个 C++ 文件。链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com