如何使用Cygwin在c++中添加外部库

How to add an external library in c++ using Cygwin

本文关键字:添加 外部 c++ 何使用 Cygwin      更新时间:2023-10-16

我已经浪费时间试图弄清楚了,但到目前为止运气不佳。。。我试着在StackOverflow中查找同样的问题,但它主要与人们使用的IDE有关,例如VS或Eclipse。

我在斯坦福阅读器上为他们的C++课程做了一些例子,但代码无法正常工作。我正试图弄清楚如何使用外部库,但总是出了问题。我可能没有使用正确的命令,但我不知道该使用哪个命令。

我正在使用Cygwin,它是做c++练习的终端。我把所有的文件都放在同一个文件夹里。我使用的是windows7,但这不应该是最大的问题。

作为一个例子,这个错误很好地显示了我在编写g++Craps.cpp:时得到的结果

$g++Craps.cpp

/tmp/ccdDi0t.o:Caps.cpp:(.text+0x1cf):对`randomInteger(int,int)'的未定义引用

/tmp/ccdDi0t.o:Caps.cpp:(.text+0x1e6):对`randomInteger(int,int)'的未定义引用

/usr/lib/gcc/i686 pc cygwin/4.5.3/../../../i686 pc cygwin/bin/ld:/tmp/ccdDi0t.o:错误

重新定位".ctors"部分中的地址0x0

collect2:ld返回1退出状态

我这次运行的例子只是其中一个带有外部库的例子,如果我错了另一个,它会给我同样的错误。它找不到图书馆。

这是我的主要也称为Craps.cpp:

#include <iostream>
#include "random.h"
using namespace std;
bool tryToMakePoint(int point);
int rollTwoDice();
int main() {
cout << "This program plays a game of craps." << endl;
int point = rollTwoDice();
switch (point) {
case 7: case 11:
cout << "That's a natural. You win." << endl;
break;
case 2: case 3: case 12:
cout << "That's craps. You lose" << endl;
break;
default:
cout << "Your point is " << point << "." << endl;
if (tryToMakePoint(point)) {
cout << "You made your point. You win." << endl;
} else {
cout << "You rolled a seven. You lose." << endl;
}
}
return 0;
}

bool tryToMakePoint(int point) {
while (true) {
int total = rollTwoDice();
if (total == point) return true;
if (total == 7) return false;
}
}

int rollTwoDice() {
cout << "Rolling the dice . . . " << endl;
int d1 = randomInteger(1, 6);
int d2 = randomInteger(1, 6);
int total = d1 + d2;
cout << "You rolled " << d1 << " and " << d2 
<< " - that's " << total << endl;
return total;
}

我的随机.cpp:

#include <cstdlib>
#include <cmath>
#include <ctime>
#include "random.h"
using namespace std;
void initRandomSeed();
int randomInteger(int low, int high) {
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (double(high) - low + 1);
return int(floor(low + s ));
}
double randomReal(double low, double high) {
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (high - low);
return low + s;
}
bool randomChance(double p) {
initRandomSeed();
return randomReal(0, 1) < p;

}
void setRandomSeed(int seed) {
initRandomSeed();
srand(seed);
}
void initRandomSeed() {
static bool initialized = false;
if (!initialized) {
srand(int(time(NULL)));
initialized = true;
}
}

最后是我的随机。h:

#ifndef _random_h
#define _random_h
int randomInteger(int low, int high);
double randomReal(double low, double high);
bool randomChance(double p);
void setRandomSeed(int seed);
#endif

我希望有人能帮助我。如果是赛格温的命令错了,如果我能看到我应该写什么就太好了。

编辑:刚发现我甚至不能把书中的例子写对。现在修复,应该没有错误。。。我非常希望如此。很抱歉。

很快,您应该将random.cpp(或者,如果您已经编译了它,则是其编译代码所在的对象文件或库)添加到命令行中:

g++ Craps.cpp random.cpp

你面临的问题是,命令行说应该编译Craps.cpp中的代码,然后将其链接到可执行文件中。虽然有外部函数的前向声明来编译文件就足够了,但您应该向链接器提供这些函数的代码,以便它能够创建可执行文件。

对于库,您通常指定需要与GCC的-l选项一起使用的库。您可能需要指定(使用-L)从中获取库的路径,即使它们都在当前目录中。例如,假设您在当前目录中有一个名为librandom.alibrandom.so的库:

g++ Craps.cpp -L. -l random

对于外部库,可能需要指定其他目录,以便链接器知道在哪里可以找到所需的库。

其他答案有一个简单的命令来编译它,但这里有一个非常粗略和简单的Makefile示例,让您开始这条路。用这些内容创建一个名为Makefile的文件,其中<tab>必须是一个实际的制表符,而不是一些空格:

Craps.exe : Craps.o random.o
<tab>g++ -o $@ $^
Craps.o random.o : random.h

然后运行

make Craps.exe

(可能是gmakemingw32-make而不是普通的make)。

现在Make内置了一些关于文件类型的神奇功能,所以当它发现Craps.exe需要这两个.o文件时,它会开始寻找可以制作它们的文件,它会找到.cpp文件,并知道如何将它们编译成.o文件。现在它还不知道如何从.o文件中生成.exe,所以这就是为什么我们有第二行。其中$@表示规则的目标(此处为Craps.exe),而$^表示在:之后列出的所有文件。

Craps.o random.o:random.h规则意味着,如果更改了random.h,则需要重新创建Craps.o和random.o(这些.h文件依赖项可以自动生成,但只要您只有几个文件,手动写入即可)。

如果您需要添加更多的源文件,只需在第一行添加带有.o后缀的文件,make就会将它们包含在编译中。还要注意make如何只编译更改的文件。。。然后,当您获得更多的.h文件时,您将需要添加.h文件依赖项。