Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to override CXX enviroment variables set by

Context

For whatever reason, the clang compilers used within my conda environments fail to compile standard .cpp files. However my OS's base g++ compiler can compile things. This walkthrough is intended to set environment variables within an conda environment NOT to use conda's suggestions, but instead to use the base compilers.

I tried this on a mac with OS: BigSur and an intel processor (there are reports that this also works on M1 macs). I'm using zsh as my terminal shell, but this should work with bash also. At the time of testing this the most recent version of Brian2 is 2.4.2, but I also tested a dev release 2.4.2.post0.dev149.

I'll use briandev_env as a standin for whatever conda environment name you have set up. These instructions assume you have gcc and g++ installed (see "Other commands") and that these are what you want to use even inside a conda environment.

activate conda environment, switch to env startup script directory

borrows from these instructions

conda activate briandev_env
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d

for me, these directories ^ already existed

Create env_vars.sh scripts to be run on activation and deactivation of an environment

use a text editor to create/add the following to $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

(NOTE: in some quick tests, it appears that setting CC and CXX are sufficient to get tests to pass, so you could consider commenting out the export CLANG and export CLANGXX lines)

#!/bin/sh

#backup conda's previous values for these variables
export OLD_CC=$CC
export OLD_CXX=$CXX
export OLD_CLANG=$CLANG
export OLD_CLANGXX=$CLANGXX

#use the base system's gcc and g++ compilers which may be more up-to-date than conda's
export CC='gcc'
export CXX='g++'
export CLANG='gcc'
export CLANGXX='g++'
echo "overriding CC,CLANG with gcc, and CLANGXX,CXX with g++"

this should get called every time this particular conda environment is activated

use a text editor to create/add the following to $CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh

#!/bin/sh

unset OLD_CC OLD_CXX OLD_CLANG OLD_CLANGXX
unset CC CXX CLANG CLANGXX

which will revert these variables when you leave that conda environment.

run the following to reset the conda environment:

conda deactivate
conda activate briandev_env

then test brian and cython

Minimal test for cython in brian

import brian2
brian2.test('cython', test_codegen_independent=False, additional_args=['-x'])

Other commands which may help debugging

conda env list: lists all conda environments

conda list | grep clang: list all packages conda sees named clang

export | grep clang: see all remaining environment variables which contain references to clang (likely set by conda)

check which version of several C/C++ compilers you have:

g++ --version 
clang++ --version
$CXX --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment