Help funtions in a seperate file??

Discussion in 'C Programming' started by Paminu, Oct 16, 2005.

  1. Paminu

    Paminu Guest

    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?
    Paminu, Oct 16, 2005
    #1
    1. Advertising

  2. Paminu

    Skarmander Guest

    Paminu wrote:
    > 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.
    Skarmander, Oct 16, 2005
    #2
    1. Advertising

  3. On Sun, 16 Oct 2005 19:39:27 +0200, Paminu wrote:

    > 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
    Michael B Allen, Oct 16, 2005
    #3
  4. Paminu

    Malcolm Guest

    "Paminu" <> wrote
    >
    > 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.
    Malcolm, Oct 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Hood

    How to create global Funtions & Routines

    Matthew Hood, Jun 24, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,916
    Matthew Hood
    Jun 25, 2003
  2. matthias_k
    Replies:
    13
    Views:
    580
    Matthias
    Jan 26, 2005
  3. moondaddy
    Replies:
    2
    Views:
    568
    Steven Cheng[MSFT]
    Oct 16, 2006
  4. hula

    static funtions

    hula, Jan 13, 2007, in forum: Java
    Replies:
    4
    Views:
    358
    Steve
    Jan 17, 2007
  5. dios de la oscuridad

    i got a prOblem, OF FUNTIONS

    dios de la oscuridad, Aug 18, 2005, in forum: C++
    Replies:
    1
    Views:
    274
    Victor Bazarov
    Aug 18, 2005
Loading...

Share This Page