*.cpp and *.h files understanding

This is a discussion on *.cpp and *.h files understanding within the C++ Programming forums, part of the General Programming Boards category; So i have programmed in C++ for many years now, i know OOP and stuff that comes with it. But ...

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    *.cpp and *.h files understanding

    So i have programmed in C++ for many years now, i know OOP and stuff that comes with it. But there's only one stupid thing that i don't understand :
    What are cpp files used for ? ( I know that i need to create "main.cpp" but for the rest cpp files, couldn't they work even if they are h )
    I know that when you include header file, everything from that file is copyed to where it's included. So does this work for cpp files ? Could you include cpp file ?
    Are cpp files executed on them selfs without including anything ? ( Just like main.cpp )
    In what order are those files executed ? Is main first or last ?
    Why aren't variables in cpp same as variables in h ?

    Please tell me if you know anything !
    Anything related to cpp files would help...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    For a long project it is convenient to make

    first : a '.h' file with #ifndef , #define and the names of some functions ,not the implementation and #endif.

    second : a '.cpp' of the same name for implementing the function s of the 'h' file , #include "name of the '.h' file".

    third : finally, another cpp file also including the 'h' file which has the int main() and is using the function of the previous files .

    That's what I can say about .cpp and .h files , it is a bit clearer for the long projects , and the .h files can be reused with other .cpp files .

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Here is a loose example of how the C++ build process works.

    When you build a project, the compiler does many things. First it takes all of the preprocessor commands and fills those in to the source. It appends the included libraries to the program, it writes the defined constants to the source, etc.
    After that, it parses the C++ code, doing all of it's syntactical analysis, and converts it to assembly code.
    Then the assembler converts the assembly to machine code creating what are known as object files. They usually have a .o extension, though someone one told me .obj is possible. I've never seen it.
    Then finally, the linker takes it and tries to link up any unresolved reference that any single object file has. If one source has a call to myfunc() but no definition, it goes to the other object files being linked and finds the definition and in theory connects them. After that it generates a single machine code executable.

    That's the gist of it. Now understanding that, let's answer your questions. First, the .cpp files are what gets compiled in the first place. The extension, while some compilers will identify it, is really for the end user to identify what it is. Many times it could be whatever you want, more so in the command line since you're telling the compiler what files to compile anyway. You don't include .cpp files. I'm not sure what "executed on themselves" means but if you're compiling a single source, your program must have an instance of main() in it. The files aren't executed. In the build process they're compiled in the order that you pass them, it doesn't matter of the order. If main() doesn't exist anywhere the linker will find that out. What makes you think variables in .cpp files aren't the same as variables in .h files?

    The reason you have multiple source files is simple, the logic of modularity and encapsulation. When you have an extremely large program with multiple parts, you want to try and divide those parts into logical groups. First, in groups of what parts most interact with each other and what data work independent of each other. Second, what parts most likely need changing and updating and what parts are best left alone until major revisions.

    If you wrote a database, your database will have multiple large sections of code. For example, it may have a front-end GUI that handles what all of the database operators see. Allows input to the database are well as fetching data from the database. You'll have a section that parses the data inputted and finds it's logical place in the database. You'll have a section that handles regular scheduled processes that update the database. Among others. Each of these parts could have their own object file so that updating one doesn't require recompilation of the others.
    And within those parts, you'll also have sub parts. Some of the functions in those sections remain fairly constant through time and don't require any sort of maintenance. That information you try to keep "hidden" from the maintenance team so that nothing accidentally gets changed that shouldn't be. Other parts you might need updated regularly. This might not even be source code, it may be .ini files or something else that doesn't require compilation.

    The point of this is to limit what needs to be rebuilt when changes are made. If you need to change the GUI, you only have to fix that object file. The others stay the same and all you have to do is relink them all.

    Anyway, I'm don't with this rant for now. If you have more questions, ask.
    Sent from my iPadģ

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    I know that when you include header file, everything from that file is copyed to where it's included. So does this work for cpp files ? Could you include cpp file ?
    Yes, you can include a .cpp file, or any other file of any extension for that matter.

    But to avoid compilation errors, whatever is inside the file you include has to be compilable, and not cause multiple definition errors.

    Are cpp files executed on them selfs without including anything ?
    Each source file you pass to the compiler each compiled into an object file.
    These object files are later linked to form the program binary. (.EXE, .DLL)

    In what order are those files executed ? Is main first or last ?
    It doesn't really matter. All the source files are compiled into respective object files.
    The linking process will find missing functions, multiple definitions, etc.

    Why aren't variables in cpp same as variables in h ?
    They're the same. It's just that .h files are usually #included into .cpp files. So if you include a .h file (which has a global variable) more than once in two different .cpp files, you'll get multiple definition errors.

    The important thing to note is that to the compiler, all it does is compile source files. Header files are not compiled, they're copy-and-pasted into source files.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 10:52 PM
  2. Curiosity: including .cpp files
    By Mr_Miguel in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2007, 11:33 PM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  4. Help understanding C syntax
    By hpteenagewizkid in forum C Programming
    Replies: 3
    Last Post: 12-04-2006, 10:04 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 03:57 PM

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