I can't seem to get rid of these seemingly random compiles errors in one of my classes. I get about 4 errors such as:
multiple definition of `draw_line(float, float, float, float)'
and
multiple definition of `near_far_clip(float, float, float*, float*, float*, float*, float*, float*)'
that are flagged in the middle of the method.
I also consistently get multiple definition of `stack'
in the middle of another method. stack
is a global variable in a totally different file. It isn't even mentioned in the file I'm getting the error in.
I tried separating the error prone file into .h and .cpp files (was originally just a .cpp) and nothing about the error changed...
I don't have duplicate methods. I only have one #include
of lines.h and there is an #ifndef
clause at the beginning. All these errors appear in the .cpp file.
Any ideas what it could be?
Alright, I got the code up:
The lines.cpp is a converted .c file I received from my instructor. I included the makefile just in case, because I always have problems with it. I also annotated exactly where the errors were flagged in the file, but they seem pretty random so I don't know if it's particularly important. I abandoned the .h file because it wasn't solving anything or helping. I believe it will be easier to find the mistake without it.
Here is the requested main.cpp file (there is no .h).
I remade the lines.h file due to and I'm still receiving the:
multiple definition of `draw_line(float, float, float, float)'
and
multiple definition of `near_far_clip(float, float, float*, float*, float*, float*, float*, float*)'
errors in the lines.cpp file, but the multiple definition of `stack'
error is now in a random place in the ThreeD.cpp file (and is marked by a comment now). Update: This error has been fixed and the files have been revised to show this:
I messed around with labeling some the global variables extern, but it didn't seem to affect anything.
You are likely including the function definitions in a header file. Include the inline
keyword so they are not exported by each object file. Alternatively, put the functions in their own .cpp
file.
For your global variable, you need to use the extern
keyword in your header file. Otherwise, each object file exports their own variable and the linker gets confused as to which is the correct one to use.
Why do you #include lines.cpp in ThreeD.cpp? This is very unusual.
Your makefile wants lines.o, so you're going to compile lines.cpp. Anything defined in lines.cpp will be in lines.o and also in ThreeD.o.
There is an intriguing comment in lines.cpp:
Don't forget to put declarations in your .h files.
I think the instructor wants you to break lines.cpp into a .h and a .cpp.
Excerpt from lines.cpp:
/* These go in your .h file or in lines.h */
/*
Line drawing header.
*/
void draw_line(float, float, float, float);
int near_far_clip(float, float, float *, float *, float *, float *,
float *, float *);
I suspect that these two declarations are the only thing that should be in lines.h.
Please post some code snippets. Maybe you are defining your methods in both the class declaration and outside?
class X {
void foo(); // No definition, just declaration
void bar() {} // Declaration + definition
};
void X::foo() {} // First Definition, OK
void X::bar() {} // Already defined, ERROR
As has been said - not enough information to properly diagnose here.
But if draw_line
et al are defined in a header file rather than in a source (cpp) file, you may have methods which are supposedly declared as inline in the header file, which aren't actually properly inlining. In that case, each .cpp file that includes the header will generate their own definition of the draw_line
function, and generate warnings at link time.
This can happen if you use a #defined INLINE macro which comes from a system header that has been forgotten or removed, and for whatever reason the INLINE gets pre-processed away into nothing.
E.g.:
//Lines.h
#define GCCC //Note the typo
#if defined(GCC)
#define INLINE inline
#elif defined (MSVC)
#define INLINE __inline
#else
#define INLINE //Due to the typo, INLINE will be turned into nothing
#endif
INLINE void draw_line(float x1, float y1, float x2, float y2)
{
//Draw the line
}
//File1.cpp
#include "lines.h" //will define draw_line
//File2.cpp
#include "lines.h" //will also define draw_line
And linking File1.cpp and File2.cpp will generate multiple declaration errors
Check include guards on misspelling.
Check your make options, maybe someone compiled into multiple object files.
Try to exclude parts of files and code until not find cause of errors.
EDITED:
fix include *.cpp files. They are should be linked.
#ifndef THREED_H_
#define THREED_H_
#endif /* THREED_H_ */
delete or comment these lines and this worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With