You are here:

C++/header file


i have created 3 functions in different files (i mean different main.c files) how can i create a header file and put all these functions in it as well as the main program that uses all these functions? i mean how can i create a header*.h file and put the functions and my program in it?Thanks a lot

It is not clear what you already have. You know that in one project (program), you cannot have 3 main functions. So I pressume you have 3 functions like add(), sub(), and mul(). If you so, you create a file called myheader.h had put the followings in it:

#ifndef MYHEADER
#defind MYHEADER
int add(int,int);
int sub(int,int);
int mul(int,int);


Note that the above is a sample. You should put your real functions instead.
Now you can either put the functions themselves in this header file (before #endif) or put them in a separate file. If you put them in a separate file (e.g myfunc.cpp), don't forget to put the followinh line at the begining:

#include "myheader.h"

Now create another file (say mymain.cpp) and put your main function in there. At the begining put the following:

#include "myheader.h"


All Answers

Answers by Expert:

Ask Experts




You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.


I'm a professional programer in C++ and network/internet programming.

©2017 All rights reserved.