简体   繁体   English

在C应用程序中包含第三方库

[英]Including third-party libraries in C applications

I'm a bit naive when it comes to application development in C. I've been writing a lot of code for a programming language I'm working on and I want to include stuff from ICU (for internationalization and unicode support). 在使用C语言进行应用程序开发时,我有点天真。我一直在为正在使用的编程语言编写很多代码,并且希望包括ICU中的内容(用于国际化和unicode支持)。

The problem is, I'm just not sure if there are any conventions for including a third party library. 问题是,我不确定是否包含第三方库的任何约定。 for something like readline where lots of systems are probably going to have it installed already, it's safe to just link to it (I think). 对于诸如readline之类的东西,其中可能已经安装了很多系统,可以安全地链接到它(我认为)。 But what about if I wanted to include a version of the library in my own code? 但是,如果我想在自己的代码中包含该库的版本,该怎么办? Is this common or am I thinking about this all wrong? 这是普遍现象还是我在想这一切错吗?

If your code requires 3rd party libraries, you need to check for them before you build. 如果您的代码需要第三方库,则在构建之前需要检查它们。 On Linux, at least with open-source, the canonical way to do this is to use Autotools to write a configure script that looks for both the presence of libraries and how to use them. 在Linux上(至少在开源环境下),典型的做法是使用Autotools编写一个配置脚本,该脚本查找库的存在以及如何使用它们。 Thankfully this is pretty automated and there are tons of examples. 值得庆幸的是,这是非常自动化的,并且有大量示例。 Basically you write a configure.ac (and/or a Makefile.am ) which are the source files for autoconf and automake respectively. 基本上,您编写了configure.ac (和/或Makefile.am ),它们分别是autoconfautomake的源文件。 They're transformed into configure and Makefile.in , and ./configure conditionally builds the Makefile with any configure-time options you specify. 它们被转换为configureMakefile.in./configure有条件地使用您指定的任何配置时间选项来构建Makefile。

Note that this is really only for Linux. 请注意,这实际上仅适用于Linux。 I guess the canonical way to do it on Windows is with a project file for an IDE... 我猜想在Windows上规范的方法是使用IDE的项目文件...

If it is a .lib and it has no runtime linked libraries it gets complied into you code. 如果它是.lib且没有运行时链接库,则它将被编译为您的代码。 If you need to link to dynamic libraries you will have to assure they are there provide a installer or point the user to where they can obtain them. 如果需要链接到动态库,则必须确保它们在其中提供安装程序,或将用户指向可以获取它们的位置。

If you are talking about shipping your software off to end users and are worried about dependencies - you have to provide them correct packages/installers that include the dependencies needed to run your software, or otherwise make sure the user can get them (subject to local laws, export laws, etc, etc, etc, but that's all about licensing). 如果您正在谈论将软件交付给最终用户并担心依赖关系-您必须向他们提供正确的程序包/安装程序,其中包括运行软件所需的依赖关系,否则,请确保用户可以获取它们(取决于本地)法律,出口法律等,等等,但这仅与许可有关)。

You could build your software and statically link in ICU and whatever else you use, or you can ship your software and the ICU shared libraries. 您可以构建软件并在ICU和其他任何使用中静态链接,也可以交付软件和ICU共享库。

It depends on the OS you're targeting. 这取决于您要定位的操作系统。 For Linux and Unix system, you will typically see dynamic linking, so the application will use the library that is already installed on the system. 对于Linux和Unix系统,通常会看到动态链接,因此应用程序将使用系统上已安装的库。 If you do this, that means it's up to the user to obtain the library if they don't already have it. 如果执行此操作,则意味着如果用户还没有该库,则取决于用户。 Package managers in Linux will do this for you if you package your application in the distro's package format. 如果以发行版的软件包格式打包应用程序,Linux中的软件包管理器将为您完成此任务。

On Windows you typically see static linking, which means the application bundles the library and it will use that specific version. 在Windows上,您通常会看到静态链接,这意味着应用程序将库捆绑在一起,它将使用该特定版本。 many different applications may use the same library but include their own version. 许多不同的应用程序可能使用相同的库,但包含其自己的版本。 So you can have many copies of the library floating around on your system. 因此,您可以在系统上浮动许多库的副本。

The problem with shipping a copy of the library with your code is that you don't get the benefit of the library's maintainers' bug fixes for free. 随代码一起提供库副本的问题在于,您无法免费获得库维护人员的错误修复。 Obscure, small, and unsupported libraries are generally worth linking statically. 模糊,小型且不受支持的库通常值得静态链接。 Otherwise I'd just add the dependency and ensure that whatever packages you ship indicate it appropriately. 否则,我只是添加依赖项,并确保您附带的任何软件包都正确地指出了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM