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

header file

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

do*******@ulcus.owl.de

Feb 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 17 '07 #2

P: n/a
virgincita schmidtmann wrote:
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.

Feb 17 '07 #3

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

santosh wrote:
>
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.
Feb 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.