The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
undefined functions in g++
Discuss undefined functions in g++ in the C Programming forum on Dev Shed. undefined functions in g++ C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 8th, 2003, 09:23 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
undefined functions in g++
- i get 2 errors for functions that are defined in
sys/socket.h.
Code:
/**************************************************************************
* this is an object oriented client *
**************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#define MAXBUF 1024
#define PORT 9999
struct message{
int ident;
char data[MAXBUF];};
class cOneThrdClient{
private:
int sockfd;
int bytes_read;
struct sockaddr_in dest;
message block;
char serv[40];
public:
cOneThrdClient(char *mess, char *server);
bool connekt();
bool sndMsg();
bool recvMsg();
};
cOneThrdClient::cOneThrdClient(char *mess, char *server){
strcpy(block.data,mess); bytes_read = 0; block.ident = 69;
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Socket");}
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
if ( inet_addr(server, &dest.sin_addr.s_addr) == 0 ){ <-- error in this function call
perror(serv);}
dest.sin_port = htons(PORT);
}
bool cOneThrdClient::connekt(){
if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0 ){
perror("Connect");}
}
bool cOneThrdClient::sndMsg(){
send(sockfd, &block, sizeof(block), 0);
}
bool cOneThrdClient::recvMsg(){
/*---While there's data, read and print it---*/
do{
bzero(&block, sizeof(block));
bytes_read = recv(sockfd, &block, sizeof(block), 0);
if ( bytes_read > 0 )
printf("%s\n", block.data);}
while ( bytes_read > 0 );
/*---Clean up---*/
close(sockfd); <-- error in this function call
}
int main(int Count, char *Strings[])
{
cOneThrdClient *cPtr = new cOneThrdClient(Strings[1], Strings[2]);
cPtr->connekt(); cPtr->sndMsg(); cPtr->recvMsg();
return 1;
}
errors:
cClient.cpp: In constructor `cOneThrdClient::cOneThrdClient(char*, char*)':
cClient.cpp:40: `inet_addr' undeclared (first use this function)
cClient.cpp:40: (Each undeclared identifier is reported only once for each
function it appears in.)
cClient.cpp: In member function `bool cOneThrdClient::recvMsg()':
cClient.cpp:65: `close' undeclared (first use this function)
|

May 9th, 2003, 02:43 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
I don't know anything about sockets, but with file I/O close() is a function of the fstream class. Based on your error, maybe you have to call,
close(sockfd)
on an object.
|

May 9th, 2003, 09:35 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Re: undefined functions in g++
Quote: Originally posted by infamous41md
- i get 2 errors for functions that are defined in
sys/socket.h.
...
errors:
cClient.cpp: In constructor `cOneThrdClient::cOneThrdClient(char*, char*)':
cClient.cpp:40: `inet_addr' undeclared (first use this function)
cClient.cpp:40: (Each undeclared identifier is reported only once for each
function it appears in.)
cClient.cpp: In member function `bool cOneThrdClient::recvMsg()':
cClient.cpp:65: `close' undeclared (first use this function) |
I assume that this is for Linux.
inet_addr() is declared in 3 header files (ie, I think you need all three):
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
close() is declared in unistd.h
Required header files found on the functions' man pages.
Yeah, it is a bit of a pain that UNIX/Linux has the sockets functions and data structures spread out over so many different header files. But then sockets is supposed to be able to support all kinds of protocol families, not just TCP/IP, so I guess they had to split it up.
EDIT:
BTW FWIW, on my Red Hat 7 I just grep'd for close and for inet_addr in sys/socket.h and did not find them. So sys/socket.h is apparently needed for things in netinet/in.h, which is needed for things in arpa/inet.h. inet_addr is declared in arpa/inet.h.
Last edited by dwise1_aol : May 9th, 2003 at 09:44 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|