Search code examples
c++macosalglib

Undefined symbols for architecture x86_64: "alglib::spline2dcalc(alglib::spline2dinterpolant const&, double, double, alglib::xparams)"


#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "interpolation.h"

using namespace alglib;


int main(int argc, char **argv)
{
    //
    // We use bilinear spline to interpolate f(x,y)=x^2+2*y^2 sampled 
    // at (x,y) from [0.0, 0.5, 1.0] X [0.0, 1.0].
    //
    real_1d_array x = "[0.0, 0.5, 1.0]";
    real_1d_array y = "[0.0, 1.0]";
    real_1d_array f = "[0.00,0.25,1.00,2.00,2.25,3.00]";
    double vx = 0.25;
    double vy = 0.50;
    double v;
    double dx;
    double dy;
    double dxy;
    spline2dinterpolant s;

    // build spline
    spline2dbuildbicubicv(x, 3, y, 2, f, 1, s);

    // calculate S(0.25,0.50)
    v = spline2dcalc(s, vx, vy);
    printf("%.4f\n", double(v)); // EXPECTED: 1.0625

    // calculate derivatives
    spline2ddiff(s, vx, vy, v, dx, dy, dxy);
    printf("%.4f\n", double(v)); // EXPECTED: 1.0625
    printf("%.4f\n", double(dx)); // EXPECTED: 0.5000
    printf("%.4f\n", double(dy)); // EXPECTED: 2.0000
    return 0;
}

I want to use the alglib to do the bicubic spline interpolation, but when I use

g++ -I /Users/.../Documents/cpp/src -o a test.cpp

to run the code, it comes the eroor like Undefined symbols for architecture x86_64:

"alglib::spline2dcalc(alglib::spline2dinterpolant const&, double, double, alglib::xparams)", referenced from:
      _main in ccmNfxlB.o

how could I make it work?


Solution

  • You're missing the appropriate ALGLIB definitions.

    Solution: Compile ALGLIB's source (.h and .cpp files) to form a static library file and then add it when you call g++ alongside test.cpp so the compiler could find and use the missing definitions.

    The steps to accomplish that:

    First, put all of ALGLIB's source files (.h and .cpp files) into a separate folder, from which you should call:

    g++ -c *.cpp
    ar rcs alglib.a *.o
    

    Then, copy the alglib.a file to where your test.cpp resides, and from there call the compiler again but this time like this:

    g++ -I /Users/.../Documents/cpp/src -o a test.cpp alglib.a