desktop wrote:
I have a main.cpp file that will call a single function. For the sake of
curiousity I would like to place this function in a separate file called
func.cpp and include it in my main.cpp with: #include "func.cpp". But it
gives a multiple definitions error.
Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
There's nothing inherently wrong about including a .cpp file since the
compiler doesn't care what the extension is on an included file.
However, you may be missing the bigger picture here. C++ supports
compilation of multiple separate files ("translatio n units" to be
precise). Among these, there are certain elements which may appear in
multiple translation units (e.g., function _declarations_) and other
elements which must appear in exactly one translation units (e.g.,
non-inlined function _definitions_). The reason one commonly uses .h
and .cpp files is that the .h files are typically used for the elements
which may appear multiply, and the .cpp files are typically used for the
elements which must appear once.
It sounds like you may be including the definition of your function in
two separate compilations-- for main.cpp and for func.cpp-- and that is
not allowed (the linker should complain). Show us what the files look
like and we can provide more specific advice.
Mark