I recently started learning ZEROMQ and I am stuck somewhere. I tried to run the weather update examples(wuclient.c and wuserver.c) and I get the error below.
In file included from wuclient.c:5:0:
zhelpers.h: In function ‘s_sleep’:
zhelpers.h:133:5: warning: implicit declaration of function ‘nanosleep’ [-Wimplicit-function-declaration]
zhelpers.h: In function ‘s_console’:
zhelpers.h:158:5: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
zhelpers.h:159:12: warning: implicit declaration of function ‘localtime’ [-Wimplicit-function-declaration]
zhelpers.h:159:26: warning: initialization makes pointer from integer without a cast [enabled by default]
zhelpers.h:161:5: warning: implicit declaration of function ‘strftime’ [-Wimplicit-function-declaration]
zhelpers.h:161:5: warning: incompatible implicit declaration of built-in function ‘strftime’ [enabled by default]
wuclient.c: At top level:
zhelpers.h:60:1: warning: ‘s_send’ defined but not used [-Wunused-function]
zhelpers.h:67:1: warning: ‘s_sendmore’ defined but not used [-Wunused-function]
zhelpers.h:75:1: warning: ‘s_dump’ defined but not used [-Wunused-function]
zhelpers.h:115:1: warning: ‘s_set_id’ defined but not used [-Wunused-function]
zhelpers.h:125:1: warning: ‘s_sleep’ defined but not used [-Wunused-function]
zhelpers.h:139:1: warning: ‘s_clock’ defined but not used [-Wunused-function]
zhelpers.h:156:1: warning: ‘s_console’ defined but not used [-Wunused-function]
The command I used to compile it is: gcc -Wall wuclient.c -o wuclient -L/usr/local/lib -lzmq
And this is the zhelpers.h code here https://github.com/imatix/zguide/blob/master/examples/C/zhelpers.h which is causing the error.
And it was included in this code below:
// Weather update client
// Connects SUB socket to tcp://localhost:5556
// Collects weather updates and finds avg temp in zipcode
#include "zhelpers.h"
int main (int argc, char *argv [])
{
// Socket to talk to server
printf ("Collecting updates from weather server...\n");
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
int rc = zmq_connect (subscriber, "tcp://localhost:5556");
assert (rc == 0);
// Subscribe to zipcode, default is NYC, 10001
char *filter = (argc > 1)? argv [1]: "10001 ";
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
filter, strlen (filter));
assert (rc == 0);
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
char *string = s_recv (subscriber);
int zipcode, temperature, relhumidity;
sscanf (string, "%d %d %d",
&zipcode, &temperature, &relhumidity);
total_temp += temperature;
free (string);
}
printf ("Average temperature for zipcode '%s' was %dF\n",
filter, (int) (total_temp / update_nbr));
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}
I opened the "zhelpers.h" file and "time.h" was included. So I got confused on why this would happen. Am using Ubuntu 12.04 and please, I am neither an expert in C or ZEROMQ but this software looks like my realistic hope of scaling my thesis hurdle.
Thanks.
Note that these are just warnings and not errors. The compiler will still generate something but it might not work as intended.
The header file "zhelpers.h" includes <sys/time.h> and not <time.h> on Ubuntu. This is most probably incorrect. Remove the conditional in "zhelpers.h" and just include <time.h> on all platforms. This will remove half of the warnings.
The second half of the warnings are related to the fact that there are function definitions in "zhelpers.h". This is very poor coding style but the program should still work.
Related
To start off, I don't get this issue when I compile/"make" the code on a Linux machine which I connect to remotely. I'm experiencing it only on my Windows laptop with Mingw installed -- which I believe is causing the issue.
$ make
gcc -c parser.c
parser.c:34:7: error: conflicting types for 'gets'
34 | char* gets(char *buf, int max)
| ^~~~
In file included from parser.h:4,
from parser.c:1:
c:\mingw\include\stdio.h:709:41: note: previous declaration of 'gets' was here
709 | _CRTIMP __cdecl __MINGW_NOTHROW char * gets (char *);
| ^~~~
Makefile:13: recipe for target 'parser.o' failed
make: *** [parser.o] Error 1
Here's the gets() code as requested:
char* gets(char *buf, int max)
{
int i, cc;
char c;
for(i=0; i+1 < max; ){
cc = read(0, &c, 1);
if(cc < 1) break;
//c = getchar();
buf[i++] = c;
if(c == '\n' || c == '\r')
break;
}
buf[i] = '\0';
return buf;
}
Is there a way to fix this without changing the gets function name? Thank you sm
Your code works on Linux's gcc because the gets function was removed, as it should, since it was deprecated in the C99 standard and removed with C11.
For some reason the Windows MingW distribution still maintains gets and because of that you have a redefinition problem.
So unfortunately you can't use that function name, unless you remove it by hand from stdio.h, as C doesn't allow for function overloading.
Running sample on Linux gcc
Running sample on Windows gcc
As the error says the gets() function is already defined in stdio.h.
One trick you can do is put something like this:
#define gets MY_gets
before your definition of the gets() function.
That way you are actually defining a MY_gets() function which causes no conflict. And when you call gets() later on in your code you are actually calling MY_gets().
If you define gets() in a header file you should include stdio.h first and then put #define gets MY_gets before the declaration of gets() in the header file.
Though I don't see why you want to refine this function if it already exists.
It makes more sense to only define it if needed and surround the function with something like #ifndef HAVE_GETS and endif where HAVE_GETS should be defined based on tests done in the configure/build system.
I am trying to test writing in a file in Contiki. here is the code I used :
#include "contiki.h"
#include <stdio.h>
#define LEN 256
PROCESS(test_process, "Coffee test process");
AUTOSTART_PROCESSES(&test_process);
PROCESS_THREAD(test_process, ev, data)
/**/
{
PROCESS_BEGIN();
FILE * fp;
int i;
/* open the file for writing*/
fp = fopen ("/home/user/contiki/examples/mySim/1.txt","w");
/* write 10 lines of text into the file stream*/
for(i = 0; i < 10;i++){
fprintf (fp, "This is line %d\n",i + 1);
}
/* close the file*/
fclose (fp);
PROCESS_END();
}
I get this error message after compiling in Cooja simulator:
test.c: In function ‘process_thread_test_process’:
test.c:12:1: error: unknown type name ‘FILE’
test.c:15:4: warning: implicit declaration of function ‘fopen’ [-Wimplicit-function-declaration]
test.c:15:7: warning: assignment makes pointer from integer without a cast [enabled by default]
test.c:19:8: warning: implicit declaration of function ‘fprintf’ [-Wimplicit-function-declaration]
test.c:19:8: warning: incompatible implicit declaration of built-in function ‘fprintf’ [enabled by default]
test.c:23:4: warning: implicit declaration of function ‘fclose’ [-Wimplicit-function-declaration]
make: *** [test.co] Error 1
Process returned error code 2
does anyone has any idea about the problem?
Contiki does not provide/support the POSIX file API, the same way it does not have many other things (POSIX sockets API, POSIX process creation and control API). Instead, it provides its own filesystem API ("protosockets" API, "protothreads" API etc.).
There are two filesystem implementations: CFS (Contiki File System) and Coffee. You can use the functions described in the Wiki page; they are analogues to low-level POSIX file API (e.g. cfs_open is similar to POSIX open, cfs_close to POSIX close and so on). There are no analogues for buffered I/O functionality (fopen, fclose) and the FILE structure does not exist.
I have a problem described in the title. I have an Edify language parser that runs without errors when I building it on arm but fails when I try to use it with x86. I traced segfault to yy_scan_bytes function, more precisely to this code:
YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, int _yybytes_len ) {
YY_BUFFER_STATE b;
char * buf;
yy_size_t n;
int i;
/* Get memory for full buffer, including space for trailing EOB's. */
n = _yybytes_len + 2;
buf = (char *) yyalloc(n );
if ( ! buf ) {
YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
}
for ( i = 0; i < _yybytes_len; ++i ) {
buf[i] = yybytes[i]; // <==========
}
The full code is here: https://github.com/twaik/edify_x86_failing_code
I've got it from AROMA Installer source.
That's everything I discovered after debug. Thanks.
Trying to build your code gives me these errors:
main.c: In function ‘parse_string’:
main.c:27:5: warning: implicit declaration of function ‘yy_switch_to_buffer’ [-W
implicit-function-declaration]
yy_switch_to_buffer(yy_scan_string(str));
^~~~~~~~~~~~~~~~~~~
main.c:27:25: warning: implicit declaration of function ‘yy_scan_string’ [-Wimplicit-function-declaration]
yy_switch_to_buffer(yy_scan_string(str));
That means that the compiler assumes that yy_switch_to_buffer() and yy_scan_string() return an int, as it does for all functions that are not declared before use (as per the c89 standard). But that is not the case (the first returns void, and the second a pointer (YY_BUFFER_STATE)). Notice that on x86_64, the size of a pointer is not the same as the size of an int.
Adding some band-aid prototypes like
void yy_switch_to_buffer(void*);
void *yy_scan_string(const char*);
to main.c, before their use in parse_string() may stop the segfaulting.
A better fix would be to arrange in the Makefile that the lexer be run with the --header-file=lex-header.h option, and then include lex-header.h from main.c. Or even better, wrap all lex-specific code in some simple functions, and put the prototypes of those functions in a header included from both main.c and the *.l file.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(void)
{
int a,b,sub,i,count=0;char buffer[20],w[20];
scanf("%d",&a);
scanf("%d",&b);
sub=a-b;
if(sub>0)
{
scanf("%s",w); /*wrong answer*/
itoa(sub,buffer,10);
int l=strlen(buffer);
for(i=0;i<l;i++)
{
if(w[i]==buffer[i])
count++;
}
((count==l || count==l-1) && w[0]!='0') ? printf("accepted") : printf("Not accepted");
}
else printf("Sub operation returned negative value");
}
The output of the compiler is
prog.c:4:6: warning: return type of 'main' is not 'int' [-Wmain]
void main(void)
^
prog.c: In function 'main':
prog.c:13:1: warning: implicit declaration of function 'itoa' [-Wimplicit-function-declaration]
itoa(sub,buffer,10);
^
/home/HCDVgj/ccHBnrc7.o: In function `main':
prog.c:(.text.startup+0x61): undefined reference to `itoa'
collect2: error: ld returned 1 exit status
I did not use any long int variables but i still get ld return 1 exists. how to debug these errors
int main() This is teh first thing you should do.
scanf("%19s",w); This is better.
itoa is non standard (so you will not find it in any standard implementaion) better use snprintf()
printf('%s",((count==l || count==l-1) && w[0]!='0') ? "accepted" : "Not accepted"); More compact I would say.
Use of snprintf
snprintf(target, size_of_target1, "%d", source2);
sprintf takes no parameter specifying the number of bytes to write which may lead to buffer overflow which is not a good thing.
1 : In bytes
2 : source is in integer here
Few things worth mentioning to clear your idea or to be more precise
The output you specified is not output of the c program ... in the process of compilation your compiler run into error and then it generates those output. -CiaPan
main() shouldn't be of void return type it is expected to return 0 in case of normal termination. Abnormal termination is usually signaled by a non-zero.-David C. Rankin
I don't know why you're talking about "long int variables".
The first warning can be fixed by changing void main(void) to int main(void) and adding a return 0; at the end.
The second warning tells you that the compiler doesn't know what itoa is.
The following linker error tells you that the linker (ld) also doesn't know what itoa is, and that's why compilation fails.
The reason itoa is unknown is that it's not a standard function and not available on your platform.
I define a structure that I call Neuron.
In my main, I create a two dimensional array:
Neuron N_network[4][10]; //create a neural network
I have a function to print the structure element:
void print_stat_neuron(Neuron * neuron_info)
What should I write to print N_network[i][j] (as an argument of the print_stat_neuron() function)?
I try simply with &(N_network[i][j]) and I get an error message.
I have the fallowing error message with the compiler gcc -Wall -Wextra:
2ex4.c: In function 'init_network':
2ex4.c:168:2: warning: implicit declaration of function'print_stat_neuron' [-Wimplicit-function-declaration]
print_stat_neuron(&N_network[4][1]);
^
2ex4.c: At top level:
2ex4.c:191:6: warning: conflicting types for 'print_stat_neuron' [enabled by default]
void print_stat_neuron(Neuron * neuron_info)
^
2ex4.c:168:2: note: previous implicit declaration of 'print_stat_neuron' was here
print_stat_neuron(&N_network[4][1]);
You need to change the parameter to Neuron (*neuron_info)[10]. Now you can call this function as
print_stat_neuron(N_network);
or
print_stat_neuron(&N_network[0]);
The error messages you show come from not declaring the function before using it, so the compiler deduces a type for the function (extern int print_stat_neuron(); with no specification for the number or types of the parameters), and then objects to you defining it with a return type of void. Note that in C, extern int print_stat_neuron(); is not a prototype for a function with no arguments; that's extern int print_stat_neuron(void);.
This code compiles cleanly:
typedef struct Neuron
{
int x;
} Neuron;
void print_stat_neuron(Neuron *neuron_info);
int main(void)
{
Neuron N_network[4][10];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 10; j++)
print_stat_neuron(&N_network[i][j]);
}
return 0;
}
This declares the function before using it. It would also be acceptable to define the function before using it (which saves having a separate declaration). Of course, if the function will be used outside the source file where it is defined, it should have a declaration in a header, and the header should be used both where it is defined and where it is used. If the function is not used outside the source file where it is defined, it should be static.
Clean compilation checked with GCC 5.3.0 on Mac OS X 10.10.5 (my El Capitan machine is under warranty repair, unfortunately):
$ gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -c neuron.c
$