KcFnMi
KcFnMi

Reputation: 6171

Namespace use causes multiple definition of

I'm trying to make use of namespace but I'm getting a compilation error:

multiple definition of `B::Class1::Class1()

The following outlines the code structure I have here:

//.pro
SOURCES += ../main.cpp \
           ../a/class1.cpp \
           ../b/class1.cpp    
HEADERS += \
           ../a/class1.h \
           ../b/class1.h

// folder a, header
#ifndef CLASS1_H
#define CLASS1_H

namespace A {
class Class1 {
public:
    Class1();
};
}    
#endif // CLASS1_H

// folder a, source
#include "class1.h"    
using namespace A;

Class1::Class1() {
}

// folder b, header
#ifndef CLASS1_H
#define CLASS1_H

namespace B {
class Class1 {
public:
    Class1();
    void doB();
};
}    
#endif // CLASS1_H

// folder b, source
#include "class1.h"    
using namespace B;

Class1::Class1() {
}
void B::Class1::doB() {    
}

// main.cpp
#include "a/class1.h"
using namespace A;

int main(int argc, char *argv[]) {
    Class1 c;
}

What I'm doing wrong?

Here is the compilation output:

14:39:54: Running steps for project test...
14:39:54: Starting: "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" clean
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Debug clean
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Debug:133: warning: overriding recipe for target 'debug/class1.o'
Makefile.Debug:130: warning: ignoring old recipe for target 'debug/class1.o'
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Release:133: warning: overriding recipe for target 'release/class1.o'
Makefile.Release:130: warning: ignoring old recipe for target 'release/class1.o'
del debug\main.o debug\class1.o debug\class1.o
del release\main.o release\class1.o release\class1.o
Nao foi poss¡vel encontrar C:\Users\User\Downloads\namespace\build\release\main.o
Nao foi poss¡vel encontrar C:\Users\User\Downloads\namespace\build\debug\main.o
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
14:39:55: The process "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" exited normally.
14:39:55: Configuration unchanged, skipping qmake step.
14:39:55: Starting: "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" 
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Debug:133: warning: overriding recipe for target 'debug/class1.o'
Makefile.Debug:130: warning: ignoring old recipe for target 'debug/class1.o'
g++ -c -pipe -fno-keep-inline-dllexport -std=c++1y -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtWidgets" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtGui" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtCore" -I"debug" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\mkspecs\win32-g++" -o debug\main.o ..\main.cpp
g++ -c -pipe -fno-keep-inline-dllexport -std=c++1y -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtWidgets" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtGui" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtCore" -I"debug" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\mkspecs\win32-g++" -o debug\class1.o ..\b\class1.cpp
..\main.cpp:5:5: warning: unused parameter 'argc' [-Wunused-parameter]
 int main(int argc, char *argv[]) {
     ^
..\main.cpp:5:5: warning: unused parameter 'argv' [-Wunused-parameter]
g++ -Wl,-subsystem,windows -mthreads -o debug\test.exe debug/main.o debug/class1.o debug/class1.o  -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -LC:/Qt/Qt5.3.2/5.3/mingw482_32/lib -lqtmaind -LC:\mingw482\mingw32\lib -LC:\Utils\icu32_52_1_mingw482\lib -LC:\utils\postgresql\pgsql\lib -LC:\utils\mysql\mysql\lib -LC:\opensll\lib -LC:\Qt\Qt5.3.2\5.3\mingw482_32/lib -lQt5Widgetsd -lQt5Guid -lQt5Cored 
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class13doBEv':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:7: multiple definition of `B::Class1::doB()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:7: first defined here
debug/main.o: In function `main':
C:\Users\User\Downloads\namespace\build/../main.cpp:6: undefined reference to `A::Class1::Class1()'
collect2.exe: error: ld returned 1 exit status
Makefile.Debug:81: recipe for target 'debug\test.exe' failed
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
Makefile:34: recipe for target 'debug' failed
mingw32-make[1]: *** [debug\test.exe] Error 1
mingw32-make: *** [debug] Error 2
14:39:55: The process "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project test (kit: Desktop Qt 5.3 MinGW 32bit)
When executing step "Make"
14:39:55: Elapsed time: 00:01.

Upvotes: 0

Views: 823

Answers (1)

aschepler
aschepler

Reputation: 72431

It looks like qmake doesn't support files with the same name in different directories. It ends up trying to build both a/class1.cpp and b/class1.cpp into the same debug/class1.o file, and then link that file twice.

Try renaming one of your *.cpp files.

Upvotes: 3

Related Questions