[Solved] Multiple definition error on the same line


As some of the commentors mentioned it appears this kind of problem is most often caused by trying to compile the same file twice.

  • Including an implementation (.cpp) file is a quick way to do this.

  • Another way to compile a file twice is to include it twice in the project, which is what created this question. It happened in a sneaky way which did not manifest in the IDE, in this case, QT Creator.

In this case, arControls.h and arControls.cpp, shown in the question, were created, then explicitly added to the project by using the graphical IDE by right clicking on the folder then selecting “Add existing file”. The file showed up in the tree as expected. Adding the file this way had the effect of editing the project file. The IDE found an instance of SOURCES and tacked on the explicitly added file:

SOURCES     += $$COMMON/status/explicitfile1.cpp \
               $$COMMON/status/explicitfile2.cpp \
               $$COMMON/status/explicitfile3.cpp \
        ../src/arControls.cpp

Looking elsewhere in the project files, we find these lines:

SRC     = $$ROOT/src
INCLUDEPATH +=  $$SRC 
HEADERS     +=  $$SRC/*.h
SOURCES     +=  $$SRC/*.cpp

Earlier developers had used wildcards in the project file to include all files in the src folder. The added files had not shown up in the IDE when they were added manually, and the IDE never came forth with a hint that the file had been added to the project twice. Only one copy was visible in the source tree.

solved Multiple definition error on the same line