NLopt Installation
From AbInitio
Revision as of 04:03, 12 November 2008 (edit) Stevenj (Talk | contribs) ← Previous diff |
Revision as of 05:30, 12 November 2008 (edit) Stevenj (Talk | contribs) Next diff → |
||
Line 60: | Line 60: | ||
Alternatively, in GNU/Linux systems, you can add the library directory to the system-wide file <code>/etc/ld.so.conf</code> and then (as root) run <code>/sbin/ldconfig</code>. | Alternatively, in GNU/Linux systems, you can add the library directory to the system-wide file <code>/etc/ld.so.conf</code> and then (as root) run <code>/sbin/ldconfig</code>. | ||
[[Category:NLopt]] | [[Category:NLopt]] | ||
+ | |||
+ | ==NLopt with C++ algorithms== | ||
+ | |||
+ | NLopt, as-is, is callable from C, C++, and Fortran, with optional Matlab and GNU Octave plugins. By default, it includes only subroutines written in C (or written in Fortran and converted to C), to simplify linking. If you configure with: | ||
+ | |||
+ | ./configure --with-cxx | ||
+ | |||
+ | however, it will also include algorithms implemented in C++ (currently, just the StoGO algorithm), and the resulting library will be called <code>libnlopt_cxx</code> and is linked with <code>-lnlopt_cxx</code>. | ||
+ | |||
+ | The <code>libnlopt_cxx</code> has the ''same'' interface as the ordinary NLopt library, and can ''still'' be called from ordinary C and Fortran programs. However, to use it you must also ''link'' with the C++ standard libraries. The easiest way to do this is to link with the C++ linker: compile your source files into <code>.o</code> object files, and then call the C++ compiler to link these <code>.o</code> files with <code>-lnlopt_cxx</code> into your executable program. | ||
+ | |||
+ | It is because this linking process is somewhat annoying, and it only adds a single more algorithm (StoGO) to NLopt, that by default we omit StoGO to create a library that does not require the C++ standard libraries to link. |
Revision as of 05:30, 12 November 2008
NLopt |
Download |
Release notes |
FAQ |
NLopt manual |
Introduction |
Installation |
Tutorial |
Reference |
Algorithms |
License and Copyright |
The installation of NLopt is fairly standard and straightforward, at least on Unix-like systems (GNU/Linux is fine). It doesn't require any particular packages to be installed except for a C compiler, although you need to have w:GNU Octave and/or Matlab installed if you want to install the Octave and/or Matlab plugins, respectively.
In particular, NLopt uses the standard w:GNU Autoconf configure
script, which means that you compile it via:
./configure make
in the nlopt
directory. Then, you would switch to be the roInsert code here
ot user, or use the sudo
command, to install the NLopt libraries and header files via:
make install
By default, this installs the NLopt static library (libnlopt.a
) in /usr/local/lib
and the NLopt header file (nlopt.h
) in /usr/local/include
, as well manual pages and a few other files.
In the following, we describe a few details of this installation process, including how to change the installation location.
Contents |
Changing the installation directory
You may wish to install NLopt in a directory other than /usr/local
, especially if you do not have administrator access to your machine. You can do this using the --prefix
argument to the configure
script.
For example, suppose that you want to install into the install
subdirectory of your home directory ($HOME
). You would do:
./configure --prefix=$HOME/install make make install
This will create the directories $HOME/install/lib
etcetera and install NLopt into them. However, now when you compile code using NLopt, you will need to tell the compiler where to find the NLopt header files (using -I
) and libraries (using -L
) with something like:
cc -I$HOME/install/include myprogram.c -L$HOME/install/lib -lnlopt -lm -o myprogram
Octave and Matlab plugins
When you compile NLopt using the above commands, it will automatically compile plugins for both Matlab and GNU Octave (a free Matlab clone) if the latter programs are installed.
In particular, for Matlab plugins to be installed, you need to have the Matlab mex
compiler command in your Unix PATH. Some versions of Matlab also require that you compile NLopt as a shared library in order to produce a Matlab plugin; see below.
For the Octave plugins to be installed, you need to have the Octave mkoctfile
program in your PATH. mkoctfile
is Octave's equivalent of mex
. If you are using a GNU/Linux system, and you installed Octave using one of the precompiled packages for your distribution, then you probably need to install a separate package to get mkoctfile
. For example, on Debian you need to install the octave-headers
package, and on Redhat you need the octave-devel
package.
Shared libraries
By default, NLopt compiles as a static library. This means that each program you link to NLopt will make a separate copy of the library, which wastes a little disk space. The alternative is to compile NLopt as a shared library (also called a dynamic-link library). While more efficient in terms of disk space etcetera, shared libraries require a bit more care to install properly, which is why we don't install them by default.
Compiling NLopt as a shared library is easy. Just add --enable-shared
to the configure
flags, as in:
./configure --enable-shared
Then you run make
and make install
as usual.
However, at this point you need to tell the operating system where to find the shared library, so that the runtime linker works properly. There are at least two ways to do this. First, you can use the LD_LIBRARY_PATH
environment variable. For example, if you installed into the /foo/bar
directory, so that the library is in /foo/bar/lib
, then you would do
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/foo/bar/lib
in the bash shell, or
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/foo/bar/lib
Alternatively, in GNU/Linux systems, you can add the library directory to the system-wide file /etc/ld.so.conf
and then (as root) run /sbin/ldconfig
.
NLopt with C++ algorithms
NLopt, as-is, is callable from C, C++, and Fortran, with optional Matlab and GNU Octave plugins. By default, it includes only subroutines written in C (or written in Fortran and converted to C), to simplify linking. If you configure with:
./configure --with-cxx
however, it will also include algorithms implemented in C++ (currently, just the StoGO algorithm), and the resulting library will be called libnlopt_cxx
and is linked with -lnlopt_cxx
.
The libnlopt_cxx
has the same interface as the ordinary NLopt library, and can still be called from ordinary C and Fortran programs. However, to use it you must also link with the C++ standard libraries. The easiest way to do this is to link with the C++ linker: compile your source files into .o
object files, and then call the C++ compiler to link these .o
files with -lnlopt_cxx
into your executable program.
It is because this linking process is somewhat annoying, and it only adds a single more algorithm (StoGO) to NLopt, that by default we omit StoGO to create a library that does not require the C++ standard libraries to link.