Multiple files

F

fb

One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header
file (.h) and a seperate (.c) file in addition to the main (.c) file
that has the main() function in it?

What would go where?
Thanks!

fb goes looking for his missing C book...
 
P

Pieter Droogendijk

What would go where?
Thanks!

It could go all in the same directory:

headingheader.h:
void Heading(void);

mainfile.c:
#include <stdio.h>

#include "headingheader.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

headingfile.c:
#include <stdio.h>

#include "headingheader.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}
 
I

Irrwahn Grausewitz

fb said:
One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header
file (.h) and a seperate (.c) file in addition to the main (.c) file
that has the main() function in it? Exactly.

What would go where?
You would like to put the prototype in a header file (e.g. 'heading.h'),
which you #include in 'main.c', then put the function declaration in a
separate .c file (e.g. 'heading.c'), then compile and link the two.
Thanks!

fb goes looking for his missing C book...
Good luck! :)

Irrwahn
 
I

Irrwahn Grausewitz

Irrwahn Grausewitz said:
You would like to put the prototype in a header file (e.g. 'heading.h'),
which you #include in 'main.c', then put the function declaration in a
separate .c file (e.g. 'heading.c'), then compile and link the two.
Reading P.D.'s reply I've noticed I forgot to mention that you should
#include "heading.h" in 'heading.c' as well...

[I have to be more careful with the 'Send' button anyway.]
 
K

Kevin Bracey

In message <[email protected]>
fb said:
One quick question about multiple files.

---heading.h---

void Heading(void);

---heading.c---

#include <stdio.h>
#include "heading.h"

/* You should include your own header file to guarantee errors */
/* if prototypes don't match. */

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

---main.c---

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}
 
M

Martin Dickopp

fb said:
One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header file
(.h) and a seperate (.c) file in addition to the main (.c) file that has the
main() function in it?

What would go where?

Only declarations (including function prototypes) should go into header
files. If you want to define your `Heading' function in a separate file,
write a corresponding header file with the prototype, and include it both
where the function is defined and used.

Martin


---- heading.h ----

#ifndef H_HEADING
#define H_HEADING

void Heading(void);

#endif


---- heading.c ----

#include <stdio.h>
#include "heading.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}


---- myprog.c ----

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}
 
F

fb

Martin said:
Only declarations (including function prototypes) should go into header
files. If you want to define your `Heading' function in a separate file,
write a corresponding header file with the prototype, and include it both
where the function is defined and used.

Martin


---- heading.h ----

#ifndef H_HEADING
#define H_HEADING

void Heading(void);

#endif


---- heading.c ----

#include <stdio.h>
#include "heading.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}


---- myprog.c ----

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

Ok. Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?
 
I

Irrwahn Grausewitz

fb said:
... Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?

It's of course possible, and for a small projects probably the best
way - you don't want to end up with one header file per prototype or
so, do you? For larger projects there is a "rule" to have one header
file per module, and maybe one header file to gather all the #include
directives you need for your main (or even another) module. This way
it's even more easy to reuse a module in other projects, without
having to find out which parts of your header file you really need -
it's already well sorted.
However, for small projects, or if you are a beginner, it's better to
have all your prototypes and declarations in one place - this way you
won't get confused about where to look for, say, a specific function
prototype and you can concentrate on your task.
OTOH, if your single header file grows to big, you should divide it
in logical units and put these in separate files - even in small
projects you may end up with sth. like 'proto.h' and 'def.h', or the
like.
Anyway, it's up to you to modularize [is this english or germish?]
your project to your own preferences and needs. It is definetly worth
thinking about /before/ you actually start writing code.

After all, there is no one-for-all rule for this.

Irrwahn
 
J

Jack Klein

It could go all in the same directory:

C doesn't have directories, although many platforms that host C
compilers do. And I fail to see what directories have to do with it,
anyway. I could create the files you describe below on two different
computers on two different continents, and still make this work with a
proper path argument to the compiler.
headingheader.h:
void Heading(void);

mainfile.c:
#include <stdio.h>

#include "headingheader.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

headingfile.c:
#include <stdio.h>

#include "headingheader.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
K

Kevin Easton

fb said:
Ok. Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?

You shouldn't put definitions of external-linkage objects or functions
into header filse at all - the reason is that if the header file is
included in more than one .c file in the project, as is usual, then you
will have multiple external definitions which is a no-no. In general,
definitions go in .c files, and declarations go in .h files.

- Kevin.
 
M

Martin Dickopp

fb said:
Ok. Would it be possible to put all my prototypes and definitions
^^^^^^^^^^^
It is certainly a bad idea to put /definitions/ in a header file. Only
/declarations/ belong there.
into one header file? I suppose that would be bad form though...right?

The best solution, in my opinion, is to have a corresponding header file
for every source file. The header file declares the public interface of
the functionality implemented in the source file, i.e. functions that are
supposed to be called from other source files, `struct's if needed, etc.

If you're delevoping a library, that might be a possible exception. The
users of your library would probably find it inconvenient to have to
include a separate header file for every single function, so it might
make sense to group similar functions together. This is what the standard
C library does: it doesn't have <printf.h>, <fprintf.h>, <fopen.h>, etc.,
but it has <stdio.h>, which declares a number of functions dealing with
a certain "topic".

Martin
 
L

LibraryUser

fb said:
.... snip ...

Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?

You should think of header files as defining the **published**
interface to the code and data specified in a corresponding .c
file. At the same time you can take the precaution of declaring
other entities in the .c file as static (functions and file scope
variables) to ensure they cannot be externally accessed. This
will simplify future maintenance.

You can have multiple .h files for a specific .c file, thus
allowing finer control of external visibility. However this does
not prevent name clashes.
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top