#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0

    Calling functions from other files


    I'm pretty fresh meat in programming c++ and I've got a question.

    I'm gonna make a game and this is the way I want it to work. I've got the main function, and from that I want to call all other functions that do stuff. Like first I'll call the function that will initialize the starting values and then I'll call the on which shows the values to the user etc. But for easy configuration I want all of these functions to be in separate files, one function per file. How can this be done?


    Cheers,
    Karl
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    118
    Rep Power
    13
    What I would suggest is to write each function in its own seperate header file. Then have a seperate header file that holds only the function prototypes.

    In your main program, include all the headers you wrote.

    #include "func_prototypes.h"
    #include "init_func.h"
    #include "display_func.h"

    etc..
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Very easily. In fact it's the standard way that projects are written, even though beginning C/C++ books usually only have you do single-file programs.

    But the first big question is: what compiler are you using? Many IDEs incorporate and encapsulate the project creation process. So we would need to know what you are using before we can help you there.

    But the standard things are:
    1. For the functions you want to have other modules (a standard term for the code contained within one file) to use, place their prototype in a header file that will be included in the module that will use that function; eg:
    Code:
    // foo.cpp
    
    // the function that is to be shared
    void foo(void)
    {
    }
    // end of foo.cpp
    
    
    // foo.h -- the foo header file
    // use "guard defines" to prevent multiple includes 
    #ifndef _FOO_H_
    #define _FOO_H_
    
    // the prototype for the function that is to be shared
    void foo(void);
    
    #endif
    // end of foo.h
    
    // main.cpp
    
    #include "foo.h"
    using namespace std;
    
    int main(void)
    {
        foo();
        return 0;
    }
    // end of foo.cpp
    Remember that global function prototypes go into header files. Code goes into .cpp source files only (exception being inline functions).

    2. Include in the header files any class, struct, and type declarations that more than one module will need.

    3. Include in the header files external references to global variables; eg:
    Code:
    // foo2.cpp
    
    // the global variable
    int iFooBar = 42;
    
    // end of foo2.cpp
    
    
    // foo2.h -- the foo2 header file
    // use "guard defines" to prevent multiple includes 
    #ifndef _FOO2_H_
    #define _FOO2_H_
    
    // the external reference to the global variable, iFooBar
    extern int iFooBar;
    
    #endif
    // end of foo.h
    Remember that a variable can only be initialized in a source file (ie, a .cpp) and not in a header file.

    So far, these rules have been for the purpose of getting each source file to compile. The basic rule here is that before your code can use an identifier (eg, type, variable, or function name), it must have been declared.

    The next rule is for the linker to work right.

    4. Any variables or functions within a module that are private to that module and will not be shared with any other module should be declared as static. All non-static identifiers are considered global by the linker, so if you have file-scope variables or functions in more than one module with the same name, then the linker will complain of multiple instances of the same variable/function and stop the build. This should be discussed in your textbook under namespace collision. Declaring them as static avoids such collisions. Of course, a variable/function that is to be shared cannot be declared as static.

    NOTE: this usage of static is different than declaring a local variable within a function as static or than declaring a static class member.

    Hope this helps you get started. And please tell us what your compiler/IDE is so that we can tell you how to put it all together.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    Thanks for the great response, I have different ones. It's a school project and in school I have Visual c++ enterprise edition 6.0 and at home I have Dev-c++ by Bloodshed software.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    I've checked your example a litte more now. I've also reas that you shouldn't place code inside headers just as you said. But my functions are supposed to do some code, where is that code written? In other .cpp files? And in that case, how to include them?

    Thanks for answers,
    Karl
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    118
    Rep Power
    13
    Yeah, just do an #include "blah.cpp" and it should work.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    modrocker's method of #include'ing the .cpp source files is one way, though not commonly used. It will work alright for smaller projects, but when you get into anything very sizable (over 100 modules), it can cause problems.

    The common method is to group the modules together into a project. This has traditionally been done by running the make utility against a Makefile which contains a list of the modules, what compiler or assembler to use with what options set, and which files depend on which (so that only the ones that changed or are affected by the changes made will be re-created, thus saving time). Though the writing of a makefile from scratch can be considered something of an art.

    Microsoft VC++ does and Borland C++ did (I don't know about C++Builder) maintain a special project file that you can modify through the IDE; VC++ thorough the Project menu item, wherein you can add source files to the project and change the project settings (eg, to add a library that needs to be linked in). And one of the Project submenu items is "Export Makefile".

    The Dev-C++ IDE also allows you to create a project and manipulate it through the IDE, plus allow you to create a makefile based on the project.

    I also have Dev-C++, but I never use the IDE, prefering to use the MinGW gcc/g++ compiler from the command line or with my own makefile -- for a new project, I copy another project's makefile and then modify it, which I understand to be common practice. Or from the command line you could just invoke gcc or g++ directly, listing the source files (accepts wildcards), naming the executable target, providing the compile options, and listing the libraries to link in. If your project is small enough or every single .cpp file in the directory is to be included and you run DOSKEY, then it's not difficult to do. But if your project is larger, you might want to go with a makefile.

    Hope that all made sense.

    BTW, to create a dependency list for a makefile, you can run gcc with the -MM option, then simply copy the output straight into the makefile; eg:
    gcc -MM *.c

    That's no doubt too advanced for you right now, but I mentioned it because some of the lurkers reading this may be able to benefit from that info. And you might remember about it in the future when you get to that point.
    Last edited by dwise1_aol; October 6th, 2003 at 04:55 PM.
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by dwise1_aol
    [B]
    but I mentioned it because some of the lurkers reading this may be able to benefit from that info. /B]
    indeed! having learned C++ on windows i was always clueless in regards to makefiles. recently starting reading 'info make' and they are actually very handy. that's a cool little trick, thanks :)
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    Thanks guys, I got it working now! I chose to include .cpp files since it's a smaller game and I haven't learned anything about headers yet so I will have to deal with that later on.

IMN logo majestic logo threadwatch logo seochat tools logo