Duplicate Header Declaration

M

mdh

I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.

Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"

Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.

I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?
2) To the more experienced C programmers, if one suddenly gets
widespread warnings and failures to compile, is this something you
would automatically think of? ie a duplicate declaration. ( It took me
a few hours of removing each function/declaration before it was
obvious what the error was).

Thanks
 
K

Keith Thompson

mdh said:
I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.
[...]

( It took me a few hours of removing each function/declaration
before it was obvious what the error was).

This is mildly off-topic, but ...

This is where source code control systems pay off. If you kept a
record of each revision of your source files, the as soon as you
started getting compilation errors, you could compare the current
version against the last one that worked and see *exactly* what you
changed. Or, if you can't figure it out, you can revert to the
previous version and start again from there, hoping not to make the
same mistake again.

It's important to check in your changes frequently. A decent system
will save only the differences, so saving 100 versions of a file won't
cost nearly 100 times as much disk space as a single copy.

For Unix-like systems, RCS and CVS are good. There are various
solutions for other systems (most of the ones I'm familiar with for
Windows are either ported from Unix or expensive). Or you can just
keep copies of old versions of your files.
 
L

Lee

mdh said:
I decided to make a single file containing all the repetitive
functions in K&R so that I could concentrate on the new discussions.
This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c. Then, on one
compilation, I suddenly got a massive number of failures and warnings.

Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"

Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.

I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?

Amazingly I faced a similar issue only yesterday .

I had a struct and an enum which is used by several functions in
different files.
So i wrote the declaration of the struct and the enum in a header file
foo.h and included it in the files that needed them.Then on compiling
kaboom! I have some 68 errors and warnings very similar to those you
got. I Had re-"defined" enum more than once it seems.And the simplest
solution was :

#ifndef FOO_H
#define FOO_H
....
#endif
 
R

rahul

For Unix-like systems, RCS and CVS are good.
While we are off-topic, SVN seems to be an improvisation over CVS as
the commits are atomic and it handles binary files efficiently.
 
M

mdh

Thank you Richard and Keith for those suggestions. Being more awake
today, will try and reconstruct the errors. C ( for me at least) is
truly a learning process, not only in understanding the syntax, but
developing an approach that makes this syntax building easier to write
and more importantly, to debug.
One of the things I tried to find yesterday, (unsuccessfully) was
where the error messages are stored. I imagine that they have to be
unique to C? or perhaps to the compiler?
 
M

mdh

Amazingly I faced a similar issue only yesterday .

.Then on compiling
kaboom! I have some 68 errors and warnings very similar to those you
got.


Well...I think I finally figured it out completely. It was a
combination of defining a function twice, but also of omitting a ";"
in the header file.

This header file:

void reverse( char *s);
void swap( char *s, char *t);
void swap_int(int v[], int, int);
int a_toi(char *s);
int is_space ( char c);
int is_digit(char c);
void i_toa(int n, char *s);
double a_tof (char *s) <<<<<<<<<=========
int getline(char *, int);
void unget_s(char *s);
double pop(void);
void push ( double d);
void str_cpy( char *s, char *t);
void unget_ch(char c);
int get_ch(void);
int getop(char *s, int lim);
int str_len( char *s);


produces these errors.

error: syntax error before '{' token
: error: redefinition of parameter 'str_len'
error: previous definition of 'str_len' was here
error: syntax error before '{' token
: error: parameter 't' is initialized
error: 's' undeclared (first use in this function)
error: syntax error before 'if'
error: syntax error before 'while'
error: parameter 'start' is initialized
error: syntax error before 'while'
error: storage class specified for parameter 'bufpos'
error: parameter 'bufpos' is initialized
error: 'buf' undeclared (first use in this function)
error: storage class specified for parameter 'startbuf'
: error: parameter 'startbuf' is initialized
: error: storage class specified for parameter 'endbuf'
error: parameter 'endbuf' is initialized
: error: redefinition of parameter 'unget_ch'
error: previous definition of 'unget_ch' was here
: error: syntax error before '{' token
error: storage class specified for parameter 'startstack'
: error: parameter 'startstack' is initialized
: error: 'valstack' undeclared (first use in this function)
: error: storage class specified for parameter 'posstack'
: error: parameter 'posstack' is initialized
: error: storage class specified for parameter 'endstack'
: error: parameter 'endstack' is initialized
error: redefinition of parameter 'pop'
error: previous definition of 'pop' was here
error: syntax error before '{' token
Build failed (27 errors)


Adding one semicolon, clears up all.

The only clue I can see ( probably more readily seen by the more
experienced members) is the first error which occurs here.



#include <stdio.h>
#include "krExercises.h"

int main (int argc, const char * argv[]) { /* <<<----error: syntax
error before '{' token


I guess with hindsight this should have been a clue that the header
file was the problem??

All this has to pay off someday!!! :)
 
N

Nick Keighley

While we are off-topic, SVN seems to be an improvisation over CVS as

ITYM "improvement over CVS" :)

though when SVS started fighting with my compiler I think your
improvisation over CVS might have been nearer the mark...
the commits are atomic and it handles binary files efficiently.

and it has a pretty GUI
 
N

Nick Keighley

Thank you Richard and Keith for those suggestions. Being more awake
today, will try and reconstruct the errors. C ( for me at least) is
truly a learning process, not only in understanding the syntax, but
developing an approach that makes this syntax building easier to write
and more importantly, to debug.
One of the things I tried to find yesterday, (unsuccessfully) was
where the error messages are stored.

what do you mean? Do you mean the errors for a particular
run of the compiler or where the strings for all the messages
are stored or the explanations for the error messages...
I imagine that they have to be
unique to C?

well, yes!
or perhaps to the compiler?

there is no standard for the format of the diagnostic messages,
so yes they are compiler specific.

Some compilers document what the errors mean. And some compilers
think they are obvious...


--
Nick Keighley

If somebody sues you either change the algorithm
or you hire a hitman to whack the stupid git.
-Linus Torvalds
 
M

mdh

The lesson: If your compiler reports a syntax error, fix the *first*
syntax error that it reports (which might be several lines before the
indicated line), and don't take any other error messages too
seriously.

Agreed! Thanks Keith.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top