I need to define some structs and constants to be used by 2 different programs that will use them for communication.

I'm trying to figure out how to use headers files. I've read as much as I could about it.

This is what I figured so far:
- you need a .h file for prototypes and a .c for the code that goes with those prototypes

This is what I want to find out:
- How to link those 2 files in a makefile
- For structs what part goes in the .h file and what in the .c file
- How to define constants
- Do the .h and .c files need to in the same place as the makefile?

Thanks in advance

11-12-2008

officedog

Not sure it'll answer all your questions, but this article might be of interest:

But I still don't know:
- How to link those 2 files in a makefile
- For structs what part goes in the .h file and what in the .c file
- Do the .h and .c files need to in the same place as the makefile?

11-12-2008

laserlight

Quote:

Originally Posted by kotoko

How to link those 2 files in a makefile

What do you mean? It is a matter of writing the makefile so that the source can be compiled correctly.

Quote:

Originally Posted by kotoko

For structs what part goes in the .h file and what in the .c file

That article mentioned structure definitions. An example would be:

Code:

struct s
{
int x;
double y;
};

Quote:

Originally Posted by kotoko

Do the .h and .c files need to in the same place as the makefile?

Generally, no.

11-12-2008

matsp

Quote:

Originally Posted by kotoko

- For structs what part goes in the .h file and what in the .c file

Generally, we want AS LITTLE AS POSSIBLE in the header (.h) file, and as much as possible in a source (.c) file. The more we expose of functions and data structures, the less freedom we have to change those functions and data structures in the future, without also changing and/or recompiling the code that uses those things.

AS LITTLE AS POSSIBLE means that if you only need to tell the rest of the code that "there is a struct called X", then a declaration "struct X;" is sufficient to have in the header-file. However, this means that no user of the header file will actually be able to use the content of struct X directly - all references to such a structure must be via a pointer, and the content of the struct is not available). (An example of this is the FILE structure defined by stdio.h - you never use a FILE struct, only FILE *, and you never do "FILE->something", because we never need to use the CONTENT of the FILE structure - we just need to have a pointer to a FILE struct, and let the C runtime library internals worry about what the content is and how to use the content).

Of course, if you want to do complex number calculation, and you want the application to be able to access the real and imaginary parts of the complex number directly, then the whole struct should be defined in the external interface - the header file.

--
Mats

11-12-2008

kotoko

Thanks for all the explanations!

But I still don't understand the following:

How should I write the makefile so that I can include libraries that are not standard? How can I specify where they are? I guess I need to do that otherwise the compiler won't find them.

A practical example.

I have bookie.c and client.c . Each are in it's own folder because I don't know how to have a makefile for two different files.
I was going to put the header files I need and their .c files in the parent folder. How do I do that?

11-12-2008

matsp

So what does your current makefile look like? If you haven't got a makefile at all, you should read up on general makefiles.