gpt4 book ai didi

c++ - 如何获取 C++ 中的包版本? (Linux)

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:41 26 4
gpt4 key购买 nike

我一直在编写一个程序,需要 2.6 或更高版本的 wine 版本。我想将其作为 bool 值,所以我一直在尝试使用下面的代码:

#include <string>
#include <iostream>
#include "WineCheck.h"
#include <cstdlib>

using namespace std;

bool checkForWine()
{
// Create variables for checking wine state
bool wineIsThere = false;
bool wineIsVersion = false;

// Check dpkg if wine is there
if (string(system("dpkg -l | cut -c 4-9 | grep \\ wine\\ ")) == " wine ")
{
wineIsThere = true;
// Check version
if (double(system("wine --version | cut -c 6-8")) >= 2.6)
wineIsVersion = true;
}

// Return
if (wineIsThere && wineIsVersion)
return true;
else
return false;
}

最佳答案

首先,我认为你不应该为此烦恼。 Wine 2.6 应该作为依赖项包含在您的配置脚本和/或包文件中。如果您想保持对不使用该打包程序的其他 GNU/Linux 发行版的可移植性,那么在程序源代码中针对特定的包管理系统并不是一个好主意。

不过还是要回答你的问题。我发现有两种方法可以做到这一点。您可以检查/var/lib/dpkg/statusRead through the file line by line直到您到达此部分。如果您没有找到该部分,或者 Status: ... 行没有显示 installed,那么 wine 就没有安装。 Version: ... 行将告诉您安装的版本。我通过在 Debian Wheezy 上安装和卸载 Wine 来验证此方法是否有效。您没有说明您正在使用哪个发行版,但很明显您正在使用 Debian 打包系统,因此这也应该适用于 Ubuntu 和其他基于 Debian 的发行版。

$cat /var/lib/dpkg/status
...
Package: wine
Status: install ok installed
Priority: optional
Section: otherosf
Installed-Size: 80
Maintainer: Debian Wine Party &lt;pkg-wine-party@lists.alioth.debian.org&gt;
Architecture: amd64
Version: 1.4.1-4
...

另一个选项是使用libdpkg。我找到了example that lists all installed packages .

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include "filesdb.h"

const char thisname[] = "example1";

int
main(int argc, const char *const *argv)
{
struct pkg_array array;
struct pkginfo *pkg;
int i;
enum modstatdb_rw msdb_status;

standard_startup();
filesdbinit();
msdb_status = modstatdb_open(msdbrw_readonly);
pkg_infodb_init(msdb_status);

pkg_array_init_from_db(&array);
pkg_array_sort(&array, pkg_sorter_by_name);

for (i = 0; i < array.n_pkgs; i++) {
pkg = array.pkgs[i];
if (pkg->status == stat_notinstalled)
continue;
printf("Package --> %s\n", pkg->set->name);
}

pkg_array_destroy(&array);

modstatdb_shutdown();
standard_shutdown();

}

关于c++ - 如何获取 C++ 中的包版本? (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com