Multiple Source Files!?!?

This is a discussion on Multiple Source Files!?!? within the C Programming forums, part of the General Programming Boards category; Ok, I've practically finished this little program and then when I go to compile some source files together, it says ...

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Multiple Source Files!?!?

    Ok, I've practically finished this little program and then when I go to compile some source files together, it says I am calling undefined functions. In my book they have a program that is basically set up like this:


    main calls menu() and main contains a printf("end\n"); so that when control is finally returned to main from menu() (in which case the user selected the "exit" option, it lets them know it's finished.

    menu is in it's own "menu.c" file.

    menu asks user what he wants to do and then executes some functions according to what the user picks.

    Functions are contained in a "ops.c" file which also grabs some other function from a header file.

    As soon as each function in the ops file completes, control is returned to menu (menu(); at the end of each function in ops.c).


    Well, I tried to do that very thing. But for some reason when I try to compile it, it says my menu file contains undefined references to my functions contained in my ops file. I don't understand because I DID define the function in the ops file and they are not declared static.


    I keep comparing my program to the one in my book and I can't find what I have done wrong. Help would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    First of all, check and make sure that your function files include thier respective header at the top.
    Eg: header file name == myfuncs.h
    in file containing the code of the functions, at the top, you must have this line:
    #include "myfuncs.h"

    Also, I think the most likely source of this problem may come from the compiler you are using. most (if not all) compliers are not able to automatically find out which files are containing your function files.

    in order to have them know which files to look inside, you have to create a project or something like that, and add the function files(header file may not be required to be added this way...) to the project. That is all MsStudioCPP & DevCpp requires. Some of the other compilers may require that you create the makefiles for the project yourself. For more information, you'd have to look up your compiler documentation.

    I hope I gave you enough information to look up the correct data. Can't help more unless I know and have experience with your compiler.

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok thanks. Well, I am using GCC and in my book, it has an example that worked correctly with GCC. What I have to type is gcc sourcefile.c othersourcefile.c moresourcefiles.c -o exename.exe and it will compile them all. I just don't understand why they won't work in mine but they worked with the books and the books uses the same method practically (1 header file, 3 source files).

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Hmm... Don't have much experience with console based compilers, and can't look through your source codes to see if there's any errors...

    could you post the #include part of all your header files and source files?

    Also, if your not forced to use gcc, might I suggest you try out bloodshed's Dev-CPP? it's a pretty good open source freeware compiler...

  5. #5
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Well I tried not to because of space issues but I might as well post my code.

    Here it is:
    code removed to reduce size of post due to problem solved

    In my compiler this is what I type:

    gcc job.c jobmenu.c jobops.c -o job.exe
    That is when I see the compiler error. I know this can be done however, because I wrote some example code from my book and used the same method and it worked. So I must have done something I am just not spotting or something.
    Last edited by Padawan; 04-03-2004 at 02:43 PM. Reason: Removed Code

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Okay, I'm not going to go through this file by file because apparently the major problem is that you don't quite know how to include separate files yet...

    First major problem:

    The file that contains your main doesn't include ANY of the other files you created. Doesn't matter if 99% of the work is done by functions in other file or even if you main only calls one function that does all the work. The file that contains you main MUST INCLUDE all the header files.

    BTW, jobmenu doesn't have a header file, so you shouldn't even be able to include it in the first place.

    Also, the way you've done your files is confusing. The best way is to have one header file for each separate function file. Exceptions are made if you have alot of structures...
    Eg.:
    I have four .c files, one contains my main and nothing else.
    the other three call 'em myfuncs1.c, myfuncs2.c, and myfuncs3.c
    Now I have to have 3 header files:
    myfuncs1.h, myfuncs2.h, and myfuncs3.h.
    generally, what people(or at least good programmers do) is put ALL thier function prototypes into thier header files, and then in the .c file that has the same name, place the implementation(ie, type out the code.)

    Now here's the slightly tricky part... You have to include each and every one of your header files in at least two files: The file that has your main function, and it's respective code file.

    that is to say, in my example,
    main.c will have this three lines at the top:
    #include "myfuncs1.h"
    #include "myfuncs2.h"
    #include "myfuncs3.h"

    and in each respective .c file,
    ---myfuncs1.c---
    #include "myfuncs1.h"
    ---myfuncs2.c---
    #include "myfuncs2.h"
    ---myfuncs3.c---
    #include "myfuncs3.h"

    NOTE that I said AT LEAST two files... If any of your other files need to use a function that's in another file, you'll have to include it's header file as well. Generally, this sort of includes are done in the header file. Also be careful when doing this, it's very easy to accidentally create and infinte loop of includes... If your compiler hits one of those, you'll might have to reset your comp...

    this is what happens:
    --myfuncs1.h--
    #include "myfuncs2.h"
    --myfuncs2.h--
    #include "myfuncs3.h"
    --myfuncs3.h--
    #include "myfuncs1.h"

    See what happens? In myfuncs1.h, you tell your compiler to read myfuncs2.h, then in myfuncs2.h, you ask it to look in myfuncs3.h. There, you tell it again to look in myfuncs1.h, starting the whole process again until your compiler runs out of memory and crashes your comp...
    There are work arounds, but I think that's enough info dump for a post...

  7. #7
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Thank you VERY much. Problem solved. That was very helpful.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    132
    Quote Originally Posted by tzuchan
    The file that contains you main MUST INCLUDE all the header files.
    Actually it doesn't require that you do this. You can quite easily just include "myheader.h" which inside it includes all the required header files.

    Quote Originally Posted by tzuchan
    The best way is to have one header file for each separate function file.
    I wouldn't say this is the best method, infact I would get annoyed having so many files for my projects. I would just use one header file.

    Quote Originally Posted by tzuchan
    Now here's the slightly tricky part... You have to include each and every one of your header files in at least two files:
    Why? Again by looking at the first quote you will see my reasoning for this.

    And with my methods of including during a multi-file project I have never even run into, nor heard of an infinite loop of includes.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote:
    Originally Posted by tzuchan
    The file that contains you main MUST INCLUDE all the header files.


    Actually it doesn't require that you do this. You can quite easily just include "myheader.h" which inside it includes all the required header files.
    ...That's not not having to include all the header files you use, that's just putting it in another file...

    And as to the one to one ratio of header and source files, that honestly up to personal preference... I like being able topull parts of my previous projects of and use them in a new one with little or no modifications...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, main doesn't really need to include any header files. Consider the following:
    Code:
    foo1.c
    extern void menu( void );
    
    int main( void )
    {
            menu( );
            return 0;
    }
    
    foo2.c
    #include <stdio.h>
    void menu( void )
    {
            printf(
            "\t1) Do something.\n"
            "\t2) Do something else.\n"
            "\t3) Do nothing.\n"
                  );
    }
    
    gcc -o foo12 foo1.c foo2.c
    ./foo12
    
            1) Do something.
            2) Do something else.
            3) Do nothing.
    
    
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Try compiling with GCC by executing gcc *.c -o ExeFileName instead of GCC file1.c file2.c file3.c -o ExeFileName.
    The world is waiting. I must leave you now.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That only works if those are the only .c files in the directory. But it's a nice feature when it is set up that way.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Quote Originally Posted by quzah
    That only works if those are the only .c files in the directory. But it's a nice feature when it is set up that way.

    Quzah.
    As in the project you're trying to compile will only compile with my suggestion if it's the only project inside the folder you're currently in? Oh..common knowledge(I freakin hope people know what wildcards are).

    Ideally you should have a folder on your computer called source, or something similar. Then have a subfolder under that for each separate project.
    The world is waiting. I must leave you now.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, if I were actually doing a worthwile project. But when I write random snippets as illustration, I hardly feel the need to create a seperate folder for them. But thanks for telling me what I should be doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > But thanks for telling me what I should be doing.
    I wasn't telling you what to do. In fact, I wasn't even directing my first post in this thread towards you. You know what I was talking about..as you just pointed out.

    >
    Sure, if I were actually doing a worthwile project. But when I write random snippets as illustration, I hardly feel the need to create a seperate folder for them.
    <
    What I was talking about, was this. That's what is ideal for normal and serious coding.
    Last edited by Shadow; 04-03-2004 at 11:21 PM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple source files in one project
    By mintsmike in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2009, 07:20 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 02:42 AM
  3. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  4. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 02:39 PM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21