Thread: Using multiple source codes

  1. #1
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38

    Using multiple source codes

    before you say it ive looked at the FAQ and i still have a question.

    how do i use one source code for the main program but use functions from the other source code

    i thoughtmaybe a header file would do it but im not sure.

    Thanx for your help.


    quick side question does ne one know a good console game code site? (if that made ne sense)
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Yes, a header file will do it. For instance:
    Code:
    In Main.cpp:
    #include "functions.h"
    
    int main()
    {
       PrintA();
    }
    
    In Functions.h:
    //declare your functions
    void PrintA();
    
    In Functions.cpp:
    #include <iostream>
    #include "functions.h"
    //define your functions
    
    void PrintA()
    {
        std::cout << "A" << std::endl;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    awesome this will help my project tons.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  4. #4
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    it can be useful to do it if you have long source code but normaly I don't do it
    I started out with nothing and I still have most of it left.

  5. #5
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    ive just started coding and its close to ten pages because of functions its way too clustered.

    i do have a question stoill tho in your supporting source codes do you have to have a int main() and if so what do you put into it?
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There should only be one main function in the project as a whole.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    There should only be one main function in the project as a whole.
    right so the supprting source codes with the functions in them dont need int main. only the source code that runs the program.

    am i understanding right?

    Another queston in the source code that im defining my functions can i globally initialize variables there?
    Last edited by GamingMarvel; 01-14-2005 at 09:40 AM.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by GamingMarvel
    right so the supprting source codes with the functions in them dont need int main. only the source code that runs the program.

    am i understanding right?
    Pretty much... "source code" does not run the program, the program simply runs. The source is compiled into individual object files (one for each source file) and then the linker takes all of those and combines them (along with extra code from any needed libraries) into a single executable program that gets run. If there were multiple mains declared in each of the source/object files, then the linker would have a tough job trying to resolve things and spit out an error.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    so can you initialize variables in the other source code or do you only get to in the functions you define
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by GamingMarvel
    so can you initialize variables in the other source code or do you only get to in the functions you define
    Global variables are still restricted to File scope and can typically only be accessed by functions within that file. However if you wish to access a global variable from code in another file you can use the extern keyword. ie. extern int x;. That will tell the compiler that you are using a variable declared elsewhere in another file. If the variable isn't initialized in another file you will receive a linker error.

    Using the keyword static on a global variable (ie. static int x;) will tell the compiler that the global variable is to maintain file scope disallowing calls from other files using the extern keyword.

  11. #11
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    ok thanx
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. Open Source and Security
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-17-2008, 01:23 AM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  4. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM