command to compile and run the multiple file

Hi I am beginner in C++ and I am trying to create multiple files and executing them in gcc. Can any body please help me which command will be use to compile and run the program



/////main.cpp
#include "MyHeader.h"
int main()
{
sayHello();
return 0;
}

/////////MyHeader.cpp
#include <iostream>
#include "MyHeader.h"

void sayHello()
{
std::cout<<"Hello ! ";
}

////////MyHeader.h
#ifndef MYHEADER
#define MYHEADER

void sayHello();

#endif
Have you considered using an IDE such as Code::Blocks. That way the task of compiling multiple file projects would be somewhat simpler :)

EDIT: Or were you specifically wanting to do it from the command line?
Last edited on
Use the concept of Makefile for this...although you can do without it but its worth you know that...other wise use as below

Assuming all files are present in same directory:
g++ -c -I. MyHeader.cpp -o MyHeader.o
g++ -c -I. main.cpp -o main.o
g++ -o Output main.o MyHeader.o

Hope this solves your problem
Topic archived. No new replies allowed.