1

I just feel weird about how does that work ? That my first time that I've ever seen that , two c++ files located in the same directory "Test1.cpp,Test2.cpp"

Test1.cpp :

#include <iostream>
void magic();
int main(){
    magic();
return 0;
}

Test2.cpp :

#include <iostream>

using namespace std;
void magic(){
    cout << "That's not a magic , it's a logical thing.." << endl;
}

As I mentioned above , they are in the same directory , with prototype of 'magic' function. Now my question is , how does magic work without any inclusion of Test2.cpp ? Does C++ include it by default ? if that's true , then why do we need to include our classes ? why do we need header file while cpp file can does its purpose ?

nullptr
  • 127
  • 9
  • 3
    The process you're looking for is called *linking*. Linking identifies names with external linkage, so the name `magic` in both files comes to refer to the same entity in the link. A link in which all names refer to defined (rather than just declared) entities can be used to create an executable program or library. – Kerrek SB May 07 '16 at 10:36
  • 1
    The declaration `void magic();` tells the compiler that there will be a function with this signature - either later in the same file or at linking time - so you will be able to use it in your code. [What is the difference between a definition and a declaration?](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration). – t.niese May 07 '16 at 10:37
  • You could have used `Test2.h` for the `magic` declaration, but that's already in `Test1.cpp` and it's not needed in `Test1.cpp`, nor is its definition needed in `Test2.cpp`. – LogicStuff May 07 '16 at 10:38
  • C++ doesn't have prototypes; rather, we just say that you *declared* `magic`, or that Test1.cpp contains a "declaration of `magic`". The term "prototype" is used in C, which is a different language with different rules. C++ is simpler than C in that regard. – Kerrek SB May 07 '16 at 10:39
  • 1
    In Test.cpp void magic(); indicate to the copiler that somewhere a function magic is defined. This dependency will be resolved during liking phase. – ar-ms May 07 '16 at 10:41
  • Headers aren't actually source files. They're pasted directly into `.cpp` files with `#include`. The linker only cares about `.cpp` files. – uh oh somebody needs a pupper May 07 '16 at 10:42
  • Thanks all for the help , @KerrekSB "C++ doesn't have prototypes" some instructors saying that this is prototype ? – nullptr May 07 '16 at 10:44
  • @pythonlover: Not all instructors have solid C++ knowledge :-S Considering simple population statistics suggests that if you pick up a random "C++ instructor", you'll probably be learning a lot of bad habits... – Kerrek SB May 07 '16 at 10:45
  • @pythonlover there is only one reliable instructor of C++, the C++ ISO standard. Everything else is a lie (including books, human instructors and StackOverflow ;) ) – Ivan Aksamentov - Drop May 07 '16 at 11:22

2 Answers2

2

In order to obtain an executable from a C++ source code two main phases are required:

  • compilation phase;
  • linking phase.

The first one searches only for the signature of the functions and check if the function call is compatible with the found declarations.

The second one searches the implementation of the function among the libraries or objects linked through the options specified through command line of the linker (some compilers can automatically run the linker adding some command line options).

So you need to understand the compiler and linker options in order to understand this process.

Francesco Argese
  • 626
  • 4
  • 11
0

The main catch of headers file is simplifying writing of code.

Let's think about next example:

test2.cpp

#include <iostream>

using namespace std;

void my ()
{ magic(); } // here we don't know what magic() is and compiler will complain

void magic(){
  cout << "That's not a magic , it's a logical thing.." << endl;
}

This code gives next error:

gaal@linux-t420:~/Downloads> g++ test2.cpp 
test2.cpp: In function ‘void my()’:
test2.cpp:6:9: error: ‘magic’ was not declared in this scope
 { magic(); } // here we don't know what magic() is and compiler will complain
         ^

To avoid this error we need to place declaration of magic() function before definition of my(). So it is good idea to place ALL declarations in one place. Header file is a such place. If we don't use headers, we'll need to paste declaration of magic() function in any cpp-file where it will be used.

nullptr
  • 127
  • 9
George Gaál
  • 1,216
  • 10
  • 21
  • @GerogeGaal , Does that mean if I removed the declaration of magic "void magic();" it will be linker error ? (missing definition) ? – nullptr May 07 '16 at 17:48
  • It won't be linker error, but compiler error. I edited answer. Please look at specific error. – George Gaál May 07 '16 at 17:52
  • oh yes , but if we declare it without the its definition , it will be linker error , is that right ? – nullptr May 07 '16 at 18:03