I think your problem is not use of C and C++ but that file containing entire ClassX is not compiled and hence in the third stage in link time it gives error
gcc main.o function.o -o exec
Make sure that ClassX is defined in one .o file and that file is passed in above step.
Now coming to your question, you can't compile C++ code is C compilers because C is not forward compatible but C++ is backward compatible to C that is to say that most of the C code will get compiled by C++ compiler with little change. But you can write a library in c or compile c code to .o and then use it in C++ compiler to do that you need to use extern "C" linkage.
extern "C"
{
....
}
Check this link
Thanks