首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c++中获取包版本?(linux)

如何在c++中获取包版本?(linux)
EN

Stack Overflow用户
提问于 2014-11-06 09:14:12
回答 1查看 1K关注 0票数 0

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

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-11-06 09:50:16

首先,我认为你不应该为此而烦恼。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的发行版。

代码语言:javascript
复制
$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

代码语言:javascript
复制
#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();

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26770155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档