Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct's in header files! 2

Status
Not open for further replies.

cjw2

Technical User
Jan 4, 2005
10
GB
I have declared a struct (see below) in a header file to be used in two source files, however when I compile I get a multiple declaration error even though it only declared once can anyone explain this error??

//this is the struct declared in the header
struct student_data
{
int student_number;
char name[26];
char address[26];
char post_code[8];
char telephone[14];
char course[3];
};

//reference it in source file
struct student_data student

when compiled it returns an error? why?
 
> struct student_data student
Because this declares an instance of the data in all the files you include this header in. So if you have more that one file, it gets multiply declared.

This goes in the header file
Code:
extern struct student_data student;

This goes in ONE .c file
Code:
struct student_data student;

--
 
Encountered another problem I have two source files for e.g. source1.c source2.c and the header including the struct declaration as set out in original post header.h.

Both source1.c and source2.c need to be linked and use the struct.
Source2.c will compile and execute fine on its own using the header file.

However when I try and link source1.c and source2.c (using #include "source2.c") this is when I get the multiple declaration error even if there is no reference to the struct in source1.c.

when I comment out the #include "source2.c" the error does not appear?
This also happens using extern?
I dont understand?
 
You DON'T include one source file inside another.

[tt]gcc source1.c source2.c[/tt]
ONE source file contains main()
ONE source file contains the [tt]struct student_data student;[/tt]
BOTH source files [tt]#include "header.h"[/tt]

--
 
The project I am working on requires that we include source2.c inside source1.c using #include "source2.c".

source1.c uses functions defined in source2.c.

 
I guess
Code:
#include "header.h"
#include "source2.c"
But that's ugly and broken in my opinion.

--
 
That is what I have done, but still have the issue with multiple declaration of the struct, which is used in both source files when compiling.?
 
Seconded.
Header files, not source files.
Use inline functions in your header file if
you need code included.
 
Sorry I dont understand, what does inline functions in header file mean?

All the functions I have, all function prototypes have been decalred in header file, work in separate source files. when I try and use multiple files it returns the multiple declaration error (relating to the struct), which is declared in the header file.
 
I guess you're going to have to paste your source code and exactly what you type in to compile it, and exactly what error messages you get printed out.

If you did what I suggested in my first reply, I can't see how you can be getting multiple defined symbols.


--
 
Using extern still generates the same error message.
 
The project I am working on requires that we include source2.c inside source1.c using #include "source2.c".
Then the project you are working on is broken.


However when I try and link source1.c and source2.c (using #include "source2.c") this is when I get the multiple declaration error even if there is no reference to the struct in source1.c.
The word "link" means something very specific when building programs. Don't use the word if you don't really mean it.

You include header files in the source files.

You compile the source files into object files.

You link the object files together to form an executable.


when compiled it returns an error?
when I comment out the #include "source2.c" the error does not appear?
This also happens using extern?
I dont understand?
Why would you end these sentences with question marks? Stop that! They aren't questions; they're just statements! They need to end with a period.



Read this example code and restructure your code so that it matches the example. The parts in red concern the use of [tt]extern[/tt]; you may or may not need to use them.

foo.h
Code:
#ifndef FOO_H
#define FOO_H

struct foo
{
  int bar;
};

void somefunc( struct foo *f );

[COLOR=red]/* DECLARE it using "extern" */
extern struct foo globalfoo;[/color]

#endif

foo.c
Code:
#include "foo.h"

void somefunc( struct foo *f )
{
  f->bar = 5;
}

[COLOR=red]/* Then DEFINE it like this,
  in ONE source file */
struct foo globalfoo;[/color]

main.c
Code:
include "foo.h"

int main()
{
  struct foo f;
  somefunc( &f );
  somefunc( &globalfoo );
  return 0;
}


when compiled it returns an error?
An error is not just a random complaint by the compiler. It tells you what you did wrong. When you want help, posting the compiler or linker error will let people explain to you what you did wrong.
 
Thank you for your comments, however as part of the project the main.c has got to include the header file, header.h, and the source file (source2.c) this is the way I have to do it. This may seem like the wrong way of doing it but that is the way it mist be done, so source files must stay as .c files and cannot be changed to .h files.
Not allowed to make changes to the header file, which also means cannot use extern as the original header file does not contain it.
I still do not understand why I get the multiple declarartion error message when it compiles ok in one source file then when I include this .c file in the main.c the error occurs, even when there is no declaration of the struct in main.c.
 
It's just more and more bizarre rules for what you can and can't do, none of which make any sense whatsoever. Whoever is laying these specs on you shouldn't be let near a computer, let alone be specifying how programs should be written.

On the assumption that you include source2.c near the start of main.c (and not in the middle god-forbid), then perhaps a single
Code:
struct student_data student;
at the start of source2.c will do what you want.

This long ago turned into a pointless guessing game of working out an answer to a problem where the rules are revealed in a piecemeal fashion, and I'm done playing.

--
 
source files must stay as .c files and cannot be changed to .h files.
When did anyone ever suggest doing that?

No one said to turn the source file into a header file. It has been suggested that you not include source files via the preprocessor.

As I mentioned, one traditionally compiles several source files into object files, then links those object files together.

I don't think you really understand what you're trying to do, here. I think you're particularly confused about how to link object files together.


Did you even read my example? Did you understand it?

Your original post does not suggest that you even need to use the [tt]extern[/tt]. I think Salem suggested that because your original code snippet is misleading and makes it appear that you have attempted to declare a global variable in the header file.

In other words, restructuring your code to follow my example does not necessitate "changing a source file into a header file" or "modifying the header," presuming the header already defines the struct.


There's no reason that you should need to include a source file for what you're doing.

I'll say it again: you need to compile the source files separately and then link the resulting object files together.

If you fail to do that, you are using the language and the tools incorrectly, and nothing in your program justifies doing this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top