编译 C++ 时包含外部库的问题程序
我在 Windows 7 上使用 Dev C++ 和 WinPcap(开发人员包)。 Dev c++ 显然无法找到 pcap.h,即使我在项目选项中包含 /include/ 目录,但在编译时它会显示错误“pcap.h:没有这样的文件或目录”。 (以及许多其他错误)。 这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>
int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
return 0;
}
我保持简单。我最初在 Visual Studio (C++) 中工作,但分发使用 Visual C++ 编译的代码需要在目标系统上安装 Microsoft C 运行时库。我只是希望能够分发最终的可执行文件并使其在任何计算机上运行。
我检查了提供给编译器的命令行。它确实有-I [路径]选项。那么我有什么遗漏的吗?
附带说明:我用 g++(来自 dev c++ 安装目录)编译了上面的代码,并且它编译正确。但是当我尝试链接它时,生成的可执行文件在运行时崩溃了。
I'm using Dev C++ on windows 7, and WinPcap (developer's pack). Dev c++ is not able to find pcap.h apparently, even though I include the /include/ directory in project options, on compilation it displays an error saying "pcap.h: no such file or directory." (along with many other errors).
Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>
int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
return 0;
}
I've kept it simple. I was originally working in Visual Studio (C++), but distributing code compiled with Visual C++ requires Microsoft C Runtime library to be installed on the target system. I just want to be able to distribute the final executable and get it to work on any machine.
I checked the commmand line given to compiler. It did have the -I [path] option. Well is there anything I'm missing?
As a side note: I had compiled the above code with g++ (from dev c++ installation dir), and it compiled correctly. But when I tried to link it, the executable produced, just crashed on being run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题有点不清楚,但你的旁注听起来好像你可以编译它(即找到了 pcap.h 标头),并且你的问题与链接有关。
要将目录添加到库的搜索路径中,请使用
-LPATH
,其中PATH
是包含libpcap
的实际目录。要实际将其添加到链接中,请在链接器调用中使用-lpcap
,例如Your question is a little unclear, but your side-note makes it sound as if you could compile this (i.e. the
pcap.h
header was found) and your issues is with linking.To add directories to the search path for libraries use
-LPATH
wherePATH
is the actual directory containinglibpcap
. To actually add it to the link use-lpcap
in the linker call, e.g.