Thread: spilting source into many files

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    spilting source into many files

    hello,

    How do I spilt the source code from 1 big file into smaller files?

    I have a simple program which has the functions main(), hello(), bye() ...

    how do I put main() and hello() in 1 file, and bye() in another file and compile it into one executable?

    I want it to behave as if they were all in one file. hello and bye would just print hello and bye to the screen, this is just a example I made up.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    I know I have to use a header file..

    I understand that gloab vars etc go into the header file, but how do I link all this together so that main() calls hello() and hello calls bye() which is really in another source file

  3. #3
    root
    Join Date
    Sep 2003
    Posts
    232
    >how do I put main() and hello() in 1 file, and bye() in another file and compile it into one executable?
    Code:
    // bye.h
    void bye ( );
    
    // bye.cpp
    #include <iostream>
    #include "bye.h"
    
    void bye ( ) {
      std::cout<<"Bye!"<<std::endl;
    }
    
    // main.cpp
    #include <iostream>
    #include "bye.h"
    
    void hello ( ) {
      std::cout<<"Hello!"<<std::endl;
      bye();
    }
    
    int main ( ) {
      hello();
    }
    Code:
    % gcc main.cpp bye.cpp; ./a.out
    Warranty void if you don't have GCC, check your documentation for how to compile with what you have. But it should be frighteningly similar to the above.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    actually global variables don't go into headers. If you do that you'll probably get errors about a variable being defined multiple times when you've included the header file in multiple source files.

    In header files, if you are using globle variables, you would only declare them with the extern keyword.

    Code:
    extern int i;
    pretty much a variable definition but with the keyword extern in from of the definition.

    then in the respective source file you would define the global variable.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I think that if you use the #ifndef you shouldn´t have this problem:

    //header.h
    #ifndef _MY_HEADER_
    #define _MY_HEADER_

    //put header and global variables here

    #endif
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-04-2009, 08:45 PM
  2. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 08:25 AM
  3. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 09:07 AM
  4. Beginner Question: Multiple source files
    By ironfistchamp in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2006, 02:19 PM
  5. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM