[英]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.a
或librandom.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
(可能是gmake
或mingw32-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.