Agrim Pathak
Agrim Pathak

Reputation: 3207

Compiling simple Opt++ example: Undefined reference to dswap_

I downloaded Opt++. I did the basic install:

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

I replicated Example 1: Unconstrained Quasi-Newton Without Derivatives in the codes below. But as I try to compile, i.e.

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

I get the errors below. And I also think it's odd that there's a reference to my downloads directory.

//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_
...

Codes:

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

Upvotes: 0

Views: 293

Answers (1)

John Zwinck
John Zwinck

Reputation: 249444

You need to link against a BLAS library. Try adding this to the end of your build command:

-lblas

Upvotes: 2

Related Questions