Thread: Multiple source files in one project

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Multiple source files in one project

    Hello people;

    I have a quick question. When looking at projects programmed by other programmers, i sometimes notice that the project contains multiple .CPP files, each with a header file by the same name. I would like to know why this is done, how they are each used and are they all compiled. I have not only seen this in VC++ but in other packages such as Dev-C++.

    Can somebody please explain the concept to me,

    Kind regards and thanks in advance,
    mintsmike

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Separating a program into different files has a few advantages -
    1) faster build time (you don't have to rebuild the whole project when only a small part of it is changed)
    2) better organization
    3) re-usability
    4) easier collaboration (different programmers can work on different files concurrently)

    To build a project with more than one files, you would have to compile them separately, and finally link them together. IDEs usually do this for you if you add them to a project.

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    .cpp files are the files that define code, header files are the files that declare code. It's common practice to do this, as it makes your code easier to read.
    MSDN <- Programmers Haven!

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    So if i were to have a file tree that looked like this:


    • main.cpp
    • extra.cpp
    • extra.hpp

    I could declare some extra functions say genRand(int seed) in extra.hpp, I could include that header in extra.cpp and then define that function, and then use that function (without including anything) in main.cpp

    Is that right,

    Thanks in advance,
    mintsmike

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You need to have the declaration in the hpp -
    Code:
    int genRand(int seed);
    The implementation in cpp -
    Code:
    #include "extra.hpp"
    
    ...
    
    int genRand(int seed) {
    ...
    }
    and
    Code:
    #include "extra.hpp"
    in main.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    OK, thanks for clearing that up.

    But why does that work??

    Regards,
    mintsmike

  7. #7
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Separating headers (.h or .hpp files) from implementation (.cpp) files is especially useful to users of libraries. Sometimes programmers need to look at the header files to understand how to use a class. It's nice not to have all the implementation code cluttering up the file. It also allows you to keep your implementation secret. Traditionally, small functions are put into header files to allow the compiler to 'inline' them. If the function is small enough, the compiler will replace the function call with the code, saving the cost of a function call.

    The definition of the class is included in the source file using the class so that the compiler can check that you are calling the class methods properly. That is why you need to include extra.hpp in main.cpp

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Including the hpp in main tells the compiler (when compiling main) that a function named genRand() is available (implemented elsewhere). After all the cpp's are separately compiled, in the linking stage, the linker will see that main calls genRand(), but doesn't have an implementation for it, so it will search the other object files for the function, and find it in extra.o, and make calls to the function in main to refer to that. If the linker can't find the function you promised will be available, you will get a linker error.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Ok thanks for that guys. Now i may use that technique (or should i say WILL use that technique). At least now i know how it works

    Thanks again,
    mintsmike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  2. Class Inheritance over multiple source files
    By Swarvy in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2008, 10:03 AM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM