Skip to content

Instantly share code, notes, and snippets.

@nickolayl
Forked from jpz/conda_setup.md
Created February 15, 2024 15:56
Show Gist options
  • Save nickolayl/09e8e50be14e03e389af2aa5b6a1fa86 to your computer and use it in GitHub Desktop.
Save nickolayl/09e8e50be14e03e389af2aa5b6a1fa86 to your computer and use it in GitHub Desktop.
Using Conda to Create C++ Environments

Using Conda to Create C++ Environments

Conda is a package manager, which arose from the Data Science and Python community to manage complicated binary and source deployments.

It can be leveraged for non-Python environments also.

From our experience of COMP6771 2020, we required an install of clang, along with C++ libraries.

So, the steps to install all of this (excepting ranges) is:

  1. Install conda from here or use a local installer such as winget (Windows 10), brew (MacOS) or apt (Debian).

  2. Create a conda environment and install clang and the C++ libraries above.

conda create -n COMP6771
conda activate COMP6771
conda install -y -c conda-forge/label/llvm_rc clangdev
conda install -y -c conda-forge abseil-cpp gsl-lite fmt catch2
  1. Test that clang v11.0.0 is available with clang --version

  2. Test that the libraries are available with this short C++ program attached, test.cpp, by running the build script build.ps1 on Windows, or test_install.sh on MacOS/Linux.

Please note that to run clang, you need to be in conda, and you need to activate the environment with conda activate <environmentname> for everything to be set up.

Understanding what we have just done.

To understand a C++ build environment, it is nearly identical to a C build environment. It is made up of include files which are in the include directories, and the library files which are in the lib directories.

Conda here has installed the include and lib files in $env:CONDA_PREFIX\Library\include and $env:CONDA_PREFIX\Library\lib on Windows, and on MacOS and Linux, these paths will be $CONDA_PREFIX/Library/include and CONDA_PREFIX/Library/lib

So, to read the build script below, we just use clang to compile our file, but we set the include path to make sure the compiler knows where to find the include files with the -I flag. Furthermore, gsl-lite requires a certain environment variable to be set for compilation to work, so we use the -D flag.

#include <iostream>
#include <absl/container/flat_hash_set.h>
#include <catch2/catch.hpp>
#include <gsl-lite/gsl-lite.hpp>
#include <fmt/format.h>
int main() {
std::cout << "it works\n";
}
clang -Dgsl_CONFIG_DEFAULTS_VERSION=1 -I "$env:CONDA_PREFIX\Library\include" test.cpp -o test_install.exe
.\test_install.exe
clang -Dgsl_CONFIG_DEFAULTS_VERSION=1 -I "$CONDA_PREFIX/Library/include test.cpp" -o test_install
./test_install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment