源程式如下
#include <stdio.h>
#include <libvirt/libvirt.h>
int getDomainInfo(int id) {
virConnectPtr conn = NULL; /* the hypervisor connection */
virDomainPtr dom = NULL; /* the domain being checked */
virDomainInfo info; /* the information being fetched */
/* NULL means connect to local QEMU/KVM hypervisor */
conn = virConnectOpenReadOnly(NULL);
if (conn == NULL)
{
fprintf(stderr, "Failed to connect to hypervisor\n");
return 1;
}
/* Find the domain by its ID */
dom = virDomainLookupByID(conn, id);
if (dom == NULL) {
fprintf(stderr, "Failed to find Domain %d\n", id);
virConnectClose(conn);
return 1;
}
/* Get virDomainInfo structure of the domain */
if (virDomainGetInfo(dom, &info) < 0) {
fprintf(stderr, "Failed to get information for Domain %d\n", id);
virDomainFree(dom);
virConnectClose(conn);
return 1;
}
/* Print some info of the domain */
printf("Domain ID: %d\n", id);
printf(" vCPUs: %d\n", info.nrVirtCpu);
printf(" maxMem: %d KB\n", info.maxMem);
printf(" memory: %d KB\n", info.memory);
if (dom != NULL)
virDomainFree(dom);
if (conn != NULL)
virConnectClose(conn);
return 0;
}
int main(int argc, char **argv)
{
int dom_id = 3;
printf("—–Get domain info by ID via libvirt C API —–\n");
getDomainInfo(dom_id);
return 0;
}
進入eshell里面編譯之后,出現如下錯誤
/tmp/ccy8wXgz.o: In function `getDomainInfo':
qwe.c:(.text+0x21): undefined reference to `virConnectOpenReadOnly'
qwe.c:(.text+0x6b): undefined reference to `virDomainLookupByID'
qwe.c:(.text+0xa1): undefined reference to `virConnectClose'
qwe.c:(.text+0xbe): undefined reference to `virDomainGetInfo'
qwe.c:(.text+0xed): undefined reference to `virDomainFree'
qwe.c:(.text+0xf9): undefined reference to `virConnectClose'
qwe.c:(.text+0x17a): undefined reference to `virDomainFree'
qwe.c:(.text+0x18d): undefined reference to `virConnectClose'
collect2: ld returned 1 exit status
/root/Desktop #
我的頭檔案已經呼叫,而且確定包含了這些引數的。這是怎么回事而?
uj5u.com熱心網友回復:
你怎么編譯的?有沒有加-lvirt這個引數?轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/98747.html
標籤:虛擬化
