我如何将一个库链接到我的项目在CodeBlocks& GCC没有添加库源到我的项目 [英] How do I link a library to my project in CodeBlocks & GCC without adding the library source to my project

查看:113
本文介绍了我如何将一个库链接到我的项目在CodeBlocks& GCC没有添加库源到我的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个程序,使用hashlib ++库(或将使用它),但我不想将其所有的源文件添加到我的项目,因为它是巨大的。有没有反正链接到hashlib ++源文件,以便我可以在我的项目中使用它?我已尝试使用简单的

链接到标题

  #includepath / to / hashlibpp.h

但我收到一个奇怪的错误,一旦我试图调用库中的任何函数。例如:

 未定义引用`sha1wrapper :: sha1wrapper()
pre>

我使用的是Code :: Blocks IDE和GCC编译器。

解决方案

首先,您必须在您的机器上安装库,已经编译成静态或动态库文件。您可以从源代码安装,也可以找到适用于您的操作系统的预构建包(具体取决于您使用的操作系统)。您需要知道库的名称。



在hashlib ++的情况下,他们提供了从源代码中构建静态库的指令,它们的 README ;请参阅第3.2节。



在大多数情况下,动态链接是最好的选择。这意味着库在运行时与库链接,而不是在编译时将库添加到可执行文件中(这将使可执行文件更大)。



不幸的是,根据他们的README.txt,hashlib只能作为一个静态库,这限制了你的选择。



在命令行使用gcc ,'-l'选项在库中链接:

  gcc -o MyProg -lhl ++ MyProg.c 

当使用类似Code :: Blocks的IDE时,通常必须指定要链接的库。有关如何使用Code :: Blocks执行此操作的详细信息,请参见此答案


I am writing a program that uses the hashlib++ library (or will use it) but I don't want to add all of it's source files to my project because it's huge. Is there anyway to link to the hashlib++ source files so that I can use it in my project? I've tried linking to the header directly with a simple

#include "path/to/hashlibpp.h"

But I receive a nifty error for it as soon as I attempt to call any functions from the library. For example:

undefined reference to `sha1wrapper::sha1wrapper()

I am using the Code::Blocks IDE and GCC compiler.

解决方案

First you have to have the library installed on your machine, already compiled into a static or dynamic library file. You can install from source, or you may find a pre-built package available for your OS (depending on which OS you are using). You will need to know the name of the library.

In the case of hashlib++ they have provided instructions to build a static library from source in their README; see section 3.2.

In most cases, dynamic linking is the best choice. This means that the library is linked with the library at run time, instead of adding the library to your executable when it is compiled (which would make your executable file much larger).

Unfortunately, according to their README.txt, hashlib is only available as a static lib, which limits your choices.

When compiling a program on the command line using gcc, the '-l' option links in a library:

gcc -o MyProg -lhl++ MyProg.c

When using an IDE like Code::Blocks, you normally have to specify the libraries to be linked. See this answer for details on how to do this with Code::Blocks.

这篇关于我如何将一个库链接到我的项目在CodeBlocks& GCC没有添加库源到我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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