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

void testing(){

int main(){

return 0;

C++ source file two

#include "hell.h"

void testing1(){
Test tt;
std::cout<<"Hello world 2n"<<tt.t<<std::endl;
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{
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
Hello world 2
C++ method

Thanks for reading!

Leave a Comment

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