当前位置: 技术问答>linux和unix
linux下编译问题 库的使用 急!!!!
来源: 互联网 发布时间:2016-11-14
本文导语: 比如我开发一个gtk的程序 编译时候这么gcc -o app app.c `pkg-config --libs --cflags gtk+-2.0` 现在我要使用rpm库开发一个rpm相关的程序(rpm库已经安装), 那么在pkg-config 里面应该怎么用 `pkg-config --libs --cflags rpm-4.0` 这...
比如我开发一个gtk的程序 编译时候这么gcc -o app app.c `pkg-config --libs --cflags gtk+-2.0`
现在我要使用rpm库开发一个rpm相关的程序(rpm库已经安装), 那么在pkg-config 里面应该怎么用 `pkg-config --libs --cflags rpm-4.0` 这个不行
附源码:
#include
#include
#include
#include
#include
#include
int main()
{
ts = rpmtsCreate();
rpmtsSetRootDir(ts, NULL);
add_for_install(ts, optarg, upgrade);
tsi = rpmtsiInit(ts);
rc = rpmtsCheck(ts);
probs = rpmtsProblems(ts);
if (rc || probs->numProblems > 0) {
rpmpsPrint(fp, probs); // trying to redirect install logs to a file
while(fgets(buf,128,fp))
{
data.append(buf);
}
rpmpsFree(probs);
rpmtsFree(ts);
exit(rc);
}
/* Create ordering for the transaction */
rc = rpmtsOrder(ts);
if (rc > 0) {
printf("Ordering failedn");
rpmtsFree(ts);
exit(rc);
}
rpmtsClean(ts);
/* Set callback routine & flags, for example -vh */
notifyFlags |= INSTALL_LABEL | INSTALL_HASH;
rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *)notifyFlags);
/* Set transaction flags and run the actual transaction */
rpmtsSetFlags(ts, (rpmtransFlags)(rpmtsFlags(ts) | tsFlags));
rc = rpmtsRun(ts, NULL, (rpmprobFilterFlags)probFilter);
/* Check for results .. */
if (rc || probs->numProblems > 0)
rpmpsPrint(fp, probs);
while(fgets(buf,128,fp))
{
data.append(buf); // trying to redirect logs to a file
}
rpmpsFree(probs);
rpmtsFree(ts);
exit(rc);
}
int add_for_install(rpmts ts, char *file, int upgrade)
{
FD_t fd;
Header hdr;
int rc = 0;
rpmRelocation * relocs;
relocs = (rpmRelocation *)malloc(2*sizeof(rpmRelocation));
relocs->oldPath = "/opt/";
relocs->newPath = "/mnt/tmp"; // trying to relocate installation to a diff directory
fd = Fopen(file, "r.ufdio");
if (fd == NULL) {
printf("Unable to open file %sn", file);
return 1;
}
rc = rpmReadPackageFile(ts, fd, file, &hdr);
if (rc != RPMRC_OK) {
printf("Unable to read package %sn", file);
return rc;
}
/* Add it to the transaction set */
rc = rpmtsAddInstallElement(ts, hdr, file, upgrade,relocs);
if (rc) {
printf("Error adding %s to transactionn", file);
goto out;
}
out:
headerFree(hdr);
Fclose(fd);
return rc;
}
现在我要使用rpm库开发一个rpm相关的程序(rpm库已经安装), 那么在pkg-config 里面应该怎么用 `pkg-config --libs --cflags rpm-4.0` 这个不行
附源码:
#include
#include
#include
#include
#include
#include
int main()
{
ts = rpmtsCreate();
rpmtsSetRootDir(ts, NULL);
add_for_install(ts, optarg, upgrade);
tsi = rpmtsiInit(ts);
rc = rpmtsCheck(ts);
probs = rpmtsProblems(ts);
if (rc || probs->numProblems > 0) {
rpmpsPrint(fp, probs); // trying to redirect install logs to a file
while(fgets(buf,128,fp))
{
data.append(buf);
}
rpmpsFree(probs);
rpmtsFree(ts);
exit(rc);
}
/* Create ordering for the transaction */
rc = rpmtsOrder(ts);
if (rc > 0) {
printf("Ordering failedn");
rpmtsFree(ts);
exit(rc);
}
rpmtsClean(ts);
/* Set callback routine & flags, for example -vh */
notifyFlags |= INSTALL_LABEL | INSTALL_HASH;
rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *)notifyFlags);
/* Set transaction flags and run the actual transaction */
rpmtsSetFlags(ts, (rpmtransFlags)(rpmtsFlags(ts) | tsFlags));
rc = rpmtsRun(ts, NULL, (rpmprobFilterFlags)probFilter);
/* Check for results .. */
if (rc || probs->numProblems > 0)
rpmpsPrint(fp, probs);
while(fgets(buf,128,fp))
{
data.append(buf); // trying to redirect logs to a file
}
rpmpsFree(probs);
rpmtsFree(ts);
exit(rc);
}
int add_for_install(rpmts ts, char *file, int upgrade)
{
FD_t fd;
Header hdr;
int rc = 0;
rpmRelocation * relocs;
relocs = (rpmRelocation *)malloc(2*sizeof(rpmRelocation));
relocs->oldPath = "/opt/";
relocs->newPath = "/mnt/tmp"; // trying to relocate installation to a diff directory
fd = Fopen(file, "r.ufdio");
if (fd == NULL) {
printf("Unable to open file %sn", file);
return 1;
}
rc = rpmReadPackageFile(ts, fd, file, &hdr);
if (rc != RPMRC_OK) {
printf("Unable to read package %sn", file);
return rc;
}
/* Add it to the transaction set */
rc = rpmtsAddInstallElement(ts, hdr, file, upgrade,relocs);
if (rc) {
printf("Error adding %s to transactionn", file);
goto out;
}
out:
headerFree(hdr);
Fclose(fd);
return rc;
}
|
在你安装的rpm-4.0那个文件夹里应该有个lib文件夹吧?
看看有没有rpm-4.0/lib/pkgconfig这个文件夹?
如:/usr/lib/rpm-4.0/pkgconfig
有的话,执行一下:export PKG_CONFIG_PATH=/usr/lib/rpm-4.0/pkgconfig:$PKG_CONFIG_PATH
然后编译是用gcc -o app app.c `pkg-config --libs --cflags rpm-4.0`编译.
看看有没有rpm-4.0/lib/pkgconfig这个文件夹?
如:/usr/lib/rpm-4.0/pkgconfig
有的话,执行一下:export PKG_CONFIG_PATH=/usr/lib/rpm-4.0/pkgconfig:$PKG_CONFIG_PATH
然后编译是用gcc -o app app.c `pkg-config --libs --cflags rpm-4.0`编译.
|
直接加 -l库名 试试?
|
直接加 -l库名 -L路径
|
直接加 -l库名 -L路径, 可能是路径没有指正确
|
使用 pkg-config --libs --cflags rpm-4.0
需要找到rpm对应的*.pc配置文件 才可以的
需要找到rpm对应的*.pc配置文件 才可以的