r/cpp • u/nikoladsp • Apr 26 '26
Is there a C++ "venv" equivalent?
Python have venv, Rust have cargo, Node have nvm. You clone a repo, run one command, you're in a reproducible environment.
Is there a viable alternative for C++? I don't think standard will ever bother with this and to their defence, not sure if that is even possible.
I've tried Conan, vcpkg, various CMake setups. They're not bad tools, but there's no standard "activation" ritual. No isolated-per-project environment that pins compiler + deps + toolchain together. No single lockfile that means the same thing on my machine, my colleague's machine, and CI. What I keep wanting is something like: "cppenv activate" and suddenly I'm in a clean, isolated, reproducible build environment for that project. Exit it, and my system is untouched. Share a lockfile, and a teammate gets the exact same thing.
How are you handling reproducible build/development environments?
204
u/Drugbird Apr 26 '26
Docker
2
u/positivcheg Apr 26 '26
Ehm, did you use Conan right?
I’ve worked in a company that used Conan for their whole infrastructure of C++ libraries. We had one master repo of Conan profiles and one of the first step setting up any project is to run “conan config install <link>”.
It installs the profiles and then if ur on macOS you use macos_arch profile. If you don’t have exact version of C++ compiler (tied to Xcode versions on macOS) you won’t be able to configure a project. That gives that kind of stability and reproducibility - if some developer managed to build the project on macOS with that profile it means it’s gonna build on other machine using same profile given he passes “conan install …” stage.
On Linux it’s also about profiles since in profile you can specify compiler - gcc/clang + again exact version of the compiler. And here we go again, if some developer on his new Linux laptop tries to build the library it will only pass first Conan step if machine has that compiler version installed.
It’s definitely not as simple as Rust cargo because Rust comes with utility to install exact version of toolchain but it’s something pretty close to it. If you want to go nuts about it you can always declare your own Conan built tool, built the compiler, then use that built tool to built the library. But that’s too much I would say. At that stage I trust the distro providers that their builds of compilers are right.
57
u/FloweyTheFlower420 Apr 26 '26
nix flake
27
u/thalamity Apr 26 '26
Pixi/conda
18
u/bocsika Apr 26 '26
vcpkg + cmake gives you pretty much this.
You can pin down the exact MSVC or gcc toolset version, can utilize different vcpkg settings with different vcpkg base folders, which defined by an environment variable, so it is usable.
You can define a remote binary cache for vcpkg packages, so do not need to recompile everything when switching between projects, at most you can download different prebuilt packages to your local vcpkg cache folder.
43
u/HobbyQuestionThrow Apr 26 '26
This exists, but it's a PITA. It's shipping a development docker/podman/virtual machine image.
So you bypass the OS problem by having the dev environment include an entire operating system.
When you have it setup it's really nice, because it means easily reproducible builds even if you're on Linux Gentoo but your coworker is running Windows 11 and another coworker swears by Temple OS.
As long as their OS can run some kind of virtualization container, they can get the exact same build products generated at their desk that you and your CI generate.
12
u/OkFaithlessness3182 Apr 26 '26
Conan + conanbuildenv
26
u/missurunha Apr 26 '26
Have you tried bazel? You clone a repo and "bazel run" on your target. We use it since our project has multiple repositories and its mixed language, so everything runs under bazel. Theres no manual venving or npming.
9
u/zhuoqiang Apr 26 '26
pixi, you could have isolated toolchain (compiler, etc) as well as 3rd party libs in per project dir, which by default in PROJECT_ROOT/.pixi/, quite similar to python uv
3
u/EC36339 Apr 26 '26
Yes. It's called a file system and include path options.
7
u/LoadVisual Apr 26 '26
I usually just use a conanfile and CMakePreset.json so that only cloning the project is required and windows, mac, Linux and FreeBSD just work / build projects okay with settings for each envs compiler.
7
u/MrWhite26 Apr 26 '26
Not a single tool, but with a CMakePreset.json, vcpkg and optionally a dockerfile, you can set things up that all one has to do in VScode is 'open folder'.
So definitely more clunky to setup, but while developing it works well enough.
14
u/Mason-B Apr 26 '26 edited Apr 26 '26
There basically isnt because C++ is a native toolchain language. Interpreter based languages can have isolated environments and be standalone because they have VMs they run or emulate. But C++ works with the system, the binaries it generates are native components, and the tools it uses to generate them are part of your local machine.
Most languages use native executables to construct a sort of abstraction layer on top of which their ecosystem lives... but C++ is (one of) the languages you use to generate those executables, and its ecosystem is deeply embedded into each system.
This is a Google scale problem. You would need to use something like Bazel. And then build your tool chain as part of the build (I dunno if youve tried to build standalone gcc, it takes hours), and then use that tool chain to build the stable toolchain. And then use that for the build. And after all that you aren't really totally isolated. But its the closest I've ever gotten to a checkout, run one command, it just builds correctly and exactly like everyone else at the company (but it requires a team of dedicated build engineers to maintain, hence why its a 1000 C++ engineers google scale problem).
An easier answer is a VM image with specific instructions to create (e.g. so you can refresh it every few months), and then share the actual image for, that you run the official build on (and maybe also development).
3
u/wasabichicken Apr 26 '26
My employer uses a collection of shell scripts for this. Prior to running cmake, I run
setenv_<target>(for examplesetenv_x86), and this selects the toolchain, system libraries, sets a bunch of platform-specific defines, etc.Then the cmake layer takes over, and based on whats in the environment, cmake configures 3rd party libraries, what source files get included, which targets to build etc.
It's quite ad-hoc, but overall I'd say it works fairly well.
3
u/most_common_person Apr 26 '26
Clone vcpkg in your C++ project folder, and a vcpkg.json file that includes your dependencies… in your CMakeLists.txt add the vcpkg.cmake of the vcpkg you just cloned as your VCPKG_TOOLCHAIN file… and now on everything is exactly like venv. Of course, any lib you add should be added as usual to your target link libraries.
5
u/EdwinYZW Apr 26 '26
Why do you need an activation ritual if you have an option to go without it? It is huge pain for me when I must activate some env before running any python script. With C++, I just hit ninja and that's all.
As for the reproductivity and isolation, there is no other tool better than container. Conda or pipenv isn't really isolation IMO compared to the container.
5
u/jjjare Apr 26 '26
Conan
2
u/macson_g Apr 26 '26
Yeah, it's called Docker.
2
u/anti-que Apr 26 '26
spack is used heavily in HPC and they have “environments” which does what you are looking for.
2
u/Informal-Flan-5694 Apr 26 '26
lmod or Environment module
2
u/not_a_novel_account cmake dev Apr 26 '26
A venv is mostly just controlling
PATH. It's the same for C++.2
u/tgamblin Apr 26 '26
Try Spack environments:
* https://spack.readthedocs.io/en/latest/environments_basics.html
* https://spack-tutorial.readthedocs.io/en/latest/tutorial_environments.html
YAML manifest, JSON lockfile, reproducible, also allows you to develop parts of the stack and/or set up CI:
* https://spack-tutorial.readthedocs.io/en/latest/tutorial_developer_workflows.html
There is a GitHub action you can use to set up a simple environment with a buildcache:
* https://github.com/spack/setup-spack
2
u/Conscious-Secret-775 Apr 26 '26
CMake with vcpkg and CMake presets gets you close. Doesn't pin the compiler, you would need a docker build container for that.
2
u/alexeiz Apr 27 '26
Conan has a lock file. Did you know, you can install and then version-lock cmake and gcc with conan? You can use that for completely reproducible environments. It doesn't have to be "standard". It just has to work on platforms that you're targeting.
4
u/Entire-Hornet2574 Apr 26 '26
Yep, Conan. https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html
5
u/Orlha Apr 26 '26
It’s a solution to a problem that shouldn’t exist
3
u/zilled Apr 26 '26
conda is bit of a stretch because it is designed for python, but it actually does work quite well for C++ too.
Al lot of compiler's version, many libs with their pkgconfig.
2
u/Copronymus09 Apr 26 '26
Python is a scripting language, the concept of venv doesn't exist in C++. OS is the environment.
1
u/shedgehog-orchard Apr 26 '26
Like other commenters said it’s a pain in the ass but we use podman containers out the wazoo
1
u/Horrih Apr 26 '26
Docker, or otherwise provide a simple makefile whose only tasks is to call conan/cmake with the appropriate options like compiler /c++ standard version. Dependency versions can be locked with Conan.
We use this at work it's kinda nice but since we are all on the same OS it sure is easier for us than for some cross-platform open source project where docker is your only safe bet
1
u/Ikkepop Apr 26 '26 edited Apr 26 '26
docker / podman ? no real equivalent if you want to run gui apps though.
But theres really no reason you can't do a venv like system for c++ other "you havent done it yet" You can throw together a completely self contained project if you want, I'v done it before, its just a pain in the b-hind to set it up.
If you bother to master conan2 it can get you most of the way there.
1
u/osmin_og Apr 26 '26
Currently we use conda
1
u/killbot5000 Apr 26 '26
Yocto
1
u/rdtsc Apr 26 '26
For MSBuild the compiler version is explicitly specified in the build files. Just check out the repo and open the project in Visual Studio, done. For CMake just a simple wrapper which puts the correct compiler version into the PATH. No need to complicate it more.
1
u/smdowney WG21, Text/Unicode SG, optional<T&> Apr 26 '26
There's also venv. Smile.
But outside of the compiler itself, a ton of c++ tooling is available through PyPI and I've started managing a lot of it through pyproject.toml and UV.
1
u/jayylien Apr 26 '26
External build if you mean dev requirements, not including the tool chain, but simply dependencies. Similar also, vscode deps, but prefer external build. A LOT more control.
If you're looking for more environment control: Docker is tried and true. Podman seems to be aiming for a similar thing to Docker. Nix I've heard good things about, but never used. I knew a coworker who was fond of Spack, but I never personally used it.
1
u/wannaliveonmars Apr 26 '26
I think the best way is a docker environment for build, since that is language-agnostic, and provides sandboxing as well (for security).
1
u/CocktailPerson Apr 26 '26
Nix flakes and bazel.
Docker is a miserable development experience in my experience, but maybe that's more about how my company was doing it.
1
u/Infamous-Bed-7535 Apr 26 '26
Double stage cmake + docker gives a very good setup. Double stage = first stage build 3rd party deps from scratch, second stage builds your application.
First stage is used to build your dev environment.
Building deps from source can be a good plus for some libraries to have full control, or when you need to apply flags breaking ABI. You set it one place and can propagate through built dependencies..
Complex, but clean solution, not advised for small projects. For those just install the pre-buolt package and use it.
1
u/uahw Apr 27 '26
I use nix
1
u/hadi-rahimi Apr 27 '26
vs code devcontainer is also an option
1
u/juliebeezkneez Apr 27 '26
Spack envs
1
u/jones77 Apr 27 '26
This isn't the correct answer but if you build your stuff with "Odin" you're gonna have way less issues than "CMake".
1
u/VisualConnection3277 Apr 28 '26
use pixi and vcpkg
1
u/Dragdu Apr 28 '26
You can do this with bazel_env, but pretty much every place I know that uses Bazel has its own internal dev support team.
1
u/Classic_Knowledge_46 Apr 28 '26
Yep, the closest thing is called build2.
1
u/Adept_Situation2205 May 03 '26
pixi has environment, if your cpp dependencies are all from conda-forge then pixi is the most like venv.
vcpkg mainfest mode installs all dependencies into build folder, so each build forder is venv
1
u/pedersenk Apr 26 '26
Because C or C++ don't need countless bindings to access system libraries, they can just use the system package manager, optionally in something like a chroot/jail.
So really venv et al are really just workarounds to limitations of those languages.
Have a look through the lockfile to get a good idea of the problem and why C and C++ aren't quite as affected.
1
u/Minimonium Apr 26 '26
Bazel is probably the closest thing out there
1
u/abrahamlitecoin Apr 26 '26
chroot
1
u/rand3289 Apr 26 '26
I hate cargo. Last OSV took me 4 hours to figure out.
1
u/iamasatellite Apr 26 '26
\src\
\lib\
\include\
0
u/tiredoowwll Apr 26 '26
Велком ту хелл брудда
0
u/tizio_1234 Apr 27 '26
no
-1
u/Gullible_Carry1049 Apr 26 '26
https://mise.jdx.dev
-1
u/lonkamikaze Apr 26 '26
Frankly it's time compiler vendors agreed on shipping an intermediate language that is JIT compiled on the host OS.
Compilers already produce ILs, it's just that every compiler has it's own.
Technically that wouldn't be too problematic, assumptions would have to be made about required OS libraries being available.
It's of course not viable for everything e.g. embedded/realtime stuff. But even there it would be useful, because you could produce library code in the IL and reuse the IL library for multiple targets and platforms - reducing build times for native binaries in organisations that target many different devices.