How do I link a library to my project in CodeBlocks & GCC without adding the library source to my project

你说的曾经没有我的故事 提交于 2019-12-11 11:24:01

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21127038/how-do-i-link-a-library-to-my-project-in-codeblocks-gcc-without-adding-the-lib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!