how to merge two C/C++ files?

Can anybody help me to merge two C files using awk, sed or anything else? Thanks!

File 1:

#ifdef HDEADER1
#include �header1.h�
#endif
 
Fun1()
{
}
 
 

File2:

#ifdef HDEADER2
#include �header2.h�
#endif
 
Fun2()
{
}
 

Merged file:

#ifdef HDEADER1
#include �header1.h�
#endif
 
#ifdef HDEADER2
#include �header2.h�
#endif
 
Fun1()
{
}
 
Fun2()
{
}

Something like this if the order of the functions doesn't matter:

awk '/#endif/{print $0 RS; system("cat file2";print "";next}1' file1 > newfile

Thank you Frank. However not all of my headers are guarded with the macros. Anyway I found another way to do that.