Set include-path for header only library installed with conda
Asked Answered
D

2

8

I was recently advised to check out conda as a package manager. Unfortunately I don't succeed in finding how to make my compiler find a header-only library installed with conda? Ideally I would like to not having to manually specify a path to my compiler at all.

(The context is that I come from homebrew on macOS, which creates symbolic links on the right locations. Obviously this is what conda avoids. But still, a simple way to compile simple examples would be nice!)


Example

For example, if my code is the one below. Note: this question is meant to be generic, not related to a specific package, nor do I want to have to manually specify again my specific virtual environment.

#include <iostream>
#include <xtensor/xarray.hpp>
#include <xtensor/xio.hpp>

int main()
{
  xt::xarray<double> a
    {{1.0, 2.0, 3.0},
     {2.0, 5.0, 7.0},
     {2.0, 5.0, 7.0}};

  std::cout << a;
}

I have 'installed' the library using

conda create --name example
source activate example
conda install -c conda-forge xtensor-python

Now I would like to compile just with

clang++ -std=c++14 test.cpp

Note that I know that this works:

clang++ -std=c++14 -I~/miniconda3/envs/example/include test.cpp

But I don't think that this is wanted, because:

  • The path includes the environment (example).
  • It is system dependent.
Dynamiter answered 19/7, 2018 at 13:42 Comment(4)
If there is a system environment variable that sets include paths, you can set that when the environment is activatedErective
@Erective Thanks for your reply. I guess a big deficiency is that there is no such environment variable. I could, of course, introduce my own environment variable, say ${INCLUDEPATH}. If I were to do that, how can I set it based on the virtual environment?Dynamiter
https://mcmap.net/q/382101/-conda-set-ld_library_path-for-env-only-duplicateErective
@Erective Nice, thanks a lot.Dynamiter
D
3

At least on unix systems, a solution would be to use

clang++ -std=c++14 -I"${CONDA_PREFIX}"/include test.cpp

thereby "${CONDA_PREFIX}" point to the root of the current conda environment. In this case:

~/miniconda3/envs/example
Dynamiter answered 29/11, 2019 at 14:31 Comment(0)
C
2

I would rather export CPATH or CPLUS_INCLUDE_PATH variable to add ${CONDA_PREFIX}/include : it would untangle compilation process (compiling test.cpp) from conda environment allowing portability of your compilation process.

CPATH If this environment variable is present, it is treated as a delimited list of paths to be added to the default system include path list. The delimiter is the platform dependent delimiter, as used in the PATH environment variable.

C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH These environment variables specify additional paths, as for CPATH, which are only used when processing the appropriate language.

Use https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=env#setting-environment-variables to set environment variables.

$ conda env config vars set CPATH=${CONDA_PREFIX}/include:${CPATH}
Contented answered 27/8, 2021 at 9:0 Comment(2)
Please add further details to expand on your answer, such as working code or documentation citations.Sasha
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Conchitaconchobar

© 2022 - 2024 — McMap. All rights reserved.