Help funtions in a seperate file??

P

Paminu

I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?
 
S

Skarmander

Paminu said:
I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?
No, but you need to declare them, and the object files must be linked
simultaneously.

functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void foo(void);
void bar(void);
....
#endif

functions.c:
#include "functions.h"

void foo(void) {
....
}

void bar(void) {
....
}

myprogram.c:
#include "functions.h"

int main(void) {
foo();
bar();
}

Makefile:
myprogram: myprogram.o functions.o
gcc -o myprogram myprogram.o functions.o
myprogram.o: functions.h myprogram.c
gcc -c myprogram.c
functions.o: functions.h functions.c
gcc -c functions.c

The makefile sketched here is a bit silly since it makes everything
explicit (and doesn't use variables or implicit rules), but writing
makefiles effectively is another chapter.

Lastly, "functions.c" is an awful name for a unit because almost any
unit will contain functions. It's a much better idea to split up the
program according to areas of functionality, and think of more
descriptive names. If that means creating a unit with only one function
(foo.c, bar.c...), it's still better than producing a dump.

S.
 
M

Michael B Allen

I have made a lot of helping functions that I would like to have in a
seperate file. I have tried putting them in a file "functions.c". I then
have the file "myprogram.c" that contains some structs and other primary
functions.

In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?

Certainly. A Makefile is interpreted by the make command and spawns
commands (e.g. gcc) in a local shell. In your example, the spawned gcc
program isn't provided with any arguments regarding functions.c. So you
just need to add functions.c to the gcc line like:

myprogram: myprogram.c functions.c
gcc functions.c myprogram.c -o myprogram

Mike
 
M

Malcolm

Paminu said:
In my make file I then have:

myprogram: myprogram.c functions.c
gcc myprogram.c -o myprogram

But that does not work. Is it really impossible to put functions in other
files?
It is certainly possible. Most real C programs contain more than one source
file.

type
gcc myprogram.c functions.c -lm

to compile a multi-function program. -lm links the maths library, which you
may not be using.

make is a horrible tool with strange rules and requirements for tabs etc.
Once you have the program working, you need to fiddle with make's arcane
rules to get it to do what you want. If you look at someone else's makefile
and use that as a skeleton it will probably be the easiest thing.

By splitting up the actions, you find out where the problem lies.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top