调用libpci库出现的问题和解决方法

 

本方案以pciutils-3.5.1为例.

 

1. 从以下地址下载pciutils-3.5.1.tar.xz

https://www.kernel.org/pub/software/utils/pciutils/

2. 然后

[root@localhost terry]# xz –d pciutils-3.5.1.tar.xz

[root@localhost terry]tar xvf pciutils-3.5.1.tar

[root@localhost terry]# cd pciutils-3.5.1

[root@localhost pciutils-3.5.1]# make

就会发现在

[root@localhost pciutils-3.5.1]# ls lib/libpci.a

lib/libpci.a

**注意***

如果你需要动态的连接库,你只需要将Makefile改为

SHARED=yes

3. 写好自己的代码, 比如我写的如下:

#include
"pci.h"

 

bool PCIeVdp::getPCIConfig(PCI_CONFIG *pci_cfg, PCI_LOCATION *loc, int &pciCount)

{

    struct pci_access *pacc;

    struct pci_dev *dev;

    unsigned
char data[256];

 

    pciCount = 0;

    memset(data, 0, sizeof(data));

    pacc = pci_alloc();        /* Get the pci_access structure */

    /* Set all options you want -- here we stick with the defaults */

    pci_init(pacc);        /* Initialize the PCI library */

    pci_scan_bus(pacc);        /* We want to get the list of devices */

    for (dev=pacc->devices; dev; dev=dev->next)    /* Iterate over all devices */

    {

        pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);    /* Fill in header info we need */

        for(int i=0;i<256;i++)

        {

            data[i] = pci_read_byte(dev, i);

        }

        memcpy((unsigned
char *)&pci_cfg[pciCount], data, 256);

        loc[pciCount].bus_num = dev->bus;

        loc[pciCount].dev_num = dev->dev;

        loc[pciCount].func_num = dev->func;

        pciCount++;

    }

    pci_cleanup(pacc);        /* Close everything */

    return
true;

}

 

需要将pciutils-3.5.1/lib下的pci.h, header.h, types.h以及libpci.a 拷贝到你的程序文件相同的目录.

4.运行make去编译,发现出现以下问题.

折腾了下,发现是因为libpci.a这个库是用C语言写的,而我的程序是用C++写的, 于是在pci.h的头和尾加上

#ifdef __cplusplus

extern
"C" {

#endif

 

#ifdef __cplusplus

}

#endif

 

问题解决.

5. 可是新的问题有出现了

查了下发现是我的makefile中没有加载libz,于是修改makefile如下

LIB+=-ldl -lz -lpthread ./libpci.a

发现问题解决,编译顺利通过.

 

 

 

 

 

 

 

 

 

 

最新文章

  1. aws在线技术峰会笔记-游戏解决方案
  2. XAF应用开发教程(三)业务对象模型之引用类型与关联关系
  3. Hibernate理论
  4. sharepreferce支持boolean,string类型
  5. 设置IE浏览器代理上网
  6. 建索引让SQL飞起来
  7. uboot从SD卡烧写内核和文件系统
  8. flash播放器遮挡页面中元素问题解决
  9. 设计一个有getMin功能的栈(2)
  10. DML数据操作语言之查询(二)
  11. 编程之美2.18 数组分割 原创解O(nlogn)的时间复杂度求解:
  12. CSS&amp;JS小结
  13. mybatis-plus报org.apache.ibatis.binding.BindingException分析【转载】
  14. C# -- 使用XmlDocument或XDocument创建xml文件
  15. vim鼠标模式打开与关闭
  16. 2156: 中南大学2018年ACM暑期集训前期训练题集(入门题) D: 机器人的指令
  17. async和await用法(Task)
  18. linux grep 正则表达式
  19. (转)mysql创建表时反引号的作用
  20. Docker深入浅出1

热门文章

  1. codeforces#505--B Weakened Common Divisor
  2. 数据去重优化 MemoryError 内存不足
  3. 验证ip地址
  4. Mongodb3.X版本的 的用户认证
  5. SprinBoot CLI 安装(Mac版)
  6. Unity-DLL反编译学习
  7. android 异步线程刷新UI 以及 JSON解析 以及 url get请求
  8. centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
  9. poi根据模板导出word文档
  10. sipp保持sip协议的tcp长连接