ubuntu下使用eclipse调用libpcap库编写一个小程序时出的错误:pcap.h没有参与编译
2011-11-09 12:44
513 查看
这段程序是winpcap或者libpcap编程者第一次要尝试的东东,仿佛就是helloworld之余C,但还是让我挠头了一天。
情况:
【1】原本是winpcap介绍中用到的程序,由于winpcap与libpcap的兼容性,我就从win32下拿到了ubuntu下,当然也是有变化:
【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 感谢!
如果再遇到这种编译需要的参数,你知道向那里加了么,呵呵。
#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 感谢!
如果再遇到这种编译需要的参数,你知道向那里加了么,呵呵。
相关文章推荐
- Ubuntu下使用Eclipse编译pthread程序错误undefined reference to `pthread_create'
- Ubuntu下使用Eclipse编译、调试嵌入式程序的方法(上)
- Ubuntu系统下的Hadoop集群(4)_使用Eclipse编译运行MapReduce程序
- Ubuntu 下使用Eclipse编译、调试嵌入式程序的方法(中)
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
- 使用Eclipse编译运行MapReduce程序 Hadoop2.6.0_Ubuntu/CentOS
- Ubuntu 下使用Eclipse编译、调试嵌入式程序的方法(下)
- ubuntu下使用Eclipse CDT插件编写多线程程序
- ubuntu安装多个qt版本--不同qt版本编译同一个程序时出现错误--解决方案
- 2. 编写一个程序,从标准输入读取几行输入。每行输入都要打印到标准输出上,前面加上行号。在编写这个程序的时候要使用让这个程序能够处理的输入行的长度没有限制
- 使用Eclipse编译运行MapReduce程序 Hadoop2.6.0_Ubuntu/CentOS
- 使用Eclipse编译运行MapReduce程序_Hadoop2.6.0_Ubuntu/CentOS
- 使用Eclipse编译运行MapReduce程序_Hadoop2.6.0_Ubuntu/CentOS
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
- 使用Eclipse编译运行MapReduce程序 Hadoop2.7.1/Ubuntu
- 1.2.在linux环境实践,使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。并熟悉相关指令。
- 【Linux】使用vim编写一个程序,gcc查看预处理、编译、汇编、链接过程
- 使用Eclipse编译运行MapReduce程序 Hadoop2.6.0_Ubuntu/CentOS
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误