473,387 Members | 1,619 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,387 software developers and data experts.

Duplicate Header Declaration

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
11 4648
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in...
4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
0
by: Luke Airig | last post by:
I am trying to merge two xml files based on common date/time and then write out a tab-delimited xml file with the header record from one of the input files concatenated in front of the merged...
2
by: stranger | last post by:
My database is set up so people can input parts orders. Sometimes they order the same parts on a monthly basis. I want to be able to duplicate past parts orders and have it pasted in with a new...
7
by: ucfcpegirl06 | last post by:
Hello, I have a dilemma. I am trying to flag duplicate messages received off of a com port. I have a software tool that is supposed to detect dup messages and flag and write the text "DUP" on...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
4
by: wanwan | last post by:
Hi, I have a project with multiple c files. Some of these modules have functions that have the same name and their operations are exactly the same. My rusty memory tells me there is a way...
2
by: beet | last post by:
Hi all, I tried to declare a c++ struct like following in a header file; I want to include this header file in other files to create and access this struct. ------ 1 #ifndef _SEARCHDATA_H_...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.