Skip to content

Instantly share code, notes, and snippets.

@jpz
Last active April 6, 2024 22:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpz/1c33c466b9af1287abfa51497ab1c0a4 to your computer and use it in GitHub Desktop.
Save jpz/1c33c466b9af1287abfa51497ab1c0a4 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
@erwin-lebow
Copy link

iostream.h not found...

@jpz
Copy link
Author

jpz commented Apr 1, 2024

iostream.h not found...

Have you included <iostream.h> by accident instead of ? I believe the .h is deprecated - https://stackoverflow.com/questions/2976477/difference-between-iostream-and-iostream-h

@erwin-lebow
Copy link

The complete error message is:
clang: warning: unable to find a Visual Studio installation; try running Clang from a developer command prompt [-Wmsvc-not-found]
test.cpp:1:10: fatal error: 'iostream' file not found

Running the example code from the conda workspace does not work, iostream.h was a typo, it is iostream.

The compiler does not find iostream, because it is not in the path. There is a iostream.hpp, but is that the right one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment