include .cpp file instead of .h file?

D

desktop

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?
 
L

Lionel B

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.

What are you actually compiling? If you compile both func.cpp and
main.cpp (to object files) then, since main.cpp includes func.cpp, there
will indeed be definitions for your function in both object files and the
linker will complain. If you compile just main.cpp it should work.

But that's not generally the way to go...
Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?

Not essential, but it's common practice to write function *definitions*
in .cpp files and function *declarations* in header files, which are then
included where appropriate. An exception might be for inline functions,
where it is conventional to write both declaration and definition in the
same (header) file.
 
M

Mark P

desktop said:
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 ("translation 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
 
D

dragoncoder

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.
Thats because the linker finds the definition in func.o as well as
main.o. Remember ODR ?
Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
Not unless
1. You are linking func.o with main.o
2. You are linking main.o with any other file which also includes
func.cpp.

/P
 
W

WittyGuy

Not essential, but it's common practice to write function *definitions*
in .cpp files and function *declarations* in header files, which are then
included where appropriate. An exception might be for inline functions,
where it is conventional to write both declaration and definition in the
same (header) file.

I'd like to add some more to Lionel's words,
you can use #ifndef, #define, #endif for compiler to avoid any
accidental multiple inclusions of header files.

-
Sukumar R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top