C/C++ Creating a virtual environment for C programs


New Member

Messages: 1

I am having fun learning the C language, however, coming from Python, there is a feature that I really like. And that's virtualenvs which isolates packages you use in your program from your main system. This is really helpful for me. As an example, last day I needed a package that would help me play .wav files in my system, so I made a virtualenv and messed around with around 4-5 packages (some didn't work) and I found out that I can just use the built-in aplay program. So I didn't bothered using any packages anymore, but I didn't need to remove them from my main system, since they were contained only in the virtual environment. I was wondering if there is this same functionality in C programs. I have read about chroot but I don't know if that is the right answer.

TLDR: are there virtualenv for C programs?


Son of Beastie

Reaction score: 1,588
Messages: 2,718

As C doesn't specify any package management, the packaging of C libraries is left to the operating system (which is IMHO a good thing). Therefore, you'd also need a virtual environment offered by your operating system. For FreeBSD, the first thing that comes to mind is a jail(8).


Son of Beastie

Reaction score: 2,097
Messages: 2,956

You can also do the same thing as you would a cross-compiler. Make a single folder and just extract the .a, .so and .h files from the packages into directories within it. That is completely self contained (even more so than virtualenv).

Then when you use the compiler, just point it towards those directories with -I, and -L. You can do the same with CMake too and if long term, consider setting these as environment variables in your profile.

You could even point the compiler / cmake towards the libraries installed within a chroot or jail.