Thread: Multiple Cpp Files

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    24

    Multiple Cpp Files

    Although I understand the logic of using multiple cpp files in a project. I am unsure what would be a good way to impliment what I want to do... anyway:

    I am writing a rather large Win32 application and the one source file is getting a bit messy. I have decided that I would like to have a seperate cpp file for every windows message that I handle.

    for example, I would have the main cpp file called:

    WinMain.cpp

    then all my other cpp files would be like:

    WM_CREATE.cpp
    WM_MOUSEMOVE.cpp
    WM_PAINT.cpp
    WM_CLOSE.cpp

    etc, you get the idea.

    Now, each cpp file will only contain one function, and here's where I have a dilemma.

    Is it really necessary for each one of these cpp files to have its own header file for one single function declaration? it just seems a bit pointless to me..?

    Also, how would I deal with global variables, having all these seperate files? where do they get declared?

    And one quick last question: If in every cpp file I am including windows.h and any other header files I may require, are these header files literally included many times? or do all the cpp files interlink to use the same one? because my executable file sure would start getting large.

    Any help with any of these questions would be much apreciated.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by w4ck0z
    Is it really necessary for each one of these cpp files to have its own header file for one single function declaration? it just seems a bit pointless to me..?

    Also, how would I deal with global variables, having all these seperate files? where do they get declared?
    1. I'm not positive, but, from downloading source code on this site, it seems to me like you have to include in every source file. I'm looking at one right now with three source files and they all have stdlib.h included.

    Though, I believe all standard libraries contain a
    Code:
    #IFNDEF __WHATEVERTHISIS_H
    #DEFINE __WHATEVERTHISIS_H
    so I'm not sure if this is utilized when there are multiple source files.

    2. Global Variables I would say should be delcared in a libary that you're gonna include in all the source files. Use Extern or whatever.

    That's my two cents (count them... 1... 2) but then I'm a pretty new programmer, so don't let me be the final word.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is not necessary for each source file to have its own header with the function prototype. You can have one header with all the prototypes, and multiple source files each containing function definitions.

    A global variable that will be used in multiple source files needs to be declared in a header file that will be included by all source files that use it (if you only make one header that will be easy). You need to add the extern keyword to the variable declaration (e.g. extern int myVar; ). You then need to define the variable in a single source file (e.g. int myVar = 0; ).

    Including header files doesn't make your executable large. It just means that they are included for each compilation unit. The actual code from Windows.h is already compiled into a library that will be linked with your code at the end.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    So am I to understand that the source I downloaded where the guy included the library in every source file was making a mistake, or was it just because the program was old and that was an old method?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, it wasn't a mistake. I didn't say that including a header file in multiple source files is wrong. It is necessary in each source file to include headers you are using. If you are using something declared in stdlib.h in a particular source file, you should #include stdlib.h. If you are using a variable or function from your own header in a source file, #include that header in your source file. If you aren't using anything from a particular header, you don't need to #include it. It won't hurt much, but it might make compile times slower and make the code less clear.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, thanks. So you need it in every source file you use it's functions in. Thank you.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  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, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM