Thread: Error compiling...

  1. #1
    Registered User
    Join Date
    Sep 2006

    Question Error compiling...

    I'm comipling a program and it appears the next error:

    /tmp/ccDKjdG3.o: In function `main':
    /tmp/ccDKjdG3.o(.text+0x3e3): undefined reference to `NR::bessj(int, double)'
    collect2: ld returned 1 exit status

    Could anyone explain me this?

    NR::bessj(int, double) is a 'class' created in a file different from the main one.


  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    You should post your code so we can see what's the problem.
    It appears that
    NR::bessj(int, double)
    bessj method of class NR is not defined and you're trying to call it.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Sep 2006
    OK. So here is the main code.
    Thank you.

    #include <iostream>
    #include <iomanip>
    #include "nr.h"
    using namespace std;
    using std::fixed;
    // Driver for routine odeint
    DP dxsav;  // defining declarations
    int kmax,kount;
    Vec_DP *xp_p;
    Mat_DP *yp_p;
    int nrhs;   // counts function evaluations
    void derivs(const DP x, Vec_I_DP &y, Vec_O_DP &dydx)
      dydx[0] = -y[1];
      dydx[1] = y[0] - (1.0/x)*y[1];
      dydx[2] = y[1] - (2.0/x)*y[2];
      dydx[3] = y[2] - (3.0/x)*y[3];
    int main(void)
      const int N = 4, KMAX = 100;
      int i, nbad, nok;
      DP eps = 1.0e-4, h1 = 0.1, hmin = 0.0, x1 = 1.0, x2 = 10.0;
      Vec_DP ystart(N);
      ystart[0] = NR::bessj0(x1);
      ystart[1] = NR::bessj1(x1);
      ystart[2] = NR::bessj(2,x1);
      ystart[3] = NR::bessj(3,x1);
      nrhs = 0;
      dxsav = (x2-x1)/20.0;
      kmax = KMAX;
      xp_p = new Vec_DP(KMAX);
      yp_p = new Mat_DP(N,KMAX);
      Vec_DP &xp = *xp_p;
      Mat_DP &yp = *yp_p;
      cout << endl << "successful steps:" << setw(14) << " ";
      cout << setw(4) << nok << endl;
      cout << "bad steps:" << setw(21) << " " << setw(4) << nbad << endl;
      cout << "function evaluations:" << setw(10) << " ";
      cout << setw(4) << nrhs << endl;
      cout << endl << "stored intermediate values:    ";
      cout << setw(4) << kount << endl;
      cout << endl << setw(8) << "x" << setw(18) << "integral";
      cout << setw(16) << "bessj(3,x)" << endl;
      //  cout << fixed << setprecision(6);
      for (i=0;i<kount;i++) {
        cout << setw(10) << xp[i] << setw(16) << yp[3][i];
        cout << setw(15) << NR::bessj(3,xp[i]) << endl;
      delete yp_p;
      delete xp_p;
      return 0;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > ystart[2] = NR::bessj(2,x1);
    Maybe ystart[2] = NR::bessj(2.0,x1);

    The error message basically stated that there was no NR::bessj(int, double)
    That is, an int parameter and a double parameter.

    C++ overloading means that you have to match parameters more precisely.

    In C, if the function accepted a double, your 2 would be implicitly converted to a double, and the function would be called.
    C++ just complains there isn't an exact match and leaves you to figure it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Given that the error is from the linker, not the compiler, I suspect your response is mistaken Salem.

    If the issue was one of needing to convert 2 to a double, the compiler would complain about being unable to find a function to call when it sees ystart[2] = NR::bessj(2,x1). The linker would never be invoked.

    The fact that the linker is complaining suggests that a function NR::bessj(int, double) is declared somewhere (presumably in a header file), so the code gets past the compiler, but that the function is not defined (i.e. no implementation of the function is supplied). The solution is to ensure that the linker receives an object file or library file that contains the (compiled) function.

    If you have the source file that contains the implementation of NR::bessj(int, double), compile it first to create an object file, and then link.
    Last edited by grumpy; 09-12-2006 at 05:28 AM.

Popular pages Recent additions subscribe to a feed