Compiling c++ multiple sources file

c++ multiple sources file compiling using g++ is easy but it requires a little manual works. Multiple source file compiling can be more easy and straight using make file. But i will give here only a simple example. If you think you need  example of makefile too then you can Google search or write comments and i will update this post!

Hope you already understand the basic of c++ like functions,class etc.

C++ source file one


#include 
#include "hell.h"

void testing(){
std::cout<<"Test\n";
testing1();
}

int main(){
std::cout<<"Test\n";
testing();

return 0;
}



C++ source file two


#include "hell.h"

void testing1(){
Test tt;
tt.t="LALA";
std::cout<<"Hello world 2\n"<<tt.t<<std::endl;
        tt.h();
 }
void test::h(){
std::cout<<"C++ method\n";
}
 
 
 

I declared object name of the class called "Test".
t is variable declared in the header file so tt.t mean "use the variable from class Test!".
You can write any valid code in the function or in c++ Class method!

C++ Header file



#ifndef HELL_H //if hell.h not defined the go to next preprocessor
#define HELL_H // Well, Include the header!

#include

void testing1();
void testing();

class Test{
public:
std::string t;
void h();
};

#endif //Protection done!



It is just simple compiling the sources using g++ :

g++ main.cpp main2.cpp -o main

pro@pusheax:~/coding/c++/basic/multi$ ./main
Test
Test
Hello world 2
LALA
C++ method


Thanks for reading!