Hi Steven, That works perfectly, many thanks.
James On 29 May 2012 17:00, <[email protected]> wrote: > Send NLopt-discuss mailing list submissions to > [email protected] > > To subscribe or unsubscribe via the World Wide Web, visit > http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss > or, via email, send a message with subject or body 'help' to > [email protected] > > You can reach the person managing the list at > [email protected] > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of NLopt-discuss digest..." > > > Today's Topics: > > 1. Linking Issues Debian (James Barrett) > 2. Re: Linking Issues Debian (Steven G. Johnson) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 28 May 2012 17:20:14 +0100 > From: James Barrett <[email protected]> > To: [email protected] > Subject: [NLopt-discuss] Linking Issues Debian > Message-ID: > <caozou2+4xe7msydxtp4egr118qpbukwsgnanf0uixwbsqh4...@mail.gmail.com > > > Content-Type: text/plain; charset="iso-8859-1" > > Dear all, > > I am unable to compile my code with the nlopt library. My code is based on > the tutorial example (below). I compile with gcc -lnlopt main.c and get > several errors: > > main.c:(.text+0x2a): undefined reference to `nlopt_create' > main.c:(.text+0x41): undefined reference to `nlopt_set_lower_bounds' > main.c:(.text+0x57): undefined reference to `nlopt_set_min_objective' > main.c:(.text+0xa7): undefined reference to > `nlopt_add_inequality_constraint' > main.c:(.text+0xc8): undefined reference to > `nlopt_add_inequality_constraint' > main.c:(.text+0xdc): undefined reference to `nlopt_set_xtol_rel' > main.c:(.text+0x10f): undefined reference to `nlopt_optimize' > main.c:(.text+0x166): undefined reference to `nlopt_destroy' > > The library is installed in /usr/local/lib and the header files are at > /usr/local/include. My system administrator has added the library path > to `/etc/ld.so.conf'. > I'm running Debian GNU/Linux 6.0. My code is > > #include <stdio.h> > > #include <math.h> > > #include "nlopt.h" > > > double myfunc(unsigned n, const double *x, double *grad, > void*my_func_data); > > double myconstraint(unsigned n, const double *x, double *grad, void *data); > > > typedef struct { > > double a, b; > > } my_constraint_data; > > > int count = 0; > > > > int main(void){ > > > > > > double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */ > > nlopt_opt opt; /* create an `object' of type nlopt (an opaque pointer > type) */ > > > > opt = nlopt_create(NLOPT_LN_COBYLA, 2); /* algorithm and dimensionality > */ > > nlopt_set_lower_bounds(opt, lb); > > nlopt_set_min_objective(opt, myfunc, NULL); > > > > /* add two inequality constraints */ > > my_constraint_data data[2] = { {2,0}, {-1,1} }; > > nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8); > > nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8); > > > > nlopt_set_xtol_rel(opt, 1e-4); /* set stopping criterion */ > > > > double x[2] = { 1.234, 5.678 }; /* some initial guess */ > > double minf; /* the minimum objective value, upon return */ > > > > if (nlopt_optimize(opt, x, &minf) < 0) { > > printf("nlopt failed!\n"); > > } > > else { > > printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf); > > } > > > > printf("No of function evaluations = %d\n", count); > > > > nlopt_destroy(opt); > > > > } > > > > double myfunc(unsigned n, const double *x, double *grad, void > *my_func_data) > > { > > ++count; > > if (grad) { > > grad[0] = 0.0; > > grad[1] = 0.5 / sqrt(x[1]); > > } > > return sqrt(x[1]); > > } > > > > double myconstraint(unsigned n, const double *x, double *grad, void *data) > > { > > my_constraint_data *d = (my_constraint_data *) data; > > double a = d->a, b = d->b; > > if (grad) { > > grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b); > > grad[1] = -1.0; > > } > > return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]); > > } > > > Many thanks for your help, > James > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://ab-initio.mit.edu/pipermail/nlopt-discuss/attachments/20120528/ada4f026/attachment-0001.html > > > > ------------------------------ > > Message: 2 > Date: Mon, 28 May 2012 16:01:54 -0400 > From: "Steven G. Johnson" <[email protected]> > To: nlopt-discuss <[email protected]> > Subject: Re: [NLopt-discuss] Linking Issues Debian > Message-ID: <[email protected]> > Content-Type: text/plain; charset=iso-8859-1 > > > On May 28, 2012, at 12:20 PM, James Barrett wrote: > > I am unable to compile my code with the nlopt library. My code is based > on the tutorial example (below). I compile with gcc -lnlopt main.c and get > several errors: > > You need to compile with > > gcc main.c -lnlopt > > On Unix, the library has to come AFTER the program that uses it. (In > general, if A needs B, then A should come before B on the link line.) > > > ------------------------------ > > _______________________________________________ > NLopt-discuss mailing list > [email protected] > http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss > > > End of NLopt-discuss Digest, Vol 34, Issue 5 > ******************************************** > >
_______________________________________________ NLopt-discuss mailing list [email protected] http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
