C in jbase

55 views
Skip to first unread message

jayasree rajendran

unread,
May 16, 2007, 2:46:15 AM5/16/07
to jB...@googlegroups.com
Hi all,
 
We are trying to invoke C program in JBASE.
While compiling the C program in jbase, the following warnings came...(only while compiling thro' jbase, these warnings are coming)

What should be done to avoid these warnings?

testsendmsg.c:41: warning: implicit declaration of function `atoi'

testsendmsg.c:49: warning: passing arg 1 of `gethostbyname' from incompatible pointer type

testsendmsg.c:52: warning: implicit declaration of function `exit'

testsendmsg.c:70: warning: passing arg 2 of `strcpy' from incompatible pointer type

testsendmsg.c:72: warning: implicit declaration of function `write'

testsendmsg.c:76: warning: implicit declaration of function `close'

testsendmsg.c:79: warning: return from incompatible pointer type

 

waiting for a reply.

 

Greg Cooper

unread,
May 16, 2007, 4:18:21 AM5/16/07
to jB...@googlegroups.com
It looks like some standard header files are missing and are rudimentary 'C' coding errors.
 
Maybe you can post the entire source, as these snippets don't really give much to do on. Also, the command you used to compile the program would be useful.

jayasree

unread,
May 16, 2007, 6:09:32 AM5/16/07
to jB...@googlegroups.com
 
Hi greg,
 
These warnings are coming only when, invoking through jbase.
The following header files are added in the program and we feel that these warnings are not related to header files(and something to do with jbase level) since it worked fine in C.
We used 'jcompile -c test.c' for compiling the program.
 
Also how to get a char type as an argument instead of VAR?
 
test.c
 
#include <jsystem.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
 
VAR *testsendmsg(VAR *ipaddress,VAR *port,VAR *message)
{
 
int sockfd, portno, n;
VAR ipadd;
unsigned char* pipadd;
 
STRING_VBS(ipaddress,pipadd);
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
 
portno = atoi(port);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket");
else
{
 printf("%s ip is:",&ipadd);
 server = gethostbyname(ipaddress);
 if (server == NULL) {
 fprintf(stderr,"ERROR, no such host\n");
 exit(0);
}
 
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
 
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
 printf("ERROR connecting");
else
{
 bzero(buffer,4096);
 strcpy(buffer,message);
 printf("message :%s\n", buffer);
 n = write(sockfd,buffer,strlen(buffer));
 if (n < 0)
 printf("ERROR writing to socket");
 close(sockfd);
}
}
return(buffer);

Greg Cooper

unread,
May 16, 2007, 7:33:19 AM5/16/07
to jB...@googlegroups.com
This is going to take some time.... Here goes....
 
(1) Because you've used jcompile.c , I assume you are on release 4.1 or higher? If so the function header needs to change from
 
    VAR *testsendmsg(VAR *ipaddress,VAR *port,VAR *message)
 
To
 
    VAR *testsendmsg(DPSTRUCT * dp , VAR *ipaddress,VAR *port,VAR *message)
 
 
(2) The header file jsystem.h includes only those headers needed to compile BASIC code (it converts BASIC to 'C' and then compiles the 'C') and a sprinkling of functions and macros for user-defined 'C' programming. It isn't absolutely everything you always need for every single 'C' function imaginable ! So, the warnings.
 

testsendmsg.c:41: warning: implicit declaration of function `atoi'

testsendmsg.c:52: warning: implicit declaration of function `exit'

testsendmsg.c:72: warning: implicit declaration of function `write'

testsendmsg.c:76: warning: implicit declaration of function `close'


are because you are missing #include files. To find which ones you need, use a Unix shell command such as this

   % man atoi

and you'll see what include files you need to add. In this case, you need to add

#include <stdlib.h>

#include <unistd.h>

 

or at least you did on my machine.

 

(3) I am not sure what you are trying to do with

 

   STRING_VBS(ipaddress , pipadd)

 

It doesn't make any sense. It throws up an error on my machine for two reasons

(a) There is no STRING_VBS macro

(b) You are trying to put executable code in between variable declarations, you can do this in C++ but not in C.

 

(4) You have the statement

 

   portno = atoi(port)

 

The atoi() function passes a char * as the first argument. See 'man atoi'. You are passing it a 'VAR *' argument. Not only do you get a warning, but this will never work.

 

There are two ways of solving this

 

(a) Ask jBASE to return a string which you can pass to atoi() like this

     portno = atoi(CONV_CFB(port));

The jBASE macro atoi() will cause the VAR to return the string equivalent of whatever is stored in 'port'.

 

(b) Ask jBASE to return an integer directly like this

    portno = CONV_IB(port)

 

(5) You had these warning messages

   

    testsendmsg.c:49: warning: passing arg 1 of `gethostbyname' from incompatible pointer type

    testsendmsg.c:70: warning: passing arg 2 of `strcpy' from incompatible pointer type

 

These are the same problems as (4), you are not passing a char * as a parameter, use CONV_CFB ().

 

I think you need to properly read and understand the jBASE to 'C' interface before progressing further. You seem to have jumped in at the deep end without any real understanding of how you interface jBASE and 'C'. I don't want to be unkind, but it does appear your 'C' experience is somewhat limited, maybe I'm wrong on this, but I do get the impression you are trying to do something which is currently beyond your understanding. Really, you need to read any jBASE documentation on calling C from jBASE.

jayasree rajendran

unread,
May 16, 2007, 8:19:18 AM5/16/07
to jB...@googlegroups.com
Thanks for your explanation Greg.
 

Jim Idle

unread,
May 16, 2007, 4:32:25 PM5/16/07
to jB...@googlegroups.com

You really need more experience with C basically I am afraid, but I will try to help. These are standard warnings about your code due to:

 

1)      Although include <jsystem.h> will include most of the useful header files, you may still need others. Hence the implicit declarations. You don’t say what platform you are running, but look up the atoi() and read() etc  functions and find out what header file this is included in. Then #include this in your code;

2)      Warnings of incompatible types are either because you really are using the wrong pointer, or your calling spec is not an exact match for the function prototype. In the cases below I suggest you are passing a char * or jBASE STRING typedef and  need to cast to unsigned char * or the other way around.

 

Jim

Jim Idle

unread,
May 16, 2007, 4:37:56 PM5/16/07
to jB...@googlegroups.com

Greg is right, it is your code.

 

jBASE compiles at a higher warning level than default and so you are not getting the warnings. Specifically, you need to cast the types and include the relevant header files. You should do this anyway and should compile at the highest warning levels available, fixing up the warnings. Occasionally warnings can be too pedantic, but that is what #pragma is for – in general the warnings will only improve your code.

 

Jim

 

From: jB...@googlegroups.com [mailto:jB...@googlegroups.com] On Behalf Of jayasree
Sent: Wednesday, May 16, 2007 3:10 AM
To: jB...@googlegroups.com
Subject: RE: C in jbase

 

 

Hi greg,

<BR

Reply all
Reply to author
Forward
0 new messages