Compiling c++ multiple sources file

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

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 <iostream>
#include "hell.h"

void testing(){
std::cout<<"Testn";
testing1();
}

int main(){
std::cout<<"Testn";
testing();

return 0;
}

C++ source file two

#include "hell.h"

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

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 <iostream>

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!

Leave a Reply

Your email address will not be published. Required fields are marked *