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

using #ifndef

P: n/a
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
....
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?

Thanks!

John

Nov 14 '05 #1
Share this Question
Share on Google+
25 Replies


P: n/a
In article <10***************@proxy2.srv.ualberta.ca>,
John Hanley <jd******@telusplanet.net> wrote:
Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.


What's *in* the .h file? It should just be function prototypes, not
complete function definitions.

-- Richard
Nov 14 '05 #2

P: n/a
>I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.


You put the functions in program.h? That's why. Header files are
generally used for function PROTOTYPES, type definitions, #defines,
and sometimes global variables (with "extern"). Defining a function
in a header file, then including it in more than one file which
will be linked together into the same program, is a problem. As
you observed, you get multiple definitions of functions.

#ifndef only works within the same compilation unit. For example,
it WILL protect you from including "program.h" twice, or from
including "program.h" and "foo.h" which includes "program.h". It
will NOT protect you from "program.h" already being included in
another compilation unit. The compiler is not psychic. It does
not know that you are going to link that compilation unit you already
did and the current one together into the same program. And even if
it was, that's not how #ifndef is supposed to work. You start with
a clean slate at the start of every compilation unit.

Gordon L. Burditt

Nov 14 '05 #3

P: n/a
"John Hanley" <jd******@telusplanet.net> writes:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?


Yes, you've got a typo on line 137 of program.c.

Seriously, I don't see anything wrong in what you've posted. Try
narrowing your program down to a set of very small files (say 10-20
lines each) that still exhibits the problem. You just might discover
the problem while you're doing this; if not, post again.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

P: n/a
You put the functions in program.h? That's why. Header files are
Actually, I didn't. That kind of confuses me too. I just have the
prototypes, 3 structs, and some extern global variables in the header file.
I define these in my program.c file.
#ifndef only works within the same compilation unit. For example,
it WILL protect you from including "program.h" twice, or from
including "program.h" and "foo.h" which includes "program.h". It
will NOT protect you from "program.h" already being included in
another compilation unit. The compiler is not psychic. It does


I see what you mean. The compiler compiles them each individually
and can't see in to the future to see the .o files will be linked.

I will try a different approach.

Thanks so much!

John

Nov 14 '05 #5

P: n/a
John Hanley wrote:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here? cat program.h #ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(void); // declaration
#endif//GUARD_PROGRAM_H
cat program.c #include "program.h"

void my_function(void) {// definition
fprintf(stdout, "Hello from my_function(void)!\n");
}
cat main.c #include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
gcc -Wall -std=c99 -pedantic -o main main.c program.c

Nov 14 '05 #6

P: n/a
> > cat program.h
#ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(void); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(void) {// definition
fprintf(stdout, "Hello from my_function(void)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c


This exactly what I did. But in Rhide, it tells me "multple definitions of
_function", and at the command line it tells me "undefined reference to
_function". Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?

Thanks!

John
Nov 14 '05 #7

P: n/a
"John Hanley" <jd******@telusplanet.net> writes:
> cat program.h

#ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(void); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(void) {// definition
fprintf(stdout, "Hello from my_function(void)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c


This exactly what I did. But in Rhide, it tells me "multple definitions of
_function", and at the command line it tells me "undefined reference to
_function". Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?


It's *exactly* what you did?

Try compiling *exactly* the code above and see whether you get the
same error.

If not, as I suggested elsethread, trim your own code down to a short
example that exhibits the problem.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

P: n/a
John Hanley wrote:
> cat program.h #ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(void); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(void) {// definition
fprintf(stdout, "Hello from my_function(void)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c

This exactly what I did.


No it isn't exactly what you did.
But in Rhide, it tells me "multple definitions of _function",
and at the command line it tells me "undefined reference to _function".
Yet what you have above is just how I set it up.
I am unsure why it is doing that.
Any other ideas?


Yes.

Stop being such a twit!

We can't read your mind.
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."

Otherwise, stop bothering us.
Nov 14 '05 #9

P: n/a
> No it isn't exactly what you did.

Then perhaps you can point out what I did wrong. What I did was declare my
functions, global variables and structs in the .h file. I then #included
them in my .c files as you have done in the example program.
Yes.

Stop being such a twit!

We can't read your mind.
I thought perhaps there was some broader problem I was overlooking. I am
not an experienced
programmer that is why I am asking questions on the newsgroup.
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."
That's fine. I was only replying to your email as you emailed me.

Otherwise, stop bothering us.


No one is forcing you to read my posts and nobody has a gun to your head
telling you
to answer them. If you don't like my questions, you leave ME alone. I
would really
not be a bit offended if you didn't answer my posts.

I was in no way being rude in asking any questions and I appreciated all the
responses I've received as I realize people take time to try and help. You,
however, were rude. You could've been polite about it, but you weren't. I
guess that's your choice...


Nov 14 '05 #10

P: n/a
On Thu, 26 Aug 2004 17:20:11 -0600, "John Hanley"
<jd******@telusplanet.net> wrote:
I was in no way being rude in asking any questions and I appreciated all the
responses I've received as I realize people take time to try and help. You,
however, were rude. You could've been polite about it, but you weren't. I
guess that's your choice...


I had to look up the message ID to see who you were responding to,
since you didn't provide attributions. As it turns out, I couldn't
tell by the threading because I have ERT permanently killfiled. Ignore
him. Most everyone else does.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #11

P: n/a
"E. Robert Tisdale" wrote:
John Hanley wrote:
.... snipped ...
Stop being such a twit!
.... snip ...
Otherwise, stop bothering us.


Ignore Trollsdale. Its primary purpose is to annoy and give
misinformation. It is a well known troll around here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12

P: n/a
"John Hanley" <jd******@telusplanet.net> writes:
No it isn't exactly what you did.


Then perhaps you can point out what I did wrong. What I did was declare my
functions, global variables and structs in the .h file. I then #included
them in my .c files as you have done in the example program.


We can't point out what you did wrong without knowing exactly what you
did. Sometimes we can figure out the problem from a brief
description; in this case, we can't.

I suspect the problem is in some detail that you're not telling us,
perhaps because you've assumed it's not significant. That's
understandable; if you were knowledgeable enough to know exactly
what's significant and what isn't, you probably wouldn't have to come
to us for help. Since you are coming to us for help, you really
should give us the information we're asking for. We need a verbatim
cut-and-paste *brief* example of the C code that's causing the
problem. Without that, we can't help you.

[...]
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."


That's fine. I was only replying to your email as you emailed me.

[...]

This is Usenet, not email. The difference is significant; though your
remarks here might be aimed at one person, there are many more
listening in.

As for the problem you're actually asking about, we've now explained
several times what you need to tell us before we can help you. Your
description, quoted above, is not enough. ERT was unnecesarily rude
in my opinion, but he's absolutely right about at least one thing: we
can't read your mind.

You can continue describing what you're doing, and we can continue
telling you we don't know what the problem is. Or you can show us
what you're doing, and we can probably help. The choice is yours (but
one of the choices would be a collosal waste of time and, well, rude.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13

P: n/a
John Hanley wrote:
You, however, were rude.
You could've been polite about it, but you weren't.


I didn't mean to be rude.
I was trying to get your attention.
We can't help you unless you do as Keith suggested.
Nov 14 '05 #14

P: n/a
Alan Balmer wrote:
John Hanley wrote:

I had to look up the message ID to see who you were responding to,
since you didn't provide attributions. As it turns out,
I couldn't tell by the threading
because I have ERT permanently killfiled.
And I'm grateful for that.
Ignore him. Most everyone else does.


Do you intend to help John Hanley with the problem that he describes?
Nov 14 '05 #15

P: n/a
>> > cat program.h
#ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(void); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c #include "program.h"

void my_function(void) {// definition
fprintf(stdout, "Hello from my_function(void)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c


This exactly what I did. But in Rhide, it tells me "multple definitions of
_function",


There are no references to _function in any of the code above.
What, EXACTLY, did the message say?
and at the command line it tells me "undefined reference to
_function".
Again, there are no references to _function in any of the code above.
WHERE did it say this reference was?

Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?


Gordon L. Burditt
Nov 14 '05 #16

P: n/a
In article <10**************@proxy2.srv.ualberta.ca>,
John Hanley <jd******@telusplanet.net> wrote:
This exactly what I did. But in Rhide, it tells me "multple definitions of
_function", and at the command line it tells me "undefined reference to
_function".


Are you *absolutely sure* that you only have prototypes in the .h file,
and not definitions? Please post the declaration of "function" from your
..h file.

-- Richard
Nov 14 '05 #17

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"John Hanley" <jd******@telusplanet.net> writes:
No it isn't exactly what you did.
Then perhaps you can point out what I did wrong. What I did was declare my
functions, global variables and structs in the .h file. I then #included them in my .c files as you have done in the example program.


We can't point out what you did wrong without knowing exactly what you
did. Sometimes we can figure out the problem from a brief
description; in this case, we can't.

I suspect the problem is in some detail that you're not telling us,
perhaps because you've assumed it's not significant. That's
understandable; if you were knowledgeable enough to know exactly
what's significant and what isn't, you probably wouldn't have to come
to us for help. Since you are coming to us for help, you really
should give us the information we're asking for. We need a verbatim
cut-and-paste *brief* example of the C code that's causing the
problem. Without that, we can't help you.


Sounds good. I'll post that at the bottom of this reply.

[...]
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."
That's fine. I was only replying to your email as you emailed me.

[...]

This is Usenet, not email. The difference is significant; though your
remarks here might be aimed at one person, there are many more
listening in.


True. I actually was replying to an email. Mr. Tisdale had replied to my
post
over email and cc'd the newsgroup. I did a reply all and replied to both.
I
should've realized it probably shouldn't have gone to the newsgroup as well
(being more
of a personal reply). My apologies for breaking any Usenet etiquette.

You can continue describing what you're doing, and we can continue
telling you we don't know what the problem is. Or you can show us
what you're doing, and we can probably help. The choice is yours (but
one of the choices would be a collosal waste of time and, well, rude.)
Ok, here's my header file r_climat.h

struct data_record
{
...
};

struct data_list
{
struct data_record * head;
struct data_record * tail;
};

struct parameter_list
{
...
}parameters;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

/* Global variables */
extern int FORMAT_TYPE;
extern char PARAM_FILE[256];
extern char * IN_FILE;
extern char OUT_FILE[13];

extern void init(struct data_list * l);
extern struct data_record * new_node(long int sid, long int date, int year,
int month, int element, long int val[96], char f[32]);
extern void add_to_tail(struct data_list * l, long int sid, long int date,
int year, int month, int element, long int val[96], char f[32]);
extern int remove_record(struct data_list * ls, struct data_record * rec);
extern void get_filename(char * filename, char * outname);
extern int validate_format(char * f);
extern void print_list(struct data_list * dl);
extern int format_HLY(char * f);
extern int f_eat_spaces(FILE * f);
extern int get_parameters();
extern void empty_list(struct data_list * d);
extern int get_number_of_days(struct data_record * d);
extern int to_CSV_DLY(char * filename, struct data_list * list);
extern int format_DLY(char * f, struct data_list * dl);
extern int format_MLY(char * f);
extern int format_FIF(char * f);

I tried with and without the #ifndef/#define/#endif and as well with and
without the extern on the functions.

I was able to figure out what was wrong with the undefined references to my
global variables. I declared them here, but not in my .c file. So I don't
have that error anymore.

As for my "multiple declaration" problem, here's the snippet of my .c files:
main.c
#include "r_climat.h"

#define HLY 0
#define DLY 1
#define MLY 2
#define FIF 3

int FORMAT_TYPE;
char PARAM_FILE[256];
char * IN_FILE;
char OUT_FILE[13];

int main(int argv, char * argc[])
{
main program here
}

and here's r_climat.c


#include "r_climat.h"

#define HLY 0
#define DLY 1
#define MLY 2
#define FIF 3

/* initialize head and tail of data_list to NULL */
void init(struct data_list * l)
{
l->head=NULL;
l->tail=NULL;
}

and the rest of my functions that would take another 800 lines or so.

I only define them in my r_climat.c (I know I don't define any in main.c)
and I only prototype them
in r_climat.h. They all compile just fine with no errors or warnings. It
is during linking that I get this
"multiple definitions" error.

Let me know if there is anything else specific you would like.

Thanks again for all your help. I really do appreciate all of it.

Best regards,
John


Nov 14 '05 #18

P: n/a
to get an accurate answer, please post your entire source code.

chok
John Hanley wrote:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?

Thanks!

John


Nov 14 '05 #19

P: n/a
Ok, it seems my code wasn't what was buggy. I am using the RHIDE ide for
djgpp. When I built it at the command line just now it didn't give me
errors. I think what I did was when I created my "project" in RHIDE, I
added my files including my .h file. I just removed my .h file from the
project and it didn't give me any errors in RHIDE.

Sorry for taking up all this time on such a small oversight. I really do
appreciate the replies and all the help.

Thanks so much!

John


Nov 14 '05 #20

P: n/a
"E. Robert Tisdale" wrote:

Alan Balmer wrote:
John Hanley wrote:

I had to look up the message ID to see who you were responding to,
since you didn't provide attributions. As it turns out,
I couldn't tell by the threading
because I have ERT permanently killfiled.


And I'm grateful for that.
Ignore him. Most everyone else does.


Do you intend to help John Hanley with the problem that he describes?


What part of killfiled does Trollsdale not understand?

Then again, there are so many things he fails to understand.


Brian Rodenborn
Nov 14 '05 #21

P: n/a
On Fri, 27 Aug 2004 16:11:13 GMT, Default User
<fi********@boeing.com.invalid> wrote:
"E. Robert Tisdale" wrote:

Alan Balmer wrote:
> John Hanley wrote:
>
> I had to look up the message ID to see who you were responding to,
> since you didn't provide attributions. As it turns out,
> I couldn't tell by the threading
> because I have ERT permanently killfiled.


And I'm grateful for that.
> Ignore him. Most everyone else does.


Do you intend to help John Hanley with the problem that he describes?


What part of killfiled does Trollsdale not understand?

Then again, there are so many things he fails to understand.

The funny thing is that I *did* help John Hanley with the problem he
describes. In the post I replied to, his problem was that our resident
troll has been unnecessarily rude to him. I suggested a solution to
that problem.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #22

P: n/a
On Fri, 27 Aug 2004 10:23:37 -0600, "John Hanley"
<jd******@telusplanet.net> wrote:
Ok, it seems my code wasn't what was buggy. I am using the RHIDE ide for
djgpp. When I built it at the command line just now it didn't give me
errors. I think what I did was when I created my "project" in RHIDE, I
added my files including my .h file. I just removed my .h file from the
project and it didn't give me any errors in RHIDE.

Sorry for taking up all this time on such a small oversight. I really do
appreciate the replies and all the help.

Not a problem. Such posts are often valuable for the side effects :-)

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #23

P: n/a
On Fri, 27 Aug 2004 08:55:06 -0600
"John Hanley" <jd******@telusplanet.net> wrote:

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
<snip>
I suspect the problem is in some detail that you're not telling us,
perhaps because you've assumed it's not significant. That's
understandable; if you were knowledgeable enough to know exactly
what's significant and what isn't, you probably wouldn't have to
come to us for help. Since you are coming to us for help, you
really should give us the information we're asking for. We need a
verbatim cut-and-paste *brief* example of the C code that's causing
the problem. Without that, we can't help you.


Sounds good. I'll post that at the bottom of this reply.


<snip>
Ok, here's my header file r_climat.h

struct data_record
{
...
};
<snip>
struct parameter_list
{
...
}parameters;


This could be your problem (unless my brain is temporarily
non-functional, which is possible after the week I've had). I assume you
mean either:

typedef struct parameter_list
{
...
}parameters;

or:

struct parameter_list
{
...
};

You could have other similar problems in the bits you have not included.
and here's r_climat.c


#include "r_climat.h"

#define HLY 0
#define DLY 1
#define MLY 2
#define FIF 3

/* initialize head and tail of data_list to NULL */
void init(struct data_list * l)
{
l->head=NULL;
l->tail=NULL;
}

and the rest of my functions that would take another 800 lines or so.

I only define them in my r_climat.c (I know I don't define any in
main.c) and I only prototype them
in r_climat.h. They all compile just fine with no errors or warnings.
It
is during linking that I get this
"multiple definitions" error.

Let me know if there is anything else specific you would like.

Thanks again for all your help. I really do appreciate all of it.


What we want is something that compiles and shows the problem.

Delete functions one at a time until either you have something short
that compiles and shows the problem (i.e. fails linking with multiple
definitions).

You may even find that in deleting bits you stumble across the cause of
the problem yourself.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #24

P: n/a
"John Hanley" <jd******@telusplanet.net> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... [...]
This is Usenet, not email. The difference is significant; though your
remarks here might be aimed at one person, there are many more
listening in.


True. I actually was replying to an email. Mr. Tisdale had replied
to my post over email and cc'd the newsgroup. I did a reply all and
replied to both. I should've realized it probably shouldn't have
gone to the newsgroup as well (being more of a personal reply). My
apologies for breaking any Usenet etiquette.


In my opinion, that was ERT's fault for sending his response both by
e-mail and to the newsgroup. It rarely makes sense to do that. If I
see an e-mail response to something I've posted on Usenet, I'm likely
to assume that it was only a private response, and reply to it
privately; later, when I check the newsgroup, I find that I wasted my
time when I should have just posted publicly. (Actually, I'm more
likely to check first, since I've fallen into this trap a couple of
times.) If a response is sent both by e-mail and to the newsgroup, it
should be clearly labeled at the top.
You can continue describing what you're doing, and we can continue
telling you we don't know what the problem is. Or you can show us
what you're doing, and we can probably help. The choice is yours (but
one of the choices would be a collosal waste of time and, well, rude.)


Ok, here's my header file r_climat.h

struct data_record
{
...
};


I don't believe your header file has a "..." in the definition of
struct data_record. More on that below.

[snip]
As for my "multiple declaration" problem, here's the snippet of my .c files: [...] int main(int argv, char * argc[])
{
main program here
}
and here's r_climat.c
#include "r_climat.h"

[...] and the rest of my functions that would take another 800 lines or so.


I'm glad to see from another response you posted in this thread that
you were able to figure out the problem (something about another file
that your IDE picked up when you didn't want it to). But I'm going to
offer some constructive criticism anyway. Keep in mind this is
absolutely meant to be *constructive* criticism; it's also for the
benefit of other readers.

After going back and forth several times, you never actually gave us
what we asked for. First, you gave us a brief summary of what your
source code looked like. Then, you gave us snippets of your source
code (see above), but nothing that anyone else could actually compile.

What you should have done was something like this:

Copy all your source files to a separate directory (or the equivalent
for your system) and start ripping things out. If you're getting an
error message that refers to a particular function, delete everything
other than that one function. Delete everything you can from the body
of the function; narrow it down to something like:

some_type my_func(int foo, char *bar)
{
some_type dummy;
return dummy;
}

and narrow your main program down to:

#include "my_header.h"
int main(void)
{
some_type result;
result = my_func(0, NULL);
return 0;
}

At each step, from the complete source code down to a minimal version
of it, recompile and confirm that you're still getting the same error
message. With luck, you'll be able to narrow the problem down to a
few dozen lines of code, small enough to post here. If you're really
lucky, you'll figure out while you're doing this what the problem is
(in your case, you probably would have figured that out the first time
you tried to compile in a new directory). The important point is that
the rest of us can then try compiling your code ourselves. If the
code you post has things like "..." and "main program here", we can't
do that.

When we ask for the exact code that's causing the problem, we mean
exactly that -- the *exact* code, not a summary, not a snippet. If
the code causing the problem is too big to post, you can almost
certainly trim it. If you're looking at problem that shows up during
compilation or linking, the trimmed version of the program doesn't
even have to do anything useful, as long as it produces the same error
message.

And speaking of error messages, you should also post the exact error
message. Telling us that the compiler "tells me there are multiple
definitions of each of my functions" may not be sufficient;
cut-and-paste the actual error message. If you haven't figured out
the problem, don't assume that your summary of the error message won't
leave out some critical detail that you haven't recognized.

Finally, let me recommend a web page that probably covers everything
I've written here better than I have: "How To Ask Questions The Smart
Way" by Eric Raymond and Rick Moen,
<http://www.catb.org/~esr/faqs/smart-questions.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #25

P: n/a
John Hanley wrote:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.


Function definitions ought to go in C files.
And function declarations aka prototypes ought to go in a h file.

If you can make sure this separation does exist in your project,
then your compiler should not have any problem.
Problems comes into picture the moment you bring in a function
definition in a header file.

--
Karthik
Nov 14 '05 #26

This discussion thread is closed

Replies have been disabled for this discussion.