Multi-File Projects?

This is a discussion on Multi-File Projects? within the C++ Programming forums, part of the General Programming Boards category; I can't seem to understand how multi-file projects work. Are all the files internally combined into one, or do they ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    Multi-File Projects?

    I can't seem to understand how multi-file projects work. Are all the files internally combined into one, or do they all include each other...? Does every file need a main()?

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post This may help...

    Multi-file projects I think "link" to each other. No, each doesn't have to have a main. If you wanted one to call functions from it would be like this:


    // file1.cpp

    int add(int a, int b)
    {
    return a + b;
    }

    // mainfile.cpp

    #include <iostream.h>
    #include "file1.cpp"

    int main()
    {
    cout << add(5, 5);

    return 0;
    }

    You can also do this:


    // File2.cpp

    int add(int a, int b)
    {
    return a + b;
    }

    // File1.cpp

    #include <iostream.h>
    #include "File2.cpp"

    int out()
    {
    cout << add(5, 5);
    }

    // mainfile.cpp

    #include <iostream.h>
    #include "File1.cpp"

    int main()
    {
    out();

    return 0;
    }


    This isn't a good explanation, but I hope it helps some.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Each .cpp file is compiled into it's own .obj file. The linker then combines these files into your .exe file.

    Headers are used/included in your code to let the compiler know that you are accessing functionality that is outside the current file scope... in which case it knows that it will be linked in at a later date (provided that you have actually written that code, or have a library elsewhere).

    No... you only need one main() function... you program needs ONE entry point so it knows where to start, this is the main() function... so having more than one in your project would give you an error.

    Hope this helps.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    6
    So lets say I wanted to make a program with Networking code in one file and the main code in the other. Would I set it up like this?

    -------------------------------------------------

    //network.cpp

    #include <whatever.h>

    void connect(){
    //whatever
    }


    // gui.cpp

    #include <whateverelse.h>

    void main(){
    connect();
    }

    -------------------------------------------------

    And once its found the entry point, where do I go from there? Let's say the user has chosen to connect to the network, so how do I get it so the networking file "takes over"? Sorry, I'm a PHP coder, not used to compiled languages

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    As long as the prototypes are global, use conditional compliation, all will work.

    Conditional Compilation is #including a header in all files, but by using #ifndef statements ensures it is complied only once into the project.

    ie
    prototypes.h

    #ifndef MY_APPS_PROTOS
    #define MY_APPS_PROTOS
    //all the prototypes go here
    #endif

    On build the compiler will link all into one exe.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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