您的位置:首页 > 编程语言 > Java开发

ubuntu下使用eclipse调用libpcap库编写一个小程序时出的错误:pcap.h没有参与编译

2011-11-09 12:44 513 查看
这段程序是winpcap或者libpcap编程者第一次要尝试的东东,仿佛就是helloworld之余C,但还是让我挠头了一天。

#include "pcap.h"
int main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* 获取本地机器设备列表 */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
        return 1;
    }

    /* 打印列表 */
    for(d= alldevs; d != NULL; d= d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if (i == 0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return 1;
    }

    /* 不再需要设备列表了,释放它 */
    pcap_freealldevs(alldevs);
    return 1;
}


情况:

【1】原本是winpcap介绍中用到的程序,由于winpcap与libpcap的兼容性,我就从win32下拿到了ubuntu下,当然也是有变化:

pcap_findalldevs函数在win32下已经加了_ex,you know what i mean,但是libpcap下还是上文给出的样子。
【2】 直接gcc -o getdevs getdevs.c -lpcap 运行ok,./getdevs也成功显示了网卡及描述;

【3】问题是:我把源码放到eclipse的c project中,就不行了(以前没怎么用过),这次算领教了。

明明在源文件中点击pcap.h都能链接到头文件,但是下面的相关函数和变量都表示undefined reference, 邪了门了。

当然,我们看上面得gcc编译也发现还有一个参数-lpcap,但是在eclipse中这个参数怎么加呢?

直接在run as..中加是不行的,run的前提是你build要通过,这里build都通不过,那就是到项目Properties中添加:

我个人的艰辛就不说了,直接说找来的答案了吧:

I had the same problem in Eclipse. I order to link this library to your project, right click the project in "Project Explorer" (short view) and choose "Properties". On the left go to "C/C++ Build" > "Settings" and on the "Settings" panel go to "Tool Settings"
> "GCC C Linker" (if this is your compiler) > "Libraries" > and add the word "pcap" in the list titled "Libraries (-l)". Press "Apply" on the bottom and then "OK". Click "Project" > "Build All" and this should do the job. It should be no errors now. :)

答案来自:http://ubuntuforums.org/archive/index.php/t-125444.html 感谢!

如果再遇到这种编译需要的参数,你知道向那里加了么,呵呵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐