我在哪里放置第三方库来设置 C++ Linux 开发环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3685716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:00:06  来源:igfitidea点击:

Where do I put third-party libraries to set up a C++ Linux development environment?

c++linuxlibraries

提问by Killrazor

I'm not new in C++ although I'm new in Linux. I'm using CMake to precompile a cross-platform game engine with some third-party components, but I have a lot of doubts about using libraries. My question is how to work with third-party libraries and where to put them. Apt installs libs in their official place (/usr/local, /usr/lib/ ..) but I develop in Windows using local libs that are in a folder in my project dir.

尽管我是 Linux 新手,但我对 C++ 并不陌生。我正在使用CMake预编译带有一些第三方组件的跨平台游戏引擎,但我对使用库有很多疑问。我的问题是如何使用第三方库以及将它们放在哪里。Apt 在其官方位置(/usr/local、/usr/lib/ ..)安装库,但我在 Windows 中使用位于我的项目目录中的文件夹中的本地库进行开发。

Also, I need a good tutorial to know the rules of how libraries work. For example: when trying to compile my project, luabind is asking for liblua.s0.1, but AFAIK there is no way to generate this library with the source provided by Lua (at least doing make, make install).

另外,我需要一个很好的教程来了解库的工作规则。例如:在尝试编译我的项目时,luabind 要求提供 liblua.s0.1,但 AFAIK 无法使用 Lua 提供的源生成此库(至少做了 make、make install)。

I know, this question is fuzzy but I haven't enough experience to be more concise.

我知道,这个问题很模糊,但我没有足够的经验来更简洁。

Update: After reading some answers, a more concise question is the following. If I install all third-party libraries, how can I distribute my program? How do I manage dependencies without using a large readme?

更新:阅读一些答案后,以下是一个更简洁的问题。如果我安装了所有第三方库,我该如何分发我的程序?如何在不使用大型自述文件的情况下管理依赖项?

采纳答案by Josh Kelley

Where to put libraries

在哪里放置库

The best solution is to use your Linux distribution's packaging system (apt-get, yum, or similar) to install libraries from distro-provided packages wherever possible.

最好的解决办法是使用Linux发布版的包装系统(apt-getyum或类似)从发行版提供的软件包安装库尽可能。

If the distro's packaged libraries aren't of a recent enough version, or if you need some nonstandard build options, or if you need a library that your distro doesn't provide, then you can build and install it yourself. You have two main options for where to put the library:

如果发行版的打包库版本不够新,或者如果您需要一些非标准的构建选项,或者如果您需要发行版未提供的库,那么您可以自己构建和安装它。您有两个主要选项可用于放置库的位置:

  • /usr/local(libraries under /usr/local/lib, headers under /usr/local/include). This installs the libraries systemwide and is probably the simplest solution, since you should then be able to build against them without taking any extra steps. Do NOT install libraries directly under /usr, since that will interfere with your distro's packaging system.
  • Under your project directory, as you did under Windows. This has the advantages of not requiring root access and not making systemwide changes, but you'll have to update your project's include paths and library paths, and you'll have to put any shared library files someplace where the dynamic linkercan find them (using LD_LIBRARY_PATHor ld.so.conf- see the link for more details).
  • /usr/local(下的库,下的/usr/local/lib标题/usr/local/include)。这会在系统范围内安装库,并且可能是最简单的解决方案,因为您应该能够在不采取任何额外步骤的情况下针对它们进行构建。不要直接在 下安装库/usr,因为这会干扰您发行版的打包系统。
  • 在您的项目目录下,就像在 Windows 下一样。这具有不需要 root 访问权限和不进行系统范围更改的优点,但是您必须更新项目的包含路径和库路径,并且您必须将任何共享库文件放在动态链接器可以找到它们的地方(使用LD_LIBRARY_PATHld.so.conf- 有关更多详细信息,请参阅链接)。

How libraries work

图书馆如何运作

See David A. Wheeler's excellent Programming Library HOWTO. I'd recommend reading that then posting any specific questions as new topics.

请参阅 David A. Wheeler 的优秀编程库 HOWTO。我建议阅读该文章,然后将任何特定问题作为新主题发布。

How to distribute your program

如何分发您的程序

Traditionally, Unix / Linux programs do not include copies of their dependencies. It's instead up to the end user or developer to install those dependencies themselves. This can require a "large README," as you said, but it has a few advantages:

传统上,Unix / Linux 程序不包含其依赖项的副本。相反,由最终用户或开发人员自己安装这些依赖项。正如您所说,这可能需要“大型自述文件”,但它有一些优点:

  • Development libraries can be installed, managed, and updated via the distro's package manager, instead of each source copy having its own set of libraries to track.
  • There's only one copy of any given library on a system, so there's only one place that needs updating if, for example, a security flaw is found. (For example, consider the chaos that resulted when zlib, a very widely used compression library, was found to have a security flaw, so every application that included an affected version needed to be updated.)
  • If your program is popular enough (and is open source or at least freely available), then package maintainers for various Linux distributions may want to package it and include it in their distro. Package maintainers reallydon't like bundled libraries. See, for example, Fedora's page on the topic.
  • 开发库可以通过发行版的包管理器安装、管理和更新,而不是每个源副本都有自己的一组库来跟踪。
  • 系统上任何给定库只有一个副本,因此如果发现安全漏洞等,则只有一个地方需要更新。(例如,考虑到当zlib(一个使用非常广泛的压缩库)被发现存在安全漏洞时导致的混乱,因此每个包含受影响版本的应用程序都需要更新。)
  • 如果您的程序足够流行(并且是开源的或至少是免费提供的),那么各种 Linux 发行版的软件包维护者可能希望将其打包并包含在他们的发行版中。包维护者真的不喜欢捆绑的库。例如,请参阅Fedora 上有关该主题的页面

If you're distributing your program to end users, you may want to consider offering a package (.dpkgor .rpm) that they could simply download and install without having to use source. Ideally, from the end user's perspective, the package would be added to distros' repositories (if it's open source or at least freely available) so that users can download it using their package managers (apt-getor yum). This can all get complicated, because of the large number of Linux distros out there, but a Debian/Ubuntu compatible .dpkgand a Red Hat/CentOS/Fedora-compatible .rpmshould cover a good percentage of end users. Building packages isn't too hard, and there are good howtos online.

如果您将您的程序分发给最终用户,您可能需要考虑提供一个软件包(.dpkg.rpm),他们可以简单地下载和安装而无需使用源代码。理想情况下,从最终用户的角度来看,该软件包将被添加到发行版的存储库(如果它是开源的或至少是免费提供的),以便用户可以使用他们的软件包管理器(apt-getyum)下载它。由于存在大量 Linux 发行版,这一切都会变得复杂,但兼容 Debian/Ubuntu.dpkg和兼容Red Hat/CentOS/Fedora.rpm的终端用户应该占很大比例。构建软件包并不太难,而且网上有很好的 howtos。

回答by jrharshath

Okay, so this is one of the basic questions and while I myself might not come across very clear on this, here goes:

好的,这是基本问题之一,虽然我自己可能对此不太清楚,但这里有:

  1. While building a project, your compiler will need to find the header files of the libraries. The headers must be in the include path.
  2. after compilation is done, the linker will look for the library binaries (files.so or something like that). These must be in the Library path.
  1. 在构建项目时,您的编译器需要找到库的头文件。标头必须在包含路径中。
  2. 编译完成后,链接器将查找库二进制文件(files.so 或类似的东西)。这些必须在库路径中。

That's the basics.

这就是基础知识。

If you have some specific libaries, you can add them to your own project-specific lib/and include/directories and add them to the include path and the library path respectively.

如果您有一些特定的库,您可以将它们添加到您自己的项目特定lib/include/目录中,并将它们分别添加到包含路径和库路径中。

Adding these dirs to these paths can be done in many ways, depending upon how you are building the project. I'm sure there is something called LD_PATH involved in all this... But I don't really know the specifics involved with CMake.

可以通过多种方式将这些目录添加到这些路径中,具体取决于您构建项目的方式。我确定所有这些都涉及到一个叫做 LD_PATH 的东西......但我真的不知道 CMake 涉及的细节。

A little googling can help you do the above with CMake.

一点谷歌搜索可以帮助您使用 CMake 完成上述操作。

Hope that helps,
jrh

希望有帮助,
jrh

回答by doron

If you are installing the libraries with a package manager, they will probably all end up in the right place. If not you can get the compiler to search for the by providing the an additional search path using the -L <path>flag. You should be able to pass this extra flag to CMake.

如果您使用包管理器安装这些库,它们可能最终都会出现在正确的位置。如果没有,您可以通过使用-L <path>标志提供额外的搜索路径来让编译器搜索。您应该能够将这个额外的标志传递给 CMake。

Incidentally the -I <path>can be used to add an extra directory to search for include files.

顺便提一下,-I <path>可用于添加一个额外的目录来搜索包含文件。

回答by stijn

for the first part of your question regarding Windows: there's no real standard place for libraries/headers on Windows, so the easy solution is: create your own. Simply provide a single lib/ and include/ on your system and have all your projects use it (by setting the path in a cmake file that you include everywhere). Put all third party libs in there, for example:

对于有关 Windows 的问题的第一部分:Windows 上没有真正标准的库/标头位置,因此简单的解决方案是:创建自己的。只需在您的系统上提供一个 lib/ 和 include/ 并让您的所有项目都使用它(通过在包含在任何地方的 cmake 文件中设置路径)。将所有第三方库放在那里,例如:

your projects:

您的项目:

d:/projects/projectA
d:/projects/projectB

third party stuff:

第三方的东西:

d:/api/lib/lua.lib
d:/api/include/lua/....

(you can even use symlinks aka 'directory junctions' if you have different version)

(如果你有不同的版本,你甚至可以使用符号链接又名“目录连接”)

and the corresponding cmake file:

和相应的cmake文件:

include_directories( d:/api/include )
link_directories( d:/api/lib )