0

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
}

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

4

2 回答 2

0

鉴于您提供的信息,甚至很难猜出问题所在。但是,请确保 a) 您没有在另一个 cpp/h 文件中包含任何 cpp 文件。b)您在 h 文件中定义的任何实现都是内联的。(我猜 b 是你的问题)

于 2013-12-08T11:28:13.497 回答
0

您很可能包含一个在多个翻译单元中实现非内联方法的标头。Makefile 与它无关。您需要找到方法的定义,并查看它们最终是如何被包含到多个文件中的。如果它们实际上在头文件中,最简单的解决方法可能是将它们全部生成inline.

编译器看不到您将标头包含在多个翻译单元中,因为它总是一次只处理一个。当链接器看到各种目标文件时,它只会看到同一事物的许多定义并抱怨。不过,我会认为链接器指针位于定义的位置。

于 2013-12-08T10:58:47.147 回答