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

时间:2012-11-14 19:57:03

标签: c++ g++ cygwin

我已经浪费时间试图解决这个问题,但到目前为止还没有运气......我已经尝试通过StackOverflow查找相同的问题,但大多数情况下它与IDE人员相关使用,如VS或Eclipse。

我在斯坦福读者中为他们的C ++课程做了一些例子,但代码不能正常工作。我试图弄清楚如何使用外部库,但有些事情一直存在问题。我可能没有使用正确的命令,但我不知道使用哪个命令。

我正在使用Cygwin和它的终端进行c ++练习。我将所有文件放在同一个文件夹中。我使用的是Windows 7,但这不应该是最大的问题。

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

  

$ g ++ Craps.cpp

     

/tmp/ccdTdi0t.o:Craps.cpp :(。text + 0x1cf):未定义引用`randomInteger(int,int)'

     

/tmp/ccdTdi0t.o:Craps.cpp :(。text + 0x1e6):未定义引用`randomInteger(int,int)'

     

/usr/lib/gcc/i686-pc-cygwin/4.5.3 /../../../../ i686-pc-cygwin / bin / ld:/tmp/ccdTdi0t.o:bad

     

在`.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;
}

我的random.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;
   }
}

最后我的random.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

我希望有人可以帮助我。如果Cygwin命令错了,那么如果我能看到我应该写的东西那就太棒了。

编辑: 刚刚发现我甚至无法在书中写下这些例子。现在修复,应该没有错误...我非常希望如此。对不起。

2 个答案:

答案 0 :(得分:3)

很快,您应该在命令行中添加random.cpp(或者,如果您已编译它,目标文件或其编译代码所在的库):

g++ Craps.cpp random.cpp

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

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

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

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

答案 1 :(得分:1)

其他答案有简单的命令来编译它,但这是一个非常粗略和简单的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文件。现在它并没有隐含地知道如何从.exe文件中获取.o,这就是为什么我们有第二行的原因。 $@表示规则的目标(Craps.exe此处),而$^表示:之后列出的所有文件。

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

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