Compilation error

/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::MyDetectorConstruction()': construction.cc:(.text+0x0): multiple definition of MyDetectorConstruction::MyDetectorConstruction()‘; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0x0): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::DefineMaterial()': construction.cc:(.text+0x884): multiple definition of MyDetectorConstruction::DefineMaterial()’; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0x116): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::MyDetectorConstruction()': construction.cc:(.text+0x0): multiple definition of MyDetectorConstruction::MyDetectorConstruction()‘; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0x0): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::~MyDetectorConstruction()': construction.cc:(.text+0x822): multiple definition of MyDetectorConstruction::~MyDetectorConstruction()’; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0xb4): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::~MyDetectorConstruction()': construction.cc:(.text+0x822): multiple definition of MyDetectorConstruction::~MyDetectorConstruction()‘; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0xb4): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::~MyDetectorConstruction()': construction.cc:(.text+0x854): multiple definition of MyDetectorConstruction::~MyDetectorConstruction()’; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0xe6): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::Construct()': construction.cc:(.text+0x27e0): multiple definition of MyDetectorConstruction::Construct()‘; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0xbaa): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/construction.cc.o: in function MyDetectorConstruction::ConstructSDandField()': construction.cc:(.text+0x2b80): multiple definition of MyDetectorConstruction::ConstructSDandField()’; CMakeFiles/sim.dir/src/MyDetectorConstruction.cc.o:MyDetectorConstruction.cc:(.text+0x1376): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/detector.cc.o: in function MySensitiveDetector::~MySensitiveDetector()': detector.cc:(.text+0xa4): multiple definition of MySensitiveDetector::~MySensitiveDetector()‘; CMakeFiles/sim.dir/src/MySensitiveDetector.cc.o:MySensitiveDetector.cc:(.text+0x18a): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/detector.cc.o: in function MySensitiveDetector::~MySensitiveDetector()': detector.cc:(.text+0xa4): multiple definition of MySensitiveDetector::~MySensitiveDetector()’; CMakeFiles/sim.dir/src/MySensitiveDetector.cc.o:MySensitiveDetector.cc:(.text+0x18a): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/detector.cc.o: in function MySensitiveDetector::~MySensitiveDetector()': detector.cc:(.text+0xd6): multiple definition of MySensitiveDetector::~MySensitiveDetector()‘; CMakeFiles/sim.dir/src/MySensitiveDetector.cc.o:MySensitiveDetector.cc:(.text+0x1ce): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/detector.cc.o: in function MySensitiveDetector::ProcessHits(G4Step*, G4TouchableHistory*)': detector.cc:(.text+0x106): multiple definition of MySensitiveDetector::ProcessHits(G4Step*, G4TouchableHistory*)’; CMakeFiles/sim.dir/src/MySensitiveDetector.cc.o:MySensitiveDetector.cc:(.text+0x3ba): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::MyEventAction(MyRunAction*)': event.cc:(.text+0x0): multiple definition of MyEventAction::MyEventAction(MyRunAction*)‘; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x0): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::MyEventAction(MyRunAction*)': event.cc:(.text+0x0): multiple definition of MyEventAction::MyEventAction(MyRunAction*)’; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x0): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::~MyEventAction()': event.cc:(.text+0x36): multiple definition of MyEventAction::~MyEventAction()‘; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x4c): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::~MyEventAction()': event.cc:(.text+0x36): multiple definition of MyEventAction::~MyEventAction()’; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x4c): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::~MyEventAction()': event.cc:(.text+0x68): multiple definition of MyEventAction::~MyEventAction()‘; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x7e): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::BeginOfEventAction(G4Event const*)': event.cc:(.text+0x98): multiple definition of MyEventAction::BeginOfEventAction(G4Event const*)’; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0xae): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/event.cc.o: in function MyEventAction::EndOfEventAction(G4Event const*)': event.cc:(.text+0xb8): multiple definition of MyEventAction::EndOfEventAction(G4Event const*)‘; CMakeFiles/sim.dir/src/MyEventAction.cc.o:MyEventAction.cc:(.text+0x1aa): first defined here
/usr/bin/ld: CMakeFiles/sim.dir/src/MySensitiveDetector.cc.o:(.data.rel.ro._ZTV19MySensitiveDetector[_ZTV19MySensitiveDetector]+0x28): undefined reference to `MySensitiveDetector::EndOfEvent(G4HCofThisEvent*)’
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/sim.dir/build.make:339: sim] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/sim.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Everytime I am trying to compile these errors pop up and I can’t seem to find the source of this problem, I figured that this was a linking issue and made the necessary changes in the makefile but I am unaware of the changes I should make in the cmake file in order to compile without any errors popping up.

Please help.

I don’t think it’s CMake related, rather, the file construction.cc has a definition of MyDetectorConstruction::MyDetectorConstruction() that is also present in the file MyDetectorConstruction.cc.

Could you post these files and their associated construction.hh, MyDetectorConstruction.hh headers please, along with the CMakeLists.txt?

Sure I am attaching those files.
MyDetectorConstruction.cc (2.2 KB)
construction.cc (4.9 KB)
construction.hh (1.3 KB)
CMakeLists.txt (580 Bytes)
MyDetectorConstruction.hh (887 Bytes)

So should I delete the portion where I defined the constructor MyDetectorConstruction::MyDetectorConstruction() in construction.cc or should I delete it from MyDetectorConstruction.cc itself?

Which one is the file/definition that you want to use? Pick one then delete the other, along with the corresponding .hh file.

Okay so you are asking me to delete the constructor from the file where its not important.

Effectively yes, but it’s better to just the delete the relevant files directly because it’s the whole class not just the constructor that’s causing the conflict. So decide whether constructor.cc/.hh or MyDetectorConstruction.cc/.hh will define class MyDetectorConstruction, and delete the other pair.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.