Headers with Multiple .cpp Files

So I have a rather large (for me) project, requiring me to have two .cpp files and a header. At least, I think this is what I need, but I'm not completely sure. Anyway, both of the .cpp files #include the header file, but I recieve linker errors because the variables and functions in the header are declared and defined twice (once in each .cpp file). How am I supposed to do this?

Thank you!
A function shouldn't have multiple definitions in each cpp file. Otherwise, when you call it, which version are you referring to? You should have exactly one.

Perhaps if you gave a bit more detail I could give more advice.
Sorry, I wasn't very clear there. (this is as far as I understand it) Because I #include the header file, the contents of the header file are copy\pasted into the .cpp files. Then it (I think) is having a fit, because both .cpp files have the function definitions in them.

It sounds like there are definitions in the header file that should only be in the .cpp file.
But since I have two of them, and I want both of them to be able to use the variables (do calculations, modify the values, etc), what am I supposed to do?

You don't need to define them again in each source file. As long as a file includes the header, and the functions are defined somewhere in your source, the file will be able to call them.
The usual approach is to create a cpp file for each header, containing the definitions of the functions.

An exception to this are definition of templates.
┬┐do you mean evil global variables?
mark them as extern (shared)
Last edited on
Okay. This makes a lot more sense!

Thank you everyone!
Topic archived. No new replies allowed.