Building C/C++ Applications in a Conda Environment
Prerequisites
Common Build Variables
Directly Used by gcc/clang
These environment variables are automatically used by gcc/clang themselves.
CPATH- Colon-separated list of directories to search for headers before built-in include paths.
- Like passing multiple
-Ioptions.
LIBRARY_PATH- Colon-separated list of directories to search for libraries (
.so/.a) at link time. - Like passing multiple
-Loptions. - Doesn’t affect how the executable finds shared libraries when running.
- Colon-separated list of directories to search for libraries (
For Build Systems (make, cmake, etc.)
These environment variables are not used by compilers unless your build tool (make, cmake, etc.) or script expands them.
CC/CXX:- Which C/C++ compiler to use.
CFLAGS/CXXFLAGS:- Extra flags for compiling C or C++ respectively, e.g.,
-O2 -g -Wall.
- Extra flags for compiling C or C++ respectively, e.g.,
LDFLAGS:- Extra flags when linking, such as
-L/path/to/lib,-lfoo,-Wl,-rpath,/my/libs.
- Extra flags when linking, such as
🚫 Never stuff include/library/link flags inside
$CXXor$CC. Always set them as separate variables, or pass them directly on the command line to the compiler.
C++ Libraries in Conda Environments
When you install a C++ library from conda-forge, it’s placed inside your current environment, not in a system-wide directory.
- Headers:
$CONDA_PREFIX/include - Libraries:
$CONDA_PREFIX/lib
Exception: The C runtime (
libc.so,libm.so, etc.) always comes from the system, not Conda.
Create a Conda Environment for C++ Compilation
1 | |
libcxx,libcxxabi,libcxx-devel: LLVM’s C++ standard library and headers.
Tell the Compiler Where to Look for Headers and Libraries
1 | |
This automatically adds Conda’s include and lib directories to clang‘s search paths.
Set Common Build Variables for Build Systems
1 | |
LDFLAGSsets anrpathsuch that the generated executable or shared library will hard-codeLIBRARY_PATHas a shared library search path.
Compiling Open Source Applications
autossh
Install openssh:
1 | |
Then clone, compile, and install:
1 | |
busybox
Note that busybox does not use configure, only make.
1 | |
Edit .config: Change line CONFIG_TC=y to CONFIG_TC=n.
1 | |
Now, the busybox binary is built.
enscript
Install ghostscript:
1 | |
Clone, compile, and install:
1 | |
mlir
Install dependencies:
1 | |
Clone llvm-project:
1 | |
Build llvm-project:
1 | |
ocaml+dune
First compile ocaml:
1 | |
Do not install opam, as opam is hard-coded to use ~/.opam (even different binaries).
Install dune, which is not only a build system but also a dependency manager:
1 | |
proot
Install dependencies:
1 | |
Build and install talloc:
1 | |
Install uthash:
1 | |
Build and install proot:
1 | |
qemu
1 | |
Download qemu source code and enter directory:
1 | |
1 | |