LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2009, 12:20 PM   #1
map250r
Member
 
Registered: Aug 2007
Distribution: Debian Squeeze
Posts: 46

Rep: Reputation: 15
c++: gcc barfs with "Multiple definition of ...". What am I doing wrong?


I'm working on an app, and changed some data member declarations to be static. I also added definitions outside the class. Now gcc barfs with 'multiple definition of ...'

part of uiStuff.h:
Code:
#include "qoccharnesswindow.h"
#include <limits.h>
#include <QObject>
#include <QMenu>
#include <QToolBar>
#include <gp_Pnt.hxx>
#include <TopoDS_Shape.hxx>

class uiStuff : public QObject {
	Q_OBJECT
public:
	uiStuff(QoccHarnessWindow* window);
	uiStuff(){};
	~uiStuff(){};
protected:
	static QoccHarnessWindow* theWindow;
	static std::vector<TopoDS_Shape> selectedShapes;
	static int errors;
};

QoccHarnessWindow* uiStuff::theWindow = 0;
std::vector<TopoDS_Shape> uiStuff::selectedShapes;
int uiStuff::errors = 0;
g++ 4.3.3 output:

Code:
$ make
cd src/ && make -f Makefile                    
make[1]: Entering directory `/opt/cam-occ/trunk/src'
g++  -o ../bin/camocc2 .obj/main.o .obj/qoccapplication.o .obj/qoccdocument.o .obj/qoccharnesswindow.o .obj/qoccinputoutput.o .obj/qoccmakebottle.o .obj/qoccviewercontext.o .obj/qoccviewwidget.o .obj/shapeInfo.o .obj/cam.o .obj/uiStuff.o .obj/aSample.o .obj/shapeOffset.o .obj/gcode2Model.o .obj/moc_qoccapplication.o .obj/moc_qoccdocument.o .obj/moc_qoccharnesswindow.o .obj/moc_qoccinputoutput.o .obj/moc_qoccviewercontext.o .obj/moc_qoccviewwidget.o .obj/moc_shapeInfo.o .obj/moc_cam.o .obj/moc_uiStuff.o .obj/moc_aSample.o .obj/moc_shapeOffset.o .obj/moc_gcode2Model.o    -L/usr/X11R6/lib -L/usr/lib -L/opt/occ63/lib/ -lTKShHealing -lTKOffset -lTKBool -lTKSTEPBase -lTKSTEP -lTKService -lTKV3d -lTKernel -lTKIGES -lPTKernel -lTKSTL -lTKVRML -lTKTopAlgo -lTKBRep -lTKPShape -lTKShapeSchema -lXext -lX11 -lm -lGLU -lGL -lQtGui -lQtCore -lpthread                                 
.obj/cam.o: In function `__static_initialization_and_destruction_0':                                          
/usr/include/c++/4.3/ext/new_allocator.h:76: multiple definition of `uiStuff::errors'                         
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/cam.o: In function `__static_initialization_and_destruction_0':                                          
/usr/include/c++/4.3/ext/new_allocator.h:76: multiple definition of `uiStuff::theWindow'                      
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/uiStuff.o: In function `__static_initialization_and_destruction_0':                                      
/usr/include/qt4/QtCore/qglobal.h:1423: multiple definition of `uiStuff::theWindow'                           
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/uiStuff.o: In function `__static_initialization_and_destruction_0':                                      
/usr/include/qt4/QtCore/qstring.h:386: multiple definition of `uiStuff::errors'                               
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/aSample.o: In function `__static_initialization_and_destruction_0':                                      
/usr/include/qt4/QtCore/qatomic_x86_64.h:117: multiple definition of `uiStuff::theWindow'                     
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/aSample.o: In function `__static_initialization_and_destruction_0':                                      
/usr/include/qt4/QtCore/qglobal.h:1423: multiple definition of `uiStuff::errors'                              
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/shapeOffset.o: In function `TopoDS_Mismatch':                                                            
/opt/occ63/inc/Handle_Standard_Transient.hxx:97: multiple definition of `uiStuff::theWindow'                  
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/shapeOffset.o: In function `TopoDS_Mismatch':                                                            
/opt/occ63/inc/Handle_Standard_Transient.hxx:95: multiple definition of `uiStuff::errors'                     
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/gcode2Model.o: In function `__static_initialization_and_destruction_0':                                  
/usr/include/qt4/QtCore/qatomic_x86_64.h:129: multiple definition of `uiStuff::theWindow'                     
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/gcode2Model.o: In function `__static_initialization_and_destruction_0':                                  
/usr/include/qt4/QtCore/qatomic_x86_64.h:121: multiple definition of `uiStuff::errors'                        
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/moc_shapeInfo.o: In function `__static_initialization_and_destruction_0':                                
/opt/cam-occ/trunk/src/.moc/moc_shapeInfo.cpp:75: multiple definition of `uiStuff::selectedShapes'                                                                                                       
.obj/uiStuff.o:/usr/include/qt4/QtCore/qchar.h:370: first defined here                                        
.obj/moc_shapeInfo.o:/opt/cam-occ/trunk/src/.moc/moc_shapeInfo.cpp:48: multiple definition of `uiStuff::theWindow'                                                                                       
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/moc_shapeInfo.o: In function `__static_initialization_and_destruction_0':                                
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `uiStuff::errors'                       
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/moc_uiStuff.o: In function `__static_initialization_and_destruction_0':                                  
/usr/include/qt4/QtCore/qstring.h:670: multiple definition of `uiStuff::selectedShapes'                       
.obj/uiStuff.o:/usr/include/qt4/QtCore/qchar.h:370: first defined here                                        
.obj/moc_uiStuff.o:/usr/include/qt4/QtCore/qglobal.h:1423: multiple definition of `uiStuff::theWindow'        
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/moc_uiStuff.o: In function `__static_initialization_and_destruction_0':                                  
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `uiStuff::errors'                       
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here                                      
.obj/moc_aSample.o: In function `__static_initialization_and_destruction_0':                                  
/opt/cam-occ/trunk/src/.moc/moc_aSample.cpp:70: multiple definition of `uiStuff::selectedShapes'                                                                                                         
.obj/uiStuff.o:/usr/include/qt4/QtCore/qchar.h:370: first defined here                                        
.obj/moc_aSample.o:/opt/cam-occ/trunk/src/.moc/moc_aSample.cpp:45: multiple definition of `uiStuff::theWindow'                                                                                           
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here                                                  
.obj/moc_aSample.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `uiStuff::errors'
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here
.obj/moc_shapeOffset.o: In function `__static_initialization_and_destruction_0':
/opt/cam-occ/trunk/src/.moc/moc_shapeOffset.cpp:70: multiple definition of `uiStuff::selectedShapes'
.obj/uiStuff.o:/usr/include/qt4/QtCore/qchar.h:370: first defined here
.obj/moc_shapeOffset.o:/opt/cam-occ/trunk/src/.moc/moc_shapeOffset.cpp:45: multiple definition of `uiStuff::theWindow'
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here
.obj/moc_shapeOffset.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `uiStuff::errors'
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here
.obj/moc_gcode2Model.o: In function `__static_initialization_and_destruction_0':
/opt/cam-occ/trunk/src/.moc/moc_gcode2Model.cpp:70: multiple definition of `uiStuff::selectedShapes'
.obj/uiStuff.o:/usr/include/qt4/QtCore/qchar.h:370: first defined here
.obj/moc_gcode2Model.o:/opt/cam-occ/trunk/src/.moc/moc_gcode2Model.cpp:45: multiple definition of `uiStuff::theWindow'
.obj/shapeInfo.o:/opt/occ63/inc/gp.lxx:6: first defined here
.obj/moc_gcode2Model.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `uiStuff::errors'
.obj/shapeInfo.o:/opt/occ63/inc/Standard_Real.hxx:72: first defined here
collect2: ld returned 1 exit status
make[1]: *** [../bin/camocc2] Error 1
make[1]: Leaving directory `/opt/cam-occ/trunk/src'
make: *** [sub-src-make_default] Error 2
I have not examined *every* one of the headers where there's a supposed multiple definition, but in the ones I looked at I found nothing.
 
Old 02-23-2009, 12:25 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by map250r View Post
changed some data member declarations to be static. I also added definitions outside the class. Now gcc barfs with 'multiple definition of ...'
You can't have those definitions in multiple compilation units.

You put those definitions outside the class and inside a header, then included that header in multiple .cpp files and that is not permitted.

The usual correct approach is to put the definitions in a .cpp file rather than in a header file.

An alternative (that might be considered more maintainable code) is to wrap the definitions within the header file in some #ifdef and then define that symbol (before including that header) in exactly one compilation unit.

Last edited by johnsfine; 02-23-2009 at 12:28 PM.
 
Old 02-23-2009, 12:59 PM   #3
map250r
Member
 
Registered: Aug 2007
Distribution: Debian Squeeze
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnsfine View Post
You can't have those definitions in multiple compilation units.

You put those definitions outside the class and inside a header, then included that header in multiple .cpp files and that is not permitted.

The usual correct approach is to put the definitions in a .cpp file rather than in a header file.

An alternative (that might be considered more maintainable code) is to wrap the definitions within the header file in some #ifdef and then define that symbol (before including that header) in exactly one compilation unit.
Well I swear I tried that, but this time it worked.

Anyway, thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linking problem: "multiple definition of .." qanopus Programming 11 05-03-2012 03:04 AM
Can someone give me the definition of "Kernel Symbol" and "EXPORT_SYMBOL" ?? Raynus Programming 2 08-30-2008 07:19 PM
definition of the term "multi-hop network"!! vishamr2000 Linux - Networking 1 07-30-2007 03:53 PM
"US International" keyboard definition: solution found! pjotr123 Puppy 1 11-03-2006 08:00 AM
Porting to Fedora gcc 4.0 -parsing backward slash("\") comma(",") code_blew Programming 1 04-26-2006 04:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration