By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,811 Members | 785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,811 IT Pros & Developers. It's quick & easy.

Duplicate Header Declaration

P: n/a
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
Jul 21 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.
So, effectively, you're writing your own library (or rather, K&R's own
library). Fine, a good thing to do, good practice for you.

This went along just fine, and with each new function, added the
declaration to foo.h and the function to foo.c.
Right.
Then, on one
compilation, I suddenly got a massive number of failures and warnings.
Hmmm.
Like

"Redefinition errors. errors complaining about a variable not having
been declared, when it was etc"
It's hard to debug a paraphrase.
Finally turns out that I had duplicated a declaration in the header
file, which solved all the disparate warnings.
Forgive me, but this sounds unlikely. What sounds more likely is that
you've somehow duplicated some function definitions.
I have a couple of questions.

1) I thought it made no difference if a definition is declared more
than once?
The question is unclear. You can't declare a definition even once, so you
certainly can't declare it more than once. The things you declare are
objects and functions. Care is needed, because although multiple
declarations *are* okay, multiple definitions are not okay, and all object
and function definitions are themselves declarations, so it's easy to mix
up declarations with definitions.
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?
Experienced C programmers will almost certainly respond /so/ automatically
to such a problem that they might not even notice that they're responding.
ie a duplicate declaration. ( It took me
a few hours of removing each function/declaration before it was
obvious what the error was).
It is still not obvious what the error was. To assist in our, and therefore
your, fuller understanding of the error, it might be useful to reconstruct
the error, perhaps by sharing the state of your code after your
few-hours-long debugging session - i.e. the point at which the error was
still present, but suddenly obvious to you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 21 '08 #2

P: n/a
mdh <md**@comcast.netwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 21 '08 #3

P: n/a
Lee
mdh wrote:
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

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
Jul 21 '08 #4

P: n/a
On Jul 21, 1:38*pm, Keith Thompson <ks...@mib.orgwrote:
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.
Jul 21 '08 #5

P: n/a
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?
Jul 21 '08 #6

P: n/a
mdh
On Jul 21, 2:06*am, Lee <l...@yahoo.comwrote:
mdh wrote:
>
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!!! :-)


Jul 22 '08 #7

P: n/a
mdh <md**@comcast.netwrites:
On Jul 21, 2:06*am, Lee <l...@yahoo.comwrote:
>mdh wrote:
>>
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);
[6 lines deleted]
double a_tof (char *s) <<<<<<<<<=========
[8 lines deleted]
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
[25 lines deleted]
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.
[...]

Right. Some compilers will attempt to recover from a syntax error and
continue compilation, in the hope of finding and diagnosing more
errors. But the syntax of C is such that this attempt can easily
fail. If the compiler had correctly guessed that inserting a
semicolon is the right fix, then it could have done so internally and
continued compilation (finally rejecting the translation unit because
of the syntax error). But in this case, it apparently didn't "think"
of that. It likely assumed that the function prototype was the
beginning of a function definition, which it could have been.

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.

Note that I'm talking specifically about *syntax* errors, constructs
that violate the language grammar. Things like an undeclared
identifier, a misspelled identifier, or a type mismatch usually aren't
syntax errors, and don't cause this kind of havoc. On the other hand,
a misspelled typedef name can act just like a syntax error.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 22 '08 #8

P: n/a
On 21 Jul, 09:38, Keith Thompson <ks...@mib.orgwrote:
This is mildly off-topic, but ...

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.
Moving even more off-topic, but I couldn't let this go
without at least a comment. RCS is possibly still worth
using occasionally, but not really. It is certainly not
worth learning CVS at this point in time. There are so many
better options. (git, mercurial, subversion, etc)
Jul 22 '08 #9

P: n/a
On 21 Jul, 17:06, mdh <m...@comcast.netwrote:
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
Jul 22 '08 #10

P: n/a
On 21 Jul, 10:09, rahul <rahulsin...@gmail.comwrote:
On Jul 21, 1:38*pm, Keith Thompson <ks...@mib.orgwrote:
For Unix-like systems, RCS and CVS are good.

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
--
Nick Keighley

Jul 22 '08 #11

P: n/a
mdh
On Jul 21, 9:52*pm, Keith Thompson <ks...@mib.orgwrote:
>
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.
Jul 22 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.