Adham shafik
Adham shafik

Reputation: 1044

How can c++ compiler know where is the class

If you defined a class for doing something, all you need to do in order to use it is to include a header file (.h file) that contains its methods prototypes, but you don't have to include the class file itself (the .cpp file which contains the actual implementation of class methods),

Does that mean that the .cpp file must have a name identical to its header file so the compiler matches names to locate the .cpp file?

OR

When the compiler meets a function (method) call it goes to header files and sees if it has a prototype. Then it searches for implementation in its built-in functions, if not found it searches in all .cpp files in the current directory?

Or are both assumptions not true?

Upvotes: 2

Views: 509

Answers (3)

Kerrek SB
Kerrek SB

Reputation: 477010

Filenames are irrelevant in C++. The linker puts all the pieces together. All you need to know while com­pi­ling a bit of C++ code is what the names of things mean. And that's the purpose of declarations and class de­fi­ni­tions: They introduce names and thus give the compiler enough information to generate code:

file1.cpp:

int f(int, bool);         // declaration

class Foo;                // class declaration

class Bar { int g(); };   // class definition (implies declaration)

int main()
{
    Foo * p;              // OK, Foo * is a complete type, since we know that
                          // "Foo" denotes a class

    // p->h();            // Error: We don't know what Foo actually is!

    Bar b;                // OK, we know how size and alignment for Bar

    int m = b.g();        // OK, we know what sort of function B::g() is

    int n = f(50, true);  // OK, we don't need to know what f does

    return m + n;
}

When the above code is compiled, it will contain unresolved symbols for f and B::g. As long as one (and only one) other translation unit contains the definition for those symbols, the linker can resolve them and the program can be linked successfully:

file2.cpp:

int f(int n, bool b) { return b ? n * 2 : 50 - n; }

// include class definition here
int Bar::g() { return f(sizeof(Bar), sizeof(int) == sizeof(char)); }

The basic rule is that you can declare a function or a class or define a class a as many times as you like, but you can only define a function and a class member function once. There is one exception: If you declare a function for class member function as inline, then you can actually define it repeatedly, provided all the definitions are identical.

The corollary of this rule is that you can safely put all the function declarations and class definitions into a header file and include it repeatedly, as long as you only provide the actual definitions once in one single translation unit. (And the inline rule allows you to include actual member function definitions inside class definitions – member functions that are defined together with their declaration are implicitly inline.)

Upvotes: 4

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

In order to produce a program, the compiler needs both. Typically, the compilation from C++ to a binary takes place in two steps though, first every CPP file (aka translation unit) is converted to an object file (containing mostly machine code but also an address table). Then, the object files are connected (linked) to each other (for which the address table is needed) forming the final executable. Typically, in a header file, you say "there's a function with these parameters and this return value". In the object file, a call to that function is then recorded as reference to that function. Finally, when linking, this reference is resolved with the actual address of the function that must be provided by other object files.

Upvotes: 1

Macmade
Macmade

Reputation: 53960

The compiler only needs the header file. But all C++ files needs to be compiled, so the linker can produce the final binary.

Upvotes: 2

Related Questions