May 2, 2011 at 6:30pm UTC
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
May 2, 2011 at 9:57pm UTC
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 May 2, 2011 at 9:59pm UTC
May 3, 2011 at 4:34am UTC
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