This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(34个答案)


2年前关闭。



#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;
}

我想使用alglib进行双三次样条插值,但是当我使用
g++ -I /Users/.../Documents/cpp/src -o a test.cpp

运行代码,就像
架构x86_64的 undefined symbol :
"alglib::spline2dcalc(alglib::spline2dinterpolant const&, double, double, alglib::xparams)", referenced from:
      _main in ccmNfxlB.o

如何使它起作用?

最佳答案

您缺少适当的ALGLIB定义。

解决方案:编译ALGLIB的源文件(.h和.cpp文件)以形成静态库文件,然后在您与test.cpp一起调用g++时将其添加,以便编译器可以找到并使用缺少的定义。

完成该步骤的步骤:

首先,将ALGLIB的所有源文件(.h和.cpp文件)放入单独的文件夹,您应从该文件夹调用:

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

然后,将alglib.a文件复制到test.cpp所在的位置,然后从那里再次调用编译器,但这一次是这样的:
g++ -I /Users/.../Documents/cpp/src -o a test.cpp alglib.a

关于c++ - 架构x86_64的 undefined symbol : “alglib::spline2dcalc(alglib::spline2dinterpolant const&, double, double, alglib::xparams)” ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51855264/

10-13 02:32