can I include .cpp file into another .cpp file??

Recommended Answers

All 7 Replies

You can, but it's not generally advisable to #include a *.cpp file. What are you trying to do?

Why are you trying to do that?

my first programming language was Java and in Java to do any project, divides it into some classes and each class has some methods and then call method of class in another class.

I'm now beginner in c++ and I want to do the same previous scenario

So, I thought to include cpp file in another cpp file to do the previous

if that's wrong, So what can I do?

You compile both .cpp files into your project. The linker will take care of the rest.

I don't know about Java, but in C++ you compile the source into object files and then you link the object files into the executable. If you're using an IDE then these two steps will probably be obscured and you will simply click "build" or something like that.

Anyway, to answer the question, you should just have the names of the header files of the classes that you want to use in the cpp files of the files that you want to use them. So, say I have class A and another class B . Both A and B might have their own .h and .cpp files. Your main function might be in another file on its own, but you will use classes A and B in main . So, A.h might look like:

/* Some "standard" headers */
#include <iostream>
#include <string>
#include <vector>

/* Header file for a class used in this class */
#include "C.h"

/* Forward declaration (we can do this if you */
/* only use a pointer to the class, as below) */
class D;

class A{
    // Stuff inside class A
    public:
        /* Class A has member variables that are other classes */
        C m_C;    // Can't forward declare for this
        D* m_pD;  // this type is forward declared (which helps compilation time on a large project)
};

and it's .cpp file would look like:

/* Only need to include the header file for this class here  */
/* since the others are all taken care of in A.h, except for */
/* any forward declared classes (like D, in this case)       */
#include "A.h"

/* Need to include the header files for any forward declared classes */
#include "D.h"

// Define A's functions here

Class B would be similar. Now, main would have to include the headers for the classes that are used:

#include <iostream>
#include <string>

#include "A.h"
#include "B.h"

int main(){
    // Do things using classes A and B here

    // Can't do anything with C or D unless you also include them above,
    // but A and B will still compile, because C & D are included in them
    return 0;
}

Now, the compilation and linking would look something like this (using gcc): g++ main.cpp A.cpp B.cpp C.cpp D.cpp -o a.exe So, the main file and all the class cpp's are in the compilation. The compiler figures out what where to get the headers from by looking in the cpp's. The -o a.exe says that the object files produced by the compiler should be linked into an executable called "a.exe".

thanks

but if I want to but some functions in a .cpp file and this file isnot contain the main function.
is this legal in c++ or not??

It surely is legal. In fact, you need to have exactly one function named main() when you build an executable. So, if you have a main function in several cpp files, you are in trouble. Usually, cpp files which are associated with a class or a set of utility functions that you wrote, will not have a main function (unless it is a unit-test harness that is conditionally compiled only).

So, in order to be able to make an executable, the compiler will compile all those cpp files independently (called translation units) into object files (.o or .obj) and then put them all together (called linking). At the end of this process, it basically has one big chunk of compiled code linked together. It then looks through that chunk of code of a main() function to use as the "entry-point" of the executable. If it does not find one and exactly one main() function, it will give an error.

The typical setup is to have the main function in its own cpp file and do as ravenous laid out, compile a list of cpp files that includes the "library" cpp files and the one "application" cpp file (main.cpp or something like that).

For unit-testing, it is typical to add a main function at the end of each cpp file that is a simple test program that calls and tests every function in your cpp file. But, in this case, you have to enclose it in a #ifdef #endif for conditional compilation, such that they don't interfere with the compilation of applications.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.