如何使用Cygwin在C ++中添加外部库 [英] How to add an external library in c++ using Cygwin

查看:131
本文介绍了如何使用Cygwin在C ++中添加外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浪费时间尝试解决这个问题,但是到目前为止还没有运...我试图通过StackOverflow寻找相同的问题,但是大多数问题与人们正在使用的IDE有关,例如VS或Eclipse.

I've already wasted time trying to figure this out but no luck so far...I've tried looking through StackOverflow for the same problem but mostly it's related to the IDE people are using, such as VS or Eclipse.

我正在从斯坦福阅读器为其C ++课程编写一些示例,但是代码无法正常工作.我试图弄清楚如何使用外部库,但是某些地方总是出错.我可能没有使用正确的命令,但是我不知道要使用哪个命令.

I was doing some examples from the Stanford Reader for their C++ course but the code won't work right. I'm trying to figure out how to use external libraries but something keeps going wrong. I'm probably not using the right command but then I don't know which command to use.

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

I'm using Cygwin and it's terminal to do the c++ exercises. I have all the files in the same folder. I am using windows 7 but that shouldn't be the biggest problem though.

作为示例,该错误很好地说明了我在编写g ++ Craps.cpp时得到的结果:

As an example this error shows well what I get when writing g++ Craps.cpp:

$ g ++ Craps.cpp

$ g++ Craps.cpp

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

/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1cf): undefined reference to `randomInteger(int, int)'

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

/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1e6): undefined reference to `randomInteger(int, int)'

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

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

在'.ctors'部分中定位地址0x0

reloc address 0x0 in section `.ctors'

collect2:ld返回1个退出状态

collect2: ld returned 1 exit status

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

The example I was running this time is just one of the ones with external libraries which gives me the same kind of error if I wrong the other. It won't find the library.

这是我的主目录,也称为Craps.cpp:

This is my main also called 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:

My 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:

and lastly my 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命令出错,那么如果我能看到应该写的内容,那就太好了.

I hope someone can help me. If it's the Cygwin command that's wrong it would be great if I could see what I should write.

刚刚发现,我什至无法正确写下本书中的示例.现在已修复,应该没有错误...我非常希望如此.抱歉.

Just found out that I couldn't even write down the examples in the book right. Fixed now and should be without mistakes...I dearly hope so. Sorry about that.

推荐答案

简短地,您应该添加 random.cpp (或者,如果已经编译过,则添加目标文件或编译了该文件的库)代码驻留)到您的命令行中:

Shortly, you should add random.cpp (or, if you already compiled it, an object file or a library where its compiled code resides) into your command line:

g++ Craps.cpp random.cpp

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

The problem you face is that the command line says the code in Craps.cpp should be compiled and then linked into an executable. While it's sufficient to have forward declarations of external functions to compile the file, you should provide the code of these functions to the linker for it to be able to create an executable.

对于库,通常使用GCC的 -l <​​/code>选项指定需要使用的库.并且您可能需要指定(使用 -L )从中获取库的路径,即使它们都在当前目录中也是如此.例如.前提是您在当前目录中拥有一个名为 librandom.a librandom.so 的库:

As for libraries, you usually specify ones that you need to use with -l option to GCC. And you might need to specify (with -L) the path where to take libraries from, even if they all are in the current directory. E.g. provided that you have a library called librandom.a or librandom.so in the current directory:

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

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

For external libraries, other directories may need to be specified so that the linker knows where to find the libraries it needs.

这篇关于如何使用Cygwin在C ++中添加外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆