Undefined reference C++

43,916

Solution 1

You may be compiling skewNormal.cpp to a .o file, but you're not including it when you compile main.cpp.

Solution 2

Not sure if this is the problem, but it appears you're prototyping the function twice.

The double getSkewNormal(double skewValue, double x); line need only be in the header, not in main.cpp as well (since main.cpp includes skewNormal.h). Having it appear twice in a prototype form seems likely to confuse the compiler. You only need it to be visible to the code once, and usually that should be in a header (not a code file).

Try removing that line from main.cpp and recompile. :)

Share:
43,916
Ryan Amos
Author by

Ryan Amos

I graduated Dartmouth College in 2016 with a major in Computer Science. I'm currently a PhD candidate at Princeton University in security and privacy. I have worked as a research assistant in computational genomics, computational immunology, and password security. Languages I'm most familiar with are Java and Python, though I've written software in many others. All code in answers that I post on any StackExchange website are public domain, unless otherwise stated by me in the answer or comments to the answer. Do with them as you wish!

Updated on July 09, 2020

Comments

  • Ryan Amos
    Ryan Amos almost 4 years

    I'm trying to compile my first legit program that I'm converting from Java (I ran a test hello world type program to check my compiler and it works). There are three files:

    main.cpp

    #include <iostream>
    #include "skewNormal.h"
    
    using namespace std;
    
    double getSkewNormal(double, double);
    
    int main(int argc, char **argv) {
        cout << getSkewNormal(10.0, 0.5) << endl;
    }
    

    skewNormal.cpp

    #define _USE_MATH_DEFINES
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    #include <skewNormal.h>
    
    double SkewNormalEvalutatable::evaluate(double x)
    {
        return 1 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2);
    }
    
    SkewNormalEvalutatable::SkewNormalEvalutatable()
    {
    }
    
    double sum (double start, double stop,
                                   double stepSize,
                                   Evaluatable evalObj)
    {
      double sum = 0.0, current = start;
      while (current <= stop) {
        sum += evalObj.evaluate(current);
        current += stepSize;
      }
      return(sum);
    }
    
    double integrate (double start, double stop,
                                         int numSteps,
                                         Evaluatable evalObj)
    {
      double stepSize = (stop - start) / (double)numSteps;
      start = start + stepSize / 2.0;
      return (stepSize * sum(start, stop, stepSize, evalObj));
    }
    
    double getSkewNormal(double skewValue, double x)
    {
      SkewNormalEvalutatable e;
      return 2 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2) * integrate(-1000, skewValue * x, 10000, e);
    }
    

    skewNormal.h

    #ifndef SKEWNORMAL_H_INCLUDED
    #define SKEWNORMAL_H_INCLUDED
    
    class Evaluatable {
    public:
      virtual double evaluate(double x);
    };
    
    class SkewNormalEvalutatable : Evaluatable{
    public:
      SkewNormalEvalutatable();
      double evaluate(double x);
    };
    
    double getSkewNormal(double skewValue, double x);
    
    double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
    
    double sum (double start, double stop, double stepSize, Evaluatable evalObj);
    
    #endif // SKEWNORMAL_H_INCLUDED
    

    Compiling yielded the following error:

    main.cpp:9: undefined reference to `getSkewNormal(double, double)'
    

    I'm using Code::Blocks on Ubuntu 10.10.