header file

  • Thread starter virgincita schmidtmann
  • Start date
V

virgincita schmidtmann

Sorry for inconvenience!

I have two sourcefiles file1.c and file2.c. file1.c codes the standard BST
(binary search tree) functions declarations and the basic struct definition.

file2.c has some special functions, I want to implement for exercise
purposes.

In my opinion gcc file1.c file2.c should do the linking. But the compiler is
complaining, because it doesn't know about the struct and it's pointers
(file2.c).

I tried to write a header to include file2.c. But the #include prepocessor
is before the struct and still the functions in file2 don't know about the
struct.

I would appreciate if someone can give me a hint, how to make this known and
cease my confusion.

Thank you,

Ernst

(e-mail address removed)
 
R

Richard Heathfield

virgincita schmidtmann said:
Sorry for inconvenience!

I have two sourcefiles file1.c and file2.c. file1.c codes the standard
BST (binary search tree) functions declarations and the basic struct
definition.

file2.c has some special functions, I want to implement for exercise
purposes.

Put struct definitions and prototypes into a header, e.g. bst.h, and
#include that header into both C files. Don't put function bodies or
data definitions into the header, though.
 
S

santosh

virgincita said:
Sorry for inconvenience!

I have two sourcefiles file1.c and file2.c. file1.c codes the standard BST
(binary search tree) functions declarations and the basic struct definition.

file2.c has some special functions, I want to implement for exercise
purposes.

In my opinion gcc file1.c file2.c should do the linking. But the compiler is
complaining, because it doesn't know about the struct and it's pointers
(file2.c).

I tried to write a header to include file2.c. But the #include prepocessor
is before the struct and still the functions in file2 don't know about the
struct.

I would appreciate if someone can give me a hint, how to make this known and
cease my confusion.

You haven't clearly specified where your declarations are and which
file expects which declaration. I suggest seperating all the
neccessary declarations into a third header file, named as say
project.h and include this in both file1.c and file2.c. In most cases
you should not include *.c files into other files. You should try to
seperate the relevant declarations and definitions from the somename.c
file into a somename.h file and include the latter wherever
neccessary.

Compile each of your modules seperately into object code and do the
linking at the end. The command for doing that in gcc might be like:

gcc -c file1.c
gcc -c file2.c
gcc -o a.out file1.o file2.o

Also consider instructing the compiler to strictly adhere to standard
C and check for suspicious constructs by passing the -Wall, -Wextra, -
ansi and -pedantic flags. You can also use the -std=XX (XX being c89
or c99) flag instead of -ansi.
 
E

Ernst Schmidtmann

Thanks a lot to Richard and you!

Actually, when I had written the post I realised a way to do that. The
problem was some kind of circle dependencies, which I could resolve
by building two header files. By the way, now I understand the meaning
of ifndef, etc. ...

However, one week of thinking and after writing the problem in a post
I could resolve it. Funny

Thanks a load again,

Ernst
 

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

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top