Yamaneko
Yamaneko

Reputation: 3563

Build program using library and headers installed in a conda environment

I have OpenCV 3.2.0 installed in a conda environment. I would like to use it to build an application, but I'm not sure if conda already provides a tool to automatically handle which dependencies are visible considering the active environment.

For instance, the OpenCV library is in the following paths:

/home/me/anaconda2/envs/python3/lib/libopencv_imgproc.so.3.2
/home/me/anaconda2/envs/python3/lib/libopencv_optflow.so.3.2
/home/me/anaconda2/envs/python3/lib/libopencv_freetype.so.3.2
...

and headers are at:

/home/me/anaconda2/envs/python3/include/opencv2/core.hpp
/home/me/anaconda2/envs/python3/include/opencv2/ximgproc.hpp
...

I can resolve this by passing the flags directly using

-I/home/me/anaconda2/envs/python3/include/ -L/home/me/anaconda2/envs/python3/lib

or

LDFLAGS="-L/home/me/local/lib" CFLAGS="-I/home/me/local/include" make

But, is this the best way? I saw references to conda build, but seeing the docs, it seems more suited to build conda packages.

Upvotes: 0

Views: 4195

Answers (1)

darthbith
darthbith

Reputation: 19646

Indeed, conda build is for building conda packages. You can have environment variables set automatically when you activate an environment, see here: https://conda.io/docs/using/envs.html#saved-environment-variables

As you noted,

It seems conda sets the $LIBRARY_PATH variable automatically. See anaconda2/envs/<ENV>/etc/conda/activate.d/pygpu_vars.sh. I recently noticed that I was able to compile my program using OpenCV from my conda. I thought I was using the system's library, but it wasn't working until I activated the environment.

It is likely that a package created that script (maybe the pygpu package?) such that when you activate the environment, the proper variables are set to use the libraries provided with the package. It is not guaranteed that all packages will do this though...


Example based on envs/python3/etc/conda/activate.d/pygpu_vars.sh:

export CPATH_PYGPU_BACKUP="$CPATH"
export CPATH=anaconda2/envs/python3/include:"$CPATH"
export LIBRARY_PATH_PYGPU_BACKUP="$LIBRARY_PATH"
export LIBRARY_PATH=anaconda2/envs/python3/lib:"$LIBRARY_PATH"
export LD_LIBRARY_PATH_PYGPU_BACKUP="$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=anaconda2/envs/python3/lib:"$LD_LIBRARY_PATH"

Deactivate script at envs/python3/etc/conda/deactivate.d/pygpu_vars.sh:

export CPATH="$CPATH_PYGPU_BACKUP"
unset CPATH_PYGPU_BACKUP
export LIBRARY_PATH="$LIBRARY_PATH_PYGPU_BACKUP"
unset LIBRARY_PATH_PYGPU_BACKUP
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH_PYGPU_BACKUP"
unset LD_LIBRARY_PATH_PYGPU_BACKUP

Upvotes: 2

Related Questions