문제

Believe me, I've been through several posts on here, but none of them addressed the issue I'm having. I have this 2-year-old program that used to run. I'm kind of reviving it, but for some reason now it does not run.

Clearly, I'm having multiple definitions (too many of them):

============================ TERMINAL OUTPUT =============================

build_files/LinkedStack.o: In function `LinkedStack':
/home/owner/workspace/opencv-galaxies/utilities/structures/LinkedStack.cpp:12: multiple definition of `LinkedStack::LinkedStack()'
build_files/LinkedStack.o:/home/owner/workspace/opencv-galaxies/utilities/structures/LinkedStack.cpp:12: first defined here

... and so on, and so forth, ... and it all ends with:

collect2: ld returned 1 exit status
make: *** [executables/Assignment3.out] Error 1

========================================================================

Strangely, the linker does not indicate any errors throughout the extensive list of warnings, not to mention that these aren't true multiple definitions. Note that each warning in a "multiple...-first defined... " pair refers to the same line. Now I don't know what to do.

I'm wondering if it has something to do with the rather busy syntax of our makefile (though it looks really good to me):

=============================== MAKEFILE =================================

CFLAGS = -g -Wno-deprecated

OBJECTS = utilities/basic/image.h build_files/image.o build_files/ReadImage.o build_files/ReadImageHeader.o build_files/WriteImage.o build_files/LinkedStack.o build_files/unsortedList.o build_files/region.o build_files/Main.o

executables/Assignment3.out: $(OBJECTS)
    g++ $(OBJECTS) -o executables/Assignment3.out build_files/*.o $(CFLAGS) -lncurses

...

build_files/LinkedStack.o:  utilities/structures/LinkedStack.h utilities/structures/LinkedStack.cpp
    g++ -c $(CFLAGS) utilities/structures/LinkedStack.cpp -o build_files/LinkedStack.o

...

clean:
            rm build_files/*.o executables/Assignment3.out

=========================================================================

So, these are my questions: 1) why did the linker see an error and 2) why am I having so many multiple definitions?

If you want a clarification, let me know even if you kind of have an idea of what's going on.

============================== CODE EXAMPLE ==============================

Here's the full example function (I don't want to make this too long):

//constructor
LinkedStack::LinkedStack()
{
    topPtr = NULL; //set top pointer to null
}

========================================================================

도움이 되었습니까?

해결책

Most likely you are including a header which implements methods non-inline in multiple translation units. The Makefile has nothing to with it. You'll need to find the definition of the methods and see how they end up being included into multiple files. If they are actually in a header file the easiest fix is probably to make them all inline.

The compiler doesn't see that you are including the header into multiple translation units as it always only processes one at a time. When the linker sees the various object files, it just see many definitions of the same thing and complains. I would have thought that the linker pointer at the location of the definition, though.

다른 팁

Given the info you provided, it is hard to even guess what the problem is. However, make sure that a) you do not include any cpp file in another cpp/h file. b) any implementation you define in h file, is inline-ed. (I would guess b is your problem)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top