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

调用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

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

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/terrytian88/p/6656866.html