1.5. Controlling dynamic/static linking against LibXC and LibInt2
setup.py also compiles C++ code into Python extensions. Some of
these extension are linked against LibXC, LibInt2 or a combination of both. The
setup.py script attempts to detect all of the compiler and linker settings for these
For each library, following attempts are made (in the given order) to detect
the compiler and linker flags. If the attempt succeeds, i.e. gives a set of
satisfactory flags, further steps are not considered for that library.
setup.cfg in the root of the source tree for compilation and link
options for that library, as documented in setup.cfg and environment variables. Read in
environment variables for compilation and link options for that library.
- Statically link libraries in the
depends directory (for LibXC and LibInt2).
- Use program
pkg-config to get all of the compiler and linker flags
for each library. See http://www.freedesktop.org/wiki/Software/pkg-config/ for
setup.*.cfg file in the directory
corresponds to your operating system and CPU architecture for compilation and
link options for that library.
- Dynamically link libraries using default library names:
for LibXC and LibInt2, respectively.
However, if these steps do not result in decent compiler/linker options for some
library or the dependencies are not installed, then the compilation will fail
and you will get a compiler error message.
The following sections explain how you can override the default guesses of the
setup.cfg and environment variables
For each library, a section can be added to the file
setup.cfg to configure
the compiler and linker options. By default, no such file is present in the root
of the source tree, so you have to create a new one. Several examples can be
data/setup_cfgs. For example, this file would compile and link the
extensions on a 64 bit version of Fedora 21:
In each section you can define the following variables:
extra_link_args. They correspond to the optional arguments of the
Extension class of the Distutils package, as described here:
The purpose of each keyword is summarized below:
- A list of non-standard directories containing C/C++ header files
- A list of non-standard directories with shared objects (dynamic link
libraries). When any of the
*_LIBRARY_DIRS variables or the
setup.py build_ext are used, keep in mind that at runtime, the
dynamic link loader must be informed of these directories. For example, on
Linux, the variable
LD_LIBRARY_PATH must be set accordingly.
- A list of shared objects to link with.
- Extra object files for static linking.
- Extra arguments given to the compiler when compiling source code.
- Extra arguments given to the compiler when linking object files.
Multiple paths or names for one keyword are separated by a colon (
Instead of setting the library configuration in the file
setup.cfg, you may
also set the following environment variables:
1.5.3. Other ways of controlling compilation and linker flags
Instead of the library-specific variables above, there are also general methods
to configure the compiler and linker.
You may specify a colon-separated list of non-standard directories with
include files in the
CPATH environment variable. For example:
In addition, general compiler and linker flags may be set in the
stage of the installation process. To do so, the installation must be done
in two steps:
./setup.py build_ext EXTRA OPTIONS HERE
./setup.py install --user
./setup.py build_ext --help for a complete list of options. These
options apply to all extensions, so avoid them for static linking.