apt-get install <package> 等效使用 C++ apt-pkg 库

mac*_*mac 9 package-management apt qt c++

我正在构建一个小型 QT (C++) 应用程序,我向用户询问他想要安装的软件。一旦他从列表中选择了一些包 a、b、c,我所要做的就是运行

sudo apt-get install a b c
Run Code Online (Sandbox Code Playgroud)

一种方法是使用 Qpr​​ocess 或 System 并直接从 C++ 运行此命令。但我认为这将是一个黑客,并想使用 apt-pkg C++ 库。但遗憾的是,这个库的文档非常稀少:( 我看到了一些类似软件的源代码 - 软件更新程序( apt-watch )等,发现它太复杂了。只是为了运行上面的命令,它有很多代码 - 初始化 pkgCacheFile,PkgIterator , pkgAcqArchive。

我必须做所有这些才能运行这个简单的命令吗?不是有一个直接的函数以软件名称作为参数并安装它吗?我在哪里可以获得相同的示例工作代码?

小智 2

我只想使用系统:

SYSTEM(3)            Linux Programmer's Manual           SYSTEM(3)

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       system() executes a command specified in command by calling
       /bin/sh -c command, and returns after the command has  been
       completed.   During  execution of the command, SIGCHLD will
       be blocked, and SIGINT and SIGQUIT will be ignored.

RETURN VALUE
       The value returned is -1 on error (e.g.   fork(2)  failed),
       and  the return status of the command otherwise.  This lat-
       ter return status is in the format  specified  in  wait(2).
       Thus, the exit code of the command will be WEXITSTATUS(sta-
       tus).  In case /bin/sh could not be executed, the exit sta-
       tus will be that of a command that does exit(127).

       If  the  value of command is NULL, system() returns nonzero
       if the shell is available, and zero if not.

       system() does not affect the wait status of any other chil-
       dren.
Run Code Online (Sandbox Code Playgroud)

使用简单的解决方案并不是黑客。


小智 0

答案是libapt-pkg。它是一个用 C 编写的库,这使得它也可以从 C++ 代码访问。

分别安装库及其文档的软件包libapt-pkg-dev和。libapt-pkg-doc