"Multiple definition" error caused by xxxDict.cxx

I’m learning programming with ROOT. And I have two basic questions after encountered some compiling errors:

(background: project name is “mycode”; header file is “myclass.h” which defines a global object array like this, “class myclass {…} myobj[100]”; subroutine “myclass.C” provides the memeber functions of “myclass” which has myclass.h included, of course; main program is “mycode.cxx”)

  1. When I copied Makefile from ROOT test programs, I found xxxDict.cxx. what’s the function of xxxDict.cxx? Because I found my program could be compiled without creating this dictionary code, so I deleted it from my Makefile.

So why does it exist?

  1. Now I want to write my own classes into files, and I found in the User’s Guide that I needed “streamer” method, which is defined in mycodeDict.cxx and was created by rootcint automatically. But after I added mycodeDict.cxx creation and compilation lines into my Makefile, I got “multiple definition” error:

myclass.o: first defined here
mycodeDict.o: multiple definition of ‘myobj’

  1. My intuition tells me that my problems with ROOT are very simple ones in the eyes of experts and they’re also entangled with my lack of C++ programming experiences and a detailed manual can save me from asking them in this forum.

So, where can I find more detailed manuals on “programming with ROOT/C++” beside the “Guide”?

Thanks for you help!

Hello.

C++ has ODR == One Definition Rule.
And C++ has definitions, and declarations.

Examples:
1.
//sample.cxx
int i;
int i;//you’ve violate one definition rule, the are both definitions
2.

//file1.cxx
int i;

//file2.cxx
int i; //again, if you compile these files in one project you have
ODR violation, they are both definitions.

////////////////////////////////////////////////////////////
//definitions and declarations
3.
int i; //definition
extern int i;//declaration
//now everething is OK.

So, solution:

//YourClass.h
class YourClass{//};
//if you need the name of array and you’re going to include this
//header in more than one .cxx file, you need to DECLARE (not define)
extern YourClass arr[100];

//YourClass.cxx
//now, here you need to DEFINE array
#include "YourClass.h"
YourClass arr[100];//it’s a definition.

P.S.
To learn C++ you’d better read some good C++ book (“The C++ Programming Language” by Bjarne Stroustrup, for example). And you’d better use real c++ compiler.

Hi.

[quote=“weiswang”]1. When I copied Makefile from ROOT test programs, I found xxxDict.cxx. what’s the function of xxxDict.cxx? Because I found my program could be compiled without creating this dictionary code, so I deleted it from my Makefile.

So why does it exist?[/quote]
xxxDict.{cxx,h} contain the dictionary for your classes. This dictionary is needed for three major reasons:
a) object persistency. When writing objects to a file, or reading them back, someone has to know what to write. What does a class MyClass consist of? What needs to be written and read? That’s in the dictionary.
b) If you enter “new MyClass(42)” on the cint command line, cint needs to find out what “MyClass” is, and how to call its constructor taking an int. So it needs to translate from the string (as in “const char*”) “new MyClass(42)” to something that it can call - and again, this translation is done with the help of the dictionary.
c) some infrastructure depends on the dictionary (Dump() to show an object’s contents, the current THtml to generate documentation for a class,…).

[quote=“weiswang”]2. Now I want to write my own classes into files, and I found in the User’s Guide that I needed “streamer” method, which is defined in mycodeDict.cxx and was created by rootcint automatically. But after I added mycodeDict.cxx creation and compilation lines into my Makefile, I got “multiple definition” error:

myclass.o: first defined here
mycodeDict.o: multiple definition of ‘myobj’[/quote]
This is not the real error message. It probably complains about a method that’s implemented in the dictionary, and that you implemented yourself in your source file, too. Send the actual error output, and header and source of your class (we can generate the dictionary ourselves).

See the “books on C++” posting at http://root.cern.ch/phpBB2/viewtopic.php?t=1746.

Please avoid cross posting. If you’ve posted to the wrong forum, then please do NOT re-post to another forum. It’s much better to have a posting in the wrong forum than to have it twice.
Axel.

Thanks a lot for the suggestions!

Problem solved after I used “extern” to change the definitions into declarations.

There are definitions in my header file but it’s only included by one of the subroutines so it was not a problem. But after putting in mycodeDict.cxx, those definitions got duplicated thus produced “multiple definition” error during compiling.

Sorry about the multi-posting—I didn’t realize that I can’t delete my own posts until I posted the 2nd time.