473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

`sock_create' undeclared (first use this function)

Hello,
when I try to compile the following code with g++ -o client client.c
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>

#define ADDRESS "mysocket";
#define MAXLEN 200;

int main(int argc, char *argv)
{
int sd;
struct sockaddr_un sa;
int length;
char netmsg[MAXLEN];

if((sd=sock_create(AF_UNIX, SOCK_STREAM, 0))<0){
perror("Cannot create Socket");
exit(1);
}
sa.sun_family=AF_UNIX;
sa.sun_path=ADDRESS;
length=sizeof(sa.sun_family)+strlen(sa.sun_path);

if(bind(sd, (sockaddr_un*)&sa, length)<0){
perror("Couldn´t bind socket");
exit(1);
}
if(connect(sd, (sockaddr_un*)&sa, length)<0){
perror("Cannot connect to server");
exit(1);
}
if(send(sd, (char*) netmsg, MAXLEN)<0, 0){
perror("Cannot send message over network");
exit(1);
}
if(recv(sd, (char*) netmsg, MAXLEN, 0)<0){
perror("Message Receive failed");
exit(1);
}
if(close(sd)<0){
perror("Cannot close socket");
exit(1);
}

}

I get this message from the compiler:

client.c: In function `int main(int, char*)':
client.c:11: error: aggregate `sockaddr_un sa' has incomplete type and
cannot be defined
client.c:13: error: expected primary-expression before "char"
client.c:13: error: expected `;' before "char"
client.c:13: error: expected primary-expression before ']' token
client.c:13: error: expected `;' before ']' token
client.c:15: error: `sock_create' undeclared (first use this function)
client.c:15: error: (Each undeclared identifier is reported only once
for each function it appears in.)
client.c:21: error: `strlen' undeclared (first use this function)
client.c:31: error: `netmsg' undeclared (first use this function)
client.c:31: error: expected `)' before ';' token
client.c:31: error: expected `)' before ';' token
client.c:31: error: expected primary-expression before ')' token
client.c:31: error: expected `;' before ')' token
client.c:35: error: expected `)' before ';' token
client.c:35: error: expected `)' before ';' token
client.c:35: error: expected primary-expression before ',' token
client.c:35: error: expected `;' before ')' token
client.c:39: error: `close' undeclared (first use this function)
Can anybody help me what I have done wrong? I have no idea what to do.
It would be nice if somebody could help me to solve this problem.

Thanks,
Peter
Dec 16 '05 #1
6 9859

"Peter Rothenbuecher" <pr******@hrz.uni-frankfurt.de> wrote in message
news:40*************@news.dfncis.de...
Hello,
when I try to compile the following code with g++ -o client client.c
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>

#define ADDRESS "mysocket";
#define MAXLEN 200;

int main(int argc, char *argv)
{
int sd;
struct sockaddr_un sa;

^^^^^^^

The first problem you should tackle is to remove that struct statement here.
It should read "sockaddr_un sa" only to create an object of type sockadd_un.
Then recompile and look if there are other problems. Furthermore, I'd
suggest to remove those old legacy headers and replace them with standard
C++ ones, namely <cstdio> and <cstdlib>

[SNIP]

HTH
Chris
Dec 16 '05 #2
Peter Rothenbuecher wrote:
Hello,
when I try to compile the following code with g++ -o client client.c
#include<sys/socket.h>
This is a non-standard header and is thus off-topic here. You should
post in a newsgroup for your platform. See the FAQ for what is on-topic
here and for some suggestions for better newsgroups to post in:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
#include<stdio.h>
#include<stdlib.h>
If you're doing C++ rather than C (which we assume since you're posting
here but which the coding style doesn't reflect), prefer:

#include <cstdio>
#include <cstdlib>

Better, prefer <iostream> and other C++ standard library headers.

#define ADDRESS "mysocket";
#define MAXLEN 200;
You probably don't want semicolons after these lines.

int main(int argc, char *argv)
You don't use argc and argv. Drop them.
{
int sd;
struct sockaddr_un sa;
int length;
char netmsg[MAXLEN];

[snip]

First, in C++, you should not declare variables until you can
initialize and use them. IOW, don't put all the declarations at the
top.

Second, it looks like sockaddr_un is undefined. Look in your
non-standard header <sys/socket.h> for the function prototype for
bind(), and you'll see what it should be called (probably just
sockaddr).

Cheers! --M

Dec 16 '05 #3

mlimber wrote:
Peter Rothenbuecher wrote:
#include<stdio.h>
#include<stdlib.h>


If you're doing C++ rather than C (which we assume since you're posting
here but which the coding style doesn't reflect), prefer:

#include <cstdio>
#include <cstdlib>


If you're suggesting that because <cxxx> headers are supposed to put C
library names in the std namespace rather than the global namespace, do
you know of any implementations that actually do that? Every time I've
tried it, I've been able to #include <cstdio> and use ::printf, which
is incorrect (I've tried this recently with VC++8, Comeau online and
whatever compiler is under the latest release of Dev-C++).

While violating the actual standard, this behaviour does seem to be the
de facto standard. Because <cxxx> headers let me compile technically
incorrect code becuase names are in the global namespace and the std
namespace, I prefer <xxx.h>. It might be deprecated, but I know my code
is correct.

Gavin Deane

Dec 16 '05 #4
Peter Rothenbuecher wrote:
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
Also, add sys/un.h for sockaddr_un and
unistd.h for close().
#define ADDRESS "mysocket";
#define MAXLEN 200;
Remove the semi-colons.
if((sd=sock_create(AF_UNIX, SOCK_STREAM, 0))<0){
This should probably be socket(), not sock_create().
sa.sun_path=ADDRESS;
You will have to strcpy() this.
if(bind(sd, (sockaddr_un*)&sa, length)<0){
Cast to sockaddr.
if(connect(sd, (sockaddr_un*)&sa, length)<0){
Same goes here.
if(send(sd, (char*) netmsg, MAXLEN)<0, 0){


Do you see the typo?
--
Jan Vidar Krey
Unix Software Developer

Dec 16 '05 #5
de*********@hotmail.com wrote:
mlimber wrote:
Peter Rothenbuecher wrote:
#include<stdio.h>
#include<stdlib.h>


If you're doing C++ rather than C (which we assume since you're posting
here but which the coding style doesn't reflect), prefer:

#include <cstdio>
#include <cstdlib>


If you're suggesting that because <cxxx> headers are supposed to put C
library names in the std namespace rather than the global namespace, do
you know of any implementations that actually do that? Every time I've
tried it, I've been able to #include <cstdio> and use ::printf, which
is incorrect (I've tried this recently with VC++8, Comeau online and
whatever compiler is under the latest release of Dev-C++).

While violating the actual standard, this behaviour does seem to be the
de facto standard. Because <cxxx> headers let me compile technically
incorrect code becuase names are in the global namespace and the std
namespace, I prefer <xxx.h>. It might be deprecated, but I know my code
is correct.

Gavin Deane


Yes, my Texas Instruments C++ compiler puts all standard library
symbols in the std namespace only. The <cxxx> headers simply include
the <xxx.h> header which have something like this at the top:

#ifdef __cplusplus
//----------------------------------------------------------------------------
// <cstdlib> IS RECOMMENDED OVER <stdlib.h>. <stdlib.h> IS PROVIDED
FOR
// COMPATIBILITY WITH C AND THIS USAGE IS DEPRECATED IN C++
//----------------------------------------------------------------------------
extern "C" namespace std {
#endif /* !__cplusplus */

On this compiler, this code fails to compile without the "std::":

#include <cstdlib>

int main()
{
return std::rand();
}

Several (incorrect) implementations I have seen elsewhere just put
using declarations in the std namespace, thus providing C++
compatibility but leaving the global namespace polluted.

Cheers! --M

Dec 16 '05 #6
mlimber wrote:
de*********@hotmail.com wrote:
mlimber wrote:
<snip advice to prefer <cxxx> over <xxx.h> >
If you're suggesting that because <cxxx> headers are supposed to put C
library names in the std namespace rather than the global namespace, do
you know of any implementations that actually do that? Every time I've
tried it, I've been able to #include <cstdio> and use ::printf, which
is incorrect (I've tried this recently with VC++8, Comeau online and
whatever compiler is under the latest release of Dev-C++).

While violating the actual standard, this behaviour does seem to be the
de facto standard. Because <cxxx> headers let me compile technically
incorrect code becuase names are in the global namespace and the std
namespace, I prefer <xxx.h>. It might be deprecated, but I know my code
is correct.

Gavin Deane
Yes, my Texas Instruments C++ compiler puts all standard library
symbols in the std namespace only. The <cxxx> headers simply include
the <xxx.h> header which have something like this at the top:

#ifdef __cplusplus
//----------------------------------------------------------------------------
// <cstdlib> IS RECOMMENDED OVER <stdlib.h>. <stdlib.h> IS PROVIDED
FOR
// COMPATIBILITY WITH C AND THIS USAGE IS DEPRECATED IN C++
//----------------------------------------------------------------------------
extern "C" namespace std {
#endif /* !__cplusplus */


Interesting. My preference for <xxx.h>, for the reasons I've given, was
affirmed recently in an exchange with P J Plauger in this thread:

http://groups.google.co.uk/group/com...4db1c45cfe8733

He stated[*] that an admittedly simplified version of your ctsdlib

namespace std {
#include <stdlib.h>
}

is wrong, though I never asked him exactly what is wrong with it.

<snip>
Several (incorrect) implementations I have seen elsewhere just put
using declarations in the std namespace, thus providing C++
compatibility but leaving the global namespace polluted.


Indeed that is exactly my point. I wonder if it really is as simple as
Texas Instruments has made it, why some of those other implementations
(includeing Microsoft and Comeau, online at least) haven't managed to
do it.
[*] I don't think I am taking his words out of context, but any
misrepresentation here is my fault. Check the original source for
definitive quotes.

Gavin Deane

Dec 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Ströder | last post by:
HI! I'm trying to build Python2.4 on a rather old Debian machine. I only have a shell account there. That's why I'm very limited in my actions. Building _socket fails (see below) although I...
1
by: learning_C++ | last post by:
Hi, I compiled some code which includs times<int>(). The compiler says "times' undeclared (first use this function)". I also included the header <functional> in my code. But After I replaced...
2
by: kyung seop kim | last post by:
hi! all I updated compiler to gcc 3.4.3 in suse linux machine. I met the error message like subject. how can I fix this problem ?
9
by: W. Van Hooste | last post by:
Just starting with C, can somebody explain why this does not work or point me in the right direction? I wrote some tools and did some coding but cant seem to get this one. I DID declare my FILE...
0
by: Stephanie Doherty | last post by:
Hello World, I am trying to use a _spawnl function like this (and I have included the process.h file): _spawnl(_P_WAIT,iporgfile,iporgfile,NULL); It compiles with the following errors: ...
4
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code: /* This fragment of code is taken from an online tutorial */ #include<stdio.h> #include<fcntl.h> #include<stdlib.h> float bigbuff;
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
7
by: michigaki | last post by:
hello, we are having problems in compiling a 'slightly' altered x264. We are always receiving a 'helloWorld' undeclared (first use this function) error. We may be commiting a very simple error but...
0
by: Eric von Horst | last post by:
Hi, we have a third-party product that has a C++ api on HP-UX. I would like be able to use the API in Python (as I remember Python is good at doing this). I have no experience with this so...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.