I want to know how to split sources and header files

Hello.
I'm a programmer in Korea.
I knew this site few days ago and I learned something.
Thank you~

I always confused to split header file and include them.



For example...
--------------------------------------------------------------
stdHeader.h
--------------------------------------------------------------
#define __STD_LIB__
#include <stdio.h>
...

--------------------------------------------------------------
stdMyFunc.h
--------------------------------------------------------------
#define __STD_MY_FUNC__
#include "myFunc1.h"
...

--------------------------------------------------------------
main.c
--------------------------------------------------------------
#ifndef __STD_MY_FUNC__
#include "stdMyFunc.h"
...
--------------------------------------------------------------


I wanted to use this sample to use only necessary header files.
But, I sometimes met troubles in redefine or something errors.

So, I used one header file with all headers needed.
and include one header to identify #define...

I think it is not good....
I want to know how to this or reference site.
Please help me.

My English skill is poor. maybe you don't understand... so sorry....

I'll expect to reply your answer! :)

Have a good time~~

Last edited on
you dont need alot of headers, for each .c file you can create a .h file. in the .h file you have all the declarations of the functions with some #define's or other const values which you will use in your .c file.
#include all the necessary headers in .h file and include the .h file in the .c/.cpp file. that would be fine.

otherwise to include put all the necessary headers in a common location you can include all headers in a common header file and include that common header in all the .c/.cpp files. but this is not the correct practice though.

actually there is not exact rules how you should make your .h files, it depends on the scenario's.
I generally make a seperate .h[pp]/.cpp file for each class that I make.

Like sharma said, you put the prototypes etc for the functions into the .h[pp], then put the definitons inside the .cpp.

Example:
main.cpp
1
2
3
4
5
6
7
#include <iostream>

int main() {
   myfunc();
   //blah blah
   return 0;
}


myfunc.h
1
2
3
4
5
6
7
8
#ifndef MYFUNC_H
#def MYFUNC_H

#include <iostream>

void myfunc(); //prototype

#endif 


myfunc.cpp
1
2
3
4
void myfunc() {
   std::cout<<"Hello World!"<<std::endl;
   return;
}
Last edited on
Thank writetonsharma, firedraco for answer to my question.
I commonly made like yours.
but, When I worked with teams, sources are bigger than with few programmers.
When I make some .h files and used theirs or standard .h, I'm confused how I divide or include that.

For example, a sampleA.c file use a, b, c, .... u.h and the b sample file use
f, g, h... z.h and so on...
We had many .h files.
And we used the common_header.h included a,b,c....z.h.

If I have to make some .c files(divide sources for maintenance or etc),
I was confused. because the common_header is not good I think.

If one header file has all headers by #define, useless header files were
included. I think it's not clear sources, so I ask you for solving this problems.


Does the one header have all headers is no problems ??

It's only depended on my owns or my team strategy?

Else almost programmer do like this?? (for easily maintenance)

Do I think useless things?



I really want to know the good plan to divide or include files.

Thank you for read my asking.
Anybody reply for me ~ Plz~~
Last edited on
If you have a single header that includes all the others, it isn't incredibly bad...I wouldn't do it, since it would waste time making sure the header files aren't already included, but it probably isn't enough to worry about.
including headers is not a very big issue, you can add your .h files in .cpp files to any extent. if you have many include all of them in .cpp..there isnothing to worry. even if you include them multiple times the
#ifndef

block at the top of header will not include them more than once. which firedraco has shown..

1
2
3
4
5
#ifndef MYFUNC_H
#def MYFUNC_H


#endif  


so dont worry about that..
Ok. I see. :D

Thank you everybody.

^_______^
Topic archived. No new replies allowed.