我需要在我的c ++程序中安装一些软件包,所以我编写了以下代码:
pkgInitConfig(*_config);
pkgInitSystem(*_config, _system);
pkgCacheFile cache_file;
pkgCache *cache = cache_file.GetPkgCache();
pkgDepCache *dep_cache = cache_file.GetDepCache();
pkgPackageManager *pm = new pkgPackageManager(dep_cache);
for(pkgCache::PkgIterator package = cache->PkgBegin(); !package.end(); package++)
{
if((*package).CurrentState == pkgCache::State::NotInstalled)
{
if(package.Name() == "MyPkgName")
if(dep_cache->MarkInstall(package))
{
std::cout << "Marked" << std::endl;
}
break;
}
}
APT::Progress::PackageManager *progress = APT::Progress::PackageManagerProgressFactory();
pkgPackageManager::OrderResult rs = pm->DoInstall(progress);
if(rs == pkgPackageManager::Completed)
std::cout << "Completed" << std::endl;
else if (rs == pkgPackageManager::Failed)
std::cout << "Failed!" << std::endl;
else
std::cout << "InComplete!" << std::endl;
似乎这段代码正在完成它的工作而pm->DoInstall()
函数的结果是pkgPackageManager::Completed
,但是当我尝试从命令行安装这个包时,apt-get并没有说包装已安装!实际上没有安装包。
这段代码有什么问题吗?安装完成需要做些什么?