简体   繁体   English

关于图书馆的一些疑惑

[英]Some doubts about libraries

Say, I want to write a program in C++ in Linux and I need some specific libraries.比如说,我想在 Linux 中用 C++ 编写一个程序,我需要一些特定的库。 There are 2 ways of getting these libraries:有两种方法可以获取这些库:

  1. Using the command line--> apt-get install library使用命令行--> apt-get install library
  2. Downloading and extracting a .zip or .tar file from their website.从他们的网站下载并提取 .zip 或 .tar 文件。

Now my questions are:现在我的问题是:

For the first method I have seen libraries being downloaded with apt-get install library and apt-get install library-dev.对于第一种方法,我已经看到使用 apt-get install library 和 apt-get install library-dev 下载库。 I know dev means development or developer, but what is the difference between installing the dev and not installing the dev?我知道 dev 意味着开发或开发人员,但是安装 dev 和不安装 dev 有什么区别? What does dev do, exactly? dev 究竟是做什么的?

For the second method, do I need to build the libraries using a compiler?对于第二种方法,我是否需要使用编译器来构建库? Because I have seen tutorials doing it but the OS used was usually Windows, do I only have to build them on Windows and not Linux?因为我看过教程,但使用的操作系统通常是 Windows,我是否只需要在 Windows 上构建它们而不是 Linux?

Also, say I can only use the 2nd method for a certain library and not the first one.另外,说我只能对某个库使用第二种方法,而不是第一种。 After extracting, what am I supposed to do?提取后,我该怎么办? Is there any default way of installing a library manually or is each library different?是否有手动安装库的默认方式,或者每个库都不同?

Finally, when I use the first method where is the library installed to?最后,当我使用第一种方法时,库安装在哪里? Is it /usr/local/lib, /usr/lib or /usr/include?是 /usr/local/lib、/usr/lib 还是 /usr/include? Because when I have to link to these libraries in the Linker's settings I only write their name, not the path so I assume there is already a default path for libraries to be in.因为当我必须在链接器的设置中链接到这些库时,我只写了它们的名字,而不是路径,所以我假设已经有一个库的默认路径。

One last question: Is there any default way of installing and using libraries in general or does that depend on what I want to do, programming language, etc...?最后一个问题:是否有任何安装和使用库的默认方式,或者这取决于我想要做什么,编程语言等......?

The second method is very broad because it depends entirely on the how the project is designed including the build system used etc. Things get a little more conformant when you use a distribution's managed packages.第二种方法非常广泛,因为它完全取决于项目的设计方式,包括使用的构建系统等。当您使用发行版的托管包时,事情会变得更加一致。

If you want to develop a program that uses the library you need the library-dev package that usually contains the C/C++/etc.. header files.如果要开发使用该库的程序,则需要通常包含C/C++/etc..头文件的library-dev包。

Many development package conform to a standard tool that helps your build system find the libraries header and binary files.许多开发包符合标准工具,可帮助您的构建系统找到库头文件和二进制文件。

For example libcurl uses the pkg-config system so its compiler components can be found from the command line like this:例如, libcurl使用pkg-config系统,因此可以从命令行找到它的编译器组件,如下所示:

pkg-config libcurl --libs # print the library link flags

You can then add that to your Makefile (or whatever build system you use):然后,您可以将其添加到您的Makefile (或您使用的任何构建系统)中:

program:
     g++ -o program program.cpp $(shell pkg-config libcurl --libs)

The $(shell pkg-config libcurl --libs) part adds the correct compiler flags to link with the library. $(shell pkg-config libcurl --libs)部分添加了正确的编译器标志以与库链接。

Not all dev packaged use pkg-config .并非所有开发包都使用pkg-config Some come with their own tools (like mysql_config ) while others let you guess and try to figure it all out for yourself (looking at you libclang ).有些带有自己的工具(如mysql_config ),而另一些则让您猜测并尝试自己弄清楚(看着您libclang )。

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

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