How to use multiple files?

This is a discussion on How to use multiple files? within the C++ Programming forums, part of the General Programming Boards category; Ok, i know i probably should have done this earlier and all. Up until now, i have been linking my ...

  1. #1
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    How to use multiple files?

    Ok, i know i probably should have done this earlier and all.

    Up until now, i have been linking my projects that use multiple files by just creating every other file besides the main file as a header file, and then #including it so that i can use the functions. However, im sure the correct way is to create them as seperate .cpp files so that you can use them easily for many projects.

    My first attempt at this was to create myself a program that create a log for each of my other programs i run, and hence i want it to be done in the most efficient way.

    So, after many failed searches, i thought i would ask for an example of how to link files together that use .cpp extensions, and how to call these files from the main .cpp after i have included them (if you are supposed to include them). ALl help appreciated
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    It depends on what compiler you are using. Basically what you do is compile each separately and then link them together. Some IDEs (Integrated Development Environment) will do this automatically if you have the necessary files included in the project.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Ok, so its as simple(?) as putting each of the files into the linker stuff.

    Does anyone who uses Dev c++ have an example of it? Like how you call a main function from one file in another?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    With dev-c++ you can just start a new project and add files into your project. This is an example of what clownpimp was talking about, since dev-c++ will do all the linking for you. But here is a command line example (in its simplest form):

    gcc myfile1.c myfile2.c myfile3.c -o myexe.exe

  5. #5
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Ok, thats simple enough So how do i call one main function inside another main function in a different file?

    ie:

    Code:
    // file1.cpp
    #include <iostream>
    
    int main(){
    
    welcome screen;
    read input;
    
    CALL TO FILE 2...
    
    
    return 0;
    }
    Code:
    // file2.cpp
    #include <iostream>
    
    int main(){
    
    some calculations;
    draw something;
    
    return 0;
    }
    How do i make that call to file 2? Or is this not the way to do it?
    Last edited by fry; 12-20-2002 at 02:35 AM.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  6. #6
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    I played around some more, but i feel completely lost still
    Any ideas? Or possible another way to organise large projects?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    .h file's shoud lcontain any datatypes or function prototypes that you want other .cpp files to be able to call. Then write the code for the funtion prototypes you created in those .h files in .cpp files. Add those .cpp files to your project, andyou should be ready to compile. Just make sure you #include the .h file in any .cpp that you want to use the corresponding functions for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. need assistance with multiple files
    By c++.prog.newbie in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2006, 01:44 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 11:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 10:57 PM

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