编译简单的Opt ++示例:对dswap_的未定义引用

时间:2016-06-16 21:47:33

标签: c++ c++11 optimization shared-libraries static-libraries

我下载了Opt++。我做了basic install

1. cd optpp-2.4
2. ./configure
3. make >& make.log
4. make check >& makecheck.log
5. make install

我在下面的代码中复制了Example 1: Unconstrained Quasi-Newton Without Derivatives。但是当我尝试编译时,即

g++ run_rosen.cpp rosen.cpp -Wno-write-strings -DHAVE_NAMESPACES -I/usr/local/include -I/usr/local/lib -lopt -lnewmat

我收到以下错误。我还认为有一个对我的下载目录的引用很奇怪。

//usr/local/lib/libopt.a(dqrdc.o): In function dqrdc:
/home/a/downloads/optpp-2.4/src/PDS/dqrdc.c: 156: undefind reference to dswap_
/home/a/downloads/optpp-2.4/src/PDS/dqrdc.c: 182: undefind reference to dswap_
...
//usr/local/lib/libopt.a(pdsdone.o): In function pdsdone:
/home/a/downloads/optpp-2.4/src/PDS/pdsdone.c: 128: undefind reference to dnrm2_
...

代码:

run_rosen.cpp

#include <fstream>
#include "rosen.h"
#include "NLF.h"
#include "OptQNewton.h"


using namespace OPTPP;

int main()
{
    int ndim = 2;
    FDNLF1 nlp (ndim, rosen, init_rosen);
    OptQNewton objfcn(&nlp);

    objfcn.setSearchStrategy(TrustRegion);
    objfcn.setMaxFeval(200);
    objfcn.setFcnTol(1.e-4);

    if (!objfcn.setOutputFile("rosen.out", 0))
    {
        cerr << "main: output file open failed" << endl;
    }

    objfcn.optimize();
    objfcn.printStatus("Solution from quasi-newton");
    objfcn.cleanup();
}

rosen.h

#ifndef ROSEN_H
#define ROSEN_H

#include "NLF.h"
#include "OptNewton.h"

void init_rosen(int ndim, NEWMAT::ColumnVector& x);

void rosen(int ndim, const NEWMAT::ColumnVector& x, double& fx, int& result);

#endif // ROSEN_H

rosen.cpp

#include "rosen.h"

void init_rosen(int ndim, NEWMAT::ColumnVector& x)
{
    if (ndim != 2)
    {
        exit(1);
    }

    x(1) = 1.2;
    x(2) = 1.0;
}

void rosen(int ndim, const NEWMAT::ColumnVector& x, double& fx, int& result)
{

    if (ndim != 2)
    {
        exit(1);
    }

    double f1, f2;
    f1 = (x(2) - x(1) * x(1));
    f2 = 1. - x(1);
    fx = 100. * f1 * f1 + f2 * f2;
    result = OPTPP::NLPFunction;
}

1 个答案:

答案 0 :(得分:2)

您需要链接BLAS库。尝试将其添加到构建命令的末尾:

-lblas