解压libvirt-0.9.11.3.tar.gz
cd libvirt-0.9.11.3
./configure
出现:
checking libxml2 xml2-config >= 2.6.0..
则:
sudo apt-get install libxml2 libxml2-dev
configrue: error: you must install the gnutls library in order to compile and run libvirt
sudo apt-get install gnutls-doc gnutls-bin
sudo apt-get install libneon27-gnutls libcurl4-gnutls-dev
又提示缺少device-mapper-devel
从网上下载device-mapper.1.02.28.tgz,然后解压缩tar -zxvf device-mapper.1.02.28.tgz,
进入解压后的文件夹中:cd device-mapper.1.02.28。安装device-mapper。
(1) ./configure
(2) sudo make
(3) sudo make install
然后退出该文件夹,重新安装libvirt
You must install python-devel to build Python bindings
sudo apt-get install python-dev
configure: error: libnl-devel >= 1.1 is required for macvtap support
sudo apt-get install libnl-dev
最后:
sudo make
sudo make install
启动libvirt后台进程
sudo libvirtd &
更新动态链接库缓存
sudo ldconfig
c语言库的使用
编辑好.c源码(例如myxm.cpp)后,在命令行下编译 gcc myxm.c -lvirt -o myxm
然后运行./myxm即可
例子:
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
int main()
{
printf("Hello World!\n");
virConnectPtr conn=NULL;
conn=virConnectOpenReadOnly(NULL);
if(conn==NULL)
{
printf("conn is null!\n");
}
else
{
printf("connecting is successful!\n");
}
return 0;
}
使用过程出现的问题:
解决error while loading shared libraries: libvirt.so.0
第一种:
定义shell变量 LD_LIBRARY_PATH
$ LD_LIBRARY_PATH=/usr/local/lib
$ export LD_LIBRARY_PATH
$ ./example
第二种:
修改系统动态链接库配置文件
(the system dynamic linker configuration)
进入目录/etc/ld.so.conf.d
添加一个文件libvirt.conf,内容是:
# libvirt default configuration
/usr/local/lib
更新动态链接库缓存
sudo ldconfig
sudo apt-get install libxml2 libxml2-dev gnutls-doc gnutls-bin libneon27-gnutls libcurl4-gnutls-dev python-dev libnl-dev