簡體   English   中英

如何使用Cygwin在C ++中添加外部庫

[英]How to add an external library in c++ using 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:錯誤

在.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命令出錯,那么如果我能看到應該寫的內容,那就太好了。

編輯:剛剛發現我什至無法在本書中寫下這些示例。 現在已修復,應該沒有錯誤...我非常希望如此。 對於那個很抱歉。

不久,您應該在命令行中添加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文件依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM