Dopr853 multiple definition linking error


kirill_igum
06-20-2010, 07:23 PM
Hi, i'm using NR3(c++) to solve ode using rkDopr853 method

i call integration in bratoket.cc:
Output out(duration-1);
rhs_corr d(x0);
Odeint<StepperDopr853<rhs_corr> > ode(ystart,x1,x2,atol,rtol,h1,hmin,out,d);
ode.integrate();

bratoket.h contains:
#include "nr3.h"
#include "stepper.h"
#include "odeint.h"
#include "stepperdopr853.h"

no other file contains
#include "stepperdopr853.h"

when i link all the .o files, i get errors like this one

...
bratoket.o:(.rodata+0xa18): multiple definition of `Dopr853_constants::d76'
main.o:(.rodata+0x480): first defined here
bratoket.o:(.rodata+0xa20): multiple definition of `Dopr853_constants::d77'
main.o:(.rodata+0x488): first defined here
bratoket.o:(.rodata+0xa28): multiple definition of `Dopr853_constants::d78'
main.o:(.rodata+0x490): first defined here
bratoket.o:(.rodata+0xa30): multiple definition of `Dopr853_constants::d79'
main.o:(.rodata+0x498): first defined here
bratoket.o:(.rodata+0xa38): multiple definition of `Dopr853_constants::d710'
main.o:(.rodata+0x4a0): first defined here
bratoket.o:(.rodata+0xa40): multiple definition of `Dopr853_constants::d711'
main.o:(.rodata+0x4a8): first defined here
bratoket.o:(.rodata+0xa48): multiple definition of `Dopr853_constants::d712'
main.o:(.rodata+0x4b0): first defined here
bratoket.o:(.rodata+0xa50): multiple definition of `Dopr853_constants::d713'
main.o:(.rodata+0x4b8): first defined here
bratoket.o:(.rodata+0xa58): multiple definition of `Dopr853_constants::d714'
main.o:(.rodata+0x4c0): first defined here
bratoket.o:(.rodata+0xa60): multiple definition of `Dopr853_constants::d715'
main.o:(.rodata+0x4c8): first defined here
bratoket.o:(.rodata+0xa68): multiple definition of `Dopr853_constants::d716'
main.o:(.rodata+0x4d0): first defined here
make: *** [main] Error 1

errors come up for all dopr853 constants and other files between main and bratoket.

the errors don't come up if i use StepperBS or StepperDopr5. the errors also don't come up if i make the whole program with dopr5 and then change to dopr853 in the bratoket.h and .cc files and then make again.

putiing #ifndef ... doesn't help

can someone c a solution?

--Kirill

davekw7x
06-21-2010, 08:09 AM
...no other file contains
#include "stepperdopr853.h" Do you have a header file that includes stepperdopr853.h? Is that header included from more than one .cc file? (For example: Does bratoket.h include stepperdopr853.h and is bratoket.h included in more than one of your .cc files?)


#ifndef ... doesn't helpThat won't protect against including the same header file from other .cc files.

Each .cc file is compiled separately and a symbol table is created for that object file. When the linker puts all of the object files together it merges the symbol tables. If the same symbol is defined in more than one of the object file tables, the linker reports an error and no executable can be created.


Bottom line: With files from the Numerical Recipes Version 3 distribution you can't include header files like stepperdopr853.h from more than one .cc file since the header file actually declares memory storage.

It "just happens" that, since stepperdopr5.h just consists of some templates and doesn't contain any data definitions that would result in problems like this, you actually can get away with including it from more than one .cc file. But that's not true for many other NR3 header files.


If you are wondering why the package was put together that way:

See responses from one of the authors here: http://www.nr.com/forum/showthread.php?t=1332 and here: http://www.nr.com/forum/showthread.php?t=1260


Regards,

Dave

kirill_igum
10-01-2011, 01:14 PM
i didn't realize that i forgot to respond to this. thank you for the solution. it worked. i compiled all nr files together