Thread: including other .c files

  1. #1

    including other .c files

    I've never tried to include other .c files into a program before, so any help would be appreciated.
    If the following were the case:


    #include "p1.c"
    #include "p2.c"

    if a structure/ variable/ function was declared in, for instance, p1.c
    would it then become unneccessary to declare it in any of the other files or even (in this case) the menuprog.c file?
    Are functions called from other files just by the function name?
    Is passing values to the functions done in the same way as if the function were in the same program?
    Would I be right in thinking that variables can only be accessed if they are declared extern?

  2. #2
    ok take out all the #include "something.c" that you've got. when you go to compile the program, dont like it to the executable, make it into an object file (something.o), and do that with all the other c programs youve got, then link them all into one executable with your compiler

  3. #3
    Registered User
    Join Date
    Aug 2001
    Okay, here's how you do it...

    First of all, have you ever included a *.c file in anything??? No, you include a *.h file. This is basically a text file that has prototypes for functions.

    Basically it looks like this:

    void myfunc1(void);
    int myfunc2(void);
    double myfunc3(double x);

    If you save that into myheader.h, they should corespond to the functions in myheader.c (where the functions are defined, and not just declared).

    Now you want to compile myheader.c into an object file; with gcc you do this as "gcc -o myheader.c"

    Put an "#include "myheader.h"" into the program that will call any of the functions in myheader.c, and then compile that *.c file as you normally would (if it contains main()). That should make those functions accessable.

    good luck.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Opening .c files
    By ss1david1ss in forum C Programming
    Replies: 4
    Last Post: 01-23-2007, 11:13 PM
  3. Curiosity: including .cpp files
    By Mr_Miguel in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2007, 11:33 PM
  4. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  5. Including my files?
    By gibbofresco in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2006, 09:57 AM