LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-12-2009, 05:17 AM   #1
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Rep: Reputation: 15
Compiling libpcap files


// start

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h> /* GIMME a libpcap plz! */
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
char *dev; /* name of the device to use */
char *net; /* dot notation of the network address */
char *mask;/* dot notation of the network mask */
int ret; /* return code */
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netp; /* ip */
bpf_u_int32 maskp;/* subnet mask */
struct in_addr addr;

/* ask pcap to find a valid device for use to sniff on */
dev = pcap_lookupdev(errbuf);

/* error checking */
if(dev == NULL)
{
printf("%s\n",errbuf);
exit(1);
}

/* print out device name */
printf("DEV: %s\n",dev);

/* ask pcap for the network address and mask of the device */
ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);

if(ret == -1)
{
printf("%s\n",errbuf);
exit(1);
}

/* get the network address in a human readable form */
addr.s_addr = netp;
net = inet_ntoa(addr);

if(net == NULL)/* thanks Scott :-P */
{
perror("inet_ntoa");
exit(1);
}

printf("NET: %s\n",net);

/* do the same as above for the device's mask */
addr.s_addr = maskp;
mask = inet_ntoa(addr);

if(mask == NULL)
{
perror("inet_ntoa");
exit(1);
}

printf("MASK: %s\n",mask);

return 0;
}

// end

I try to compile the file (ldev.c) in fedora 7 using the command
>> gcc ldev.c -lpcap but the following errors have occcured :-

ldev.c:3:46: error: pcap.h: No such file or directory
ldev.c: In function ‘main’:
ldev.c:15: error: ‘PCAP_ERRBUF_SIZE’ undeclared (first use in this function)
ldev.c:15: error: (Each undeclared identifier is reported only once
ldev.c:15: error: for each function it appears in.)
ldev.c:16: error: ‘bpf_u_int32’ undeclared (first use in this function)
ldev.c:16: error: expected ‘;’ before ‘netp’
ldev.c:17: error: expected ‘;’ before ‘maskp’
ldev.c:21: warning: assignment makes pointer from integer without a cast
ldev.c:34: error: ‘netp’ undeclared (first use in this function)
ldev.c:34: error: ‘maskp’ undeclared (first use in this function)

what should i do to remove the errors ?
 
Old 04-12-2009, 06:36 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,518

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
No 'libpcap-devel' installed ? ?

Example address, find a 'libpcap-devel', that matches
the installed 'libpcap'
http://rpm.pbone.net/index.php3

> "Search" > > Advanced Search, Fedora 7
.....

To know the installed version :
'rpm -qa | grep libpcap'
.....
 
Old 04-12-2009, 01:08 PM   #3
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
I have formatted my pc And have now installed RHEL4.

I have used the command "rpm -qa | grep libpcap"
which shows libpcap-0.8.3-10.RHEL4 .

Afterthat when I use the command "gcc ldev.c -lbcap" .
It shows something like this :

/usr/bin/ld: cannot find -lbcap
collect2: ld returned 1 exit status
 
Old 04-12-2009, 01:21 PM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,518

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Quote:
/usr/bin/ld: cannot find -lbcap
The lpcap = libpcap used for linking (ld) is 'libpcap.so'
which is from the package 'libpcap-devel'

( 'pcap.h' is also in 'libpcap-devel' )

Redhat EL4 : Install 'libpcap-devel' with the package manager.
.....
 
Old 04-12-2009, 01:35 PM   #5
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
when I tried to install 'libpcap-devel' with the package manager.

It says that package libcap-devel-1.10-20 is already installed .
 
Old 04-12-2009, 01:53 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mridulj View Post
Afterthat when I use the command "gcc ldev.c -lbcap"
It is gcc ldev.c -lpcap not -lbcap.
 
Old 04-12-2009, 02:05 PM   #7
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
Thankful to colucix ,

I have corrected my error and its working fine .

when I use the command "gcc ldev.c -lbcap" and a.out
is created which I need to run as "./a.out" .

Is there a command by which I can directly get the output at
the terminal ?
 
Old 04-12-2009, 02:22 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mridulj View Post
Is there a command by which I can directly get the output at the terminal ?
What do you mean? Which output? The printf statements in the C code should send the output to the terminal (standard output).
 
Old 04-12-2009, 02:31 PM   #9
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
yes I know that the printf in the c code will send the output to the
terminal .

but I doesn't want to run the outputfile as "./a.out" instead
I want that when I use the command "gcc ldev.c -lpcap" the output in the terminal after that should be

DEV: eth0
NET: 192.168.1.0
MASK: 255.255.255.0
 
Old 04-12-2009, 02:40 PM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
gcc is just used to compile, not run programs. You could do "gcc ldev.c -lpcap; ./a.out", but that really isn't any different.
 
Old 04-12-2009, 03:24 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mridulj View Post
but I doesn't want to run the outputfile as "./a.out"
As Nylex already pointed out, you cannot compile and run a program at the same time. You can eventually give a different name to the executable and put it in some directory specified in your PATH, e.g. $HOME/bin. In this way you can type the name of the executable without the leading ./. For example:
Code:
$ gcc ldev.c -o $HOME/bin/ldev -lpcap
$ ldev
DEV: eth0
NET: 192.168.1.0
MASK: 255.255.255.224
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error compiling a program using libpcap uma mahesh Linux - Networking 14 03-06-2009 03:43 AM
libpcap error while compiling ntop from source noir911 Linux - Server 1 07-24-2008 02:08 AM
Compiling files tekmann33 Linux - Newbie 6 06-24-2008 10:31 AM
Compiling c++ files :( gizmo_thunder Programming 6 10-23-2007 12:57 PM
Compiling files bacloff Linux - Software 3 08-27-2003 08:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration