关于库在 C 中如何工作的问题

Questions about how libraries work in C

我是一个学习C的新手,希望在一个项目中使用gLib库函数:http://www.linuxfromscratch.org/blfs/view/svn/general/glib2.html (我正在使用 Ubuntu)

关于库在 C 中的工作方式以及安装或想要使用库时会发生什么,我有几个问题:

  1. 当我安装它时(运行 ./configure && make && make install inside the folder),它到底在做什么?据我了解,C 中有共享库,C 中有静态库。它是否正在安装库并将文件包含到 /usr/lib/ 或某处?

  2. 将 gcc 与外部库一起使用时,您必须指定 -L 和 -I 标志以指定在何处查找库和头文件。安装 glib 时,是否需要指定这些标志?

  3. 如果我想为另一台机器打包我的可执行文件,如果另一台机器没有 glib 会怎样?我想如果我有静态库我就可以将它包含在二进制文件中,但是它如何适用于 glib?

  1. 关于 configuremakemake installconfigure 是一个 shell 脚本,用于发现(和配置)您的开发环境。 makemake install 是构建软件的便捷方式。其中 make 通常涉及编译和 linking,而 make install 通常涉及将可执行文件和库复制到标准路径并进行设置(如果有的话,通常还包括 /usr/include 中的文件) ,这样您就不必在 运行 可执行文件之前明确给出路径。 make做的事情可以手工完成,但是很麻烦。

  2. 对于 glibc - 是的,您必须指定这些标志。通常,大多数平台上的所有库都会有两种风格。当实际加载程序时,二进制形式用于动态 linking。此外 - 大多数发行版将具有这些库的 -dev-devel 版本。这些是构建使用这些库的软件所必需的(上面的 configure 可以帮助确定是否安装了开发库)。通常,当您看到已安装但未开发的库时,您很可能会看到 configure 错误。简而言之,如果您想 link 使用这些库,则需要 devel 版本。如果您也使用 make and make install 从源代码构建库,则不需要此步骤。

  3. 如果你想为另一台机器打包你的可执行文件,但你不确定是否有另一个 glib 或者你想确定要安装的 glib应该是您想要的一个特定版本,您应该在构建 (compiling/linking) 库时静态 link。 this gcc man page 有几个关于 link 选项的细节。我相信应该有一种静态 link glib(或 glib2)的方法。如果您已经有足够多的应用程序在使用它,通常情况下可能不需要。

When I install this (run ./configure && make && make install inside the folder), what exactly is it doing? From what I learned there are shared libraries in C and static libraries in C. Is it true that it is installing library and include files to /usr/lib/ or somewhere?

嗯,首先是 运行 ./configure,然后如果成功则运行 make,如果成功则运行 make installconfigure is a script that takes care of a lot of compatibility issues between systems. They are usually shell scripts as this is the common denominator across systems so the configure script will work across various systems. One of the thing configure does is create a Makefile. The second command make will use the newly created Makefile to build the library or executable. Since you did not specify a target (like you will in the make install) make will build the default target, which is typically the all target. This is just by convention. Makefiles are basically a list of things to build (targets) along with what they depend on (dependencies) and how to build to target (rules). Finally, make install will actually install the necessary components. For libraries this is the library and necessary header files for executables it is just the program. man pages 也可能已安装。安装这些库的位置取决于您指定的安装位置。通常 configure 将采用 --prefix 参数,让您控制它们的安装位置。如果您不使用 --prefix,您很可能会安装在系统的默认位置。

When using gcc with external libraries, you have to specify -L and -I flags to specify where to look for library and header files. When I install glib, will I need to specify these flags?

你的问题有点不清楚,所以让我先确定一下我是否理解了。您是在问安装 glib 后是否需要使用 -L 和 -I 来告诉 gcc 在哪里寻找它们?如果是这样,则取决于您安装它们的位置。通常,当您制作和安装库时,您将在默认位置安装库和头文件。如果你这样做了,那么假设你的 gcc 配置正确,那么不,你不会。如果你没有那么你很可能必须使用 -L-I

If I want to package my executable for another machine, what would happen if the other machine doesn't have glib? I think if I had static libraries I would be able to include it in the binary, but how would it work for glib?

如果它没有 glib 并且您使用了共享库,您的应用程序将无法运行。您将需要在另一台机器上拥有相同版本的 glib 库或静态构建库。如何在统计上构建它们取决于图书馆。 This SO question 可能会有帮助。

我熟悉使用 GTK+ 和 GLIB 进行开发。据我所知,库文件位于 /usr/lib 中,包含文件位于 /usr/include 中。某些库可能位于 /usr/local/lib 等位置。我会尽量回答你的问题。

  1. 当通过源包安装库时,是的,它会将文件安装到各个文件夹 /usr/share /usr/lib /usr/include 等。强烈建议您使用您的发行版的包管理器来安装库包和开发头文件。从源代码安装总是会失败,因为可能需要必要的依赖项。

  2. 这就是 autogen 和 makefile 等工具派上用场的地方。您不一定需要关心指定所有这些。 pkg-config 等工具可以处理所有这些工作。大多数库会将包配置文件安装到 /usr/lib/pkgconfig 和 /usr/share/pkgconfig 目录中。这有助于任何人轻松开发应用程序 link 将他们的代码添加到库中。

使用包配置获取配置:

 $ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include  -lglib-2.0

使用 GCC 和包配置进行链接:

$gcc example.c `pkg-config --cflags --libs gtk+2.0 glib-2.0` -o example

以上命令将 link 我的 gtk 和 glib 程序。

使用 Makefile 不再需要再次输入那些长行:

生成文件:

OBJS = main.o callbacks.o
CFLAGS = `pkg-config --cflags --libs  gtk+-2.0`
program:  $(OBJS)
    gcc -o program $(OBJS)
main.o: main.c
    gcc -c main.c $(FLAGS)
callbacks.o: callbacks.c callbacks.h
    gcc -c callbacks.c $(FLAGS)

.PHONY : clean
clean:
    rm *.o
    rm program
.PHONY : install
install:
    cp program /usr/bin
.PHONY : uninstall
uninstall:
    rm /usr/bin/program

上面的 makefile 是一个简单的 GTK+2.0 应用程序,你可以通过 CFLAGS 中包含的包配置来判断要使程序可执行,你只需在源目录中输入 make。 pkg-config 仅在您已经安装了您尝试使用的库的开发包时才有效。要 ubuntu 安装 GTK+-3.0 和 GLIB 开发文件,您需要输入:

$ apt-get install libgtk-3-dev
  1. 我认为这是一个很好的可移植性问题。没有一个静态库是跨平台的。它必须手动为这些平台编译。我认为使用 GNOME 软件基金会开发的 Anjuta IDE 可以摆脱所有令人头疼的问题。它使开发支持 C 和 C++ 的 GLIB 和 GTK+ 应用程序变得轻而易举。它将创建 Makefileconfigure 等文件,使跨平台代码开发和部署变得容易。我可以 link 你一些资源,但我在堆栈溢出上的声誉不到 10。所以我只会在下面提到一些资源的名称。

进一步阅读

Makefile 教程

安居塔IDE (C/C++): ://anjuta.org/

GTK+-3.0 Hello World 编译和 linking 使用 pkg-config: