Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source environment variables before running CMake in VS Code

I am trying to build a large C++ framework in VS Code using CMake Tools and C/C++ Extensions in Visual Studio Code. I am browsing stackoverflow/github issues/any google suggestion and it looks like I am not the first person to encounter this, but I can not figure out for the life of me what am I doing wrong.

Here is the problem. I want to setup VS Code in a way to be able to build the framework (it is C++) right from VS Code using the built-in tools/extensions. Here is the process I was using up until now (in standard terminal in Linux) and it also works in terminal run in VS Code:

cd /path-to-project-main-folder
source scripts/env.sh
cmake .
make -j 10

Now the problem is that when I set up VS Code, open the folder where the framework is, VS Code recognizes it is cmake project and gives me the opportunity to build it. Problem is that when I try to build it, it does not set up the environment first and therefore uses wrong cmake (not the sourced one but the default one build in server) and also wrong libraries and some of them are not even recognized. The problem is in the first line:

source scripts/env.sh

where the environment variables are set and also PATHs to some libraries and programs. This line is not ran by VS Code before cmake and build. Does anyone know a solution on how to configure CMake Tools extension to run:

source scripts/env.sh

line before running cmake and then make?

I was looking into some solutions using tasks.json, settings.json files or creating my own kit. But no solution worked for me or I did not completely undestand the solution:

  • https://github.com/microsoft/vscode-cmake-tools/blob/HEAD/docs/tasks.md

  • https://github.com/microsoft/vscode-cmake-tools/issues/2243

  • https://github.com/microsoft/vscode-cmake-tools/pull/995

  • VSCode: Set environment variables via script and man pages of CMake Tools, VS Code,...

  • VScode remote development: How can I run a build task to source environment variables before running the real build task? ---> but I use cmake

  • VSCode, how to source environment variable files like setup.bash?

like image 240
Arual Avatar asked Jul 01 '26 11:07

Arual


2 Answers

It kind of depends on how complicated the logic of your environment setup script is. If there's no control flow and it just sets some environment variables, that's pretty easy to handle idiomatically:

In such a simple case, I'd suggest to try using CMake Presets. configure presets can have an environment property where you define environment variables. For library paths, it depends how exactly you're using those library paths. If it's for find_package calls, I think you might be able to use the cacheVariables configure preset property and set CMAKE_PREFIX_PATH. Or perhaps you could set the link flags directly using CMAKE_CXX_FLAGS.

Another mechanism you could take a look at is CMake Tools' settings- of which cmake.environment, cmake.configureEnvironment, cmake.buildEnvironment, and cmake.testEnvironment are probably of interest.

If you're using CMake Kits (a CMake-Tools-only feature), then you can use the environmentSetupScript property of a kit definition. See also https://github.com/microsoft/vscode-cmake-tools/issues/1885#issuecomment-848239346 and https://gitlab.kitware.com/cmake/cmake/-/issues/21619.

If those don't work for you or aren't suited to your use-case, then I think you'll just have to try launching VS Code from an environment that has sourced that environment setup script so it can inherit those environment variables.

like image 163
starball Avatar answered Jul 04 '26 02:07

starball


The proper CMake way to handle this kind of thing is twofold:

  1. any build target specific setup should go into toolchain files. See e.g. the Android SDK setup example in the relevant documentation
  2. any "external" dependency setup (library directories, tools that can be called) should be set up with a provided .cmake or .config script, in the best case also generated by CMake when these externals are built. Things like a proper conan build setup or similar can help setting up, maintaining, and distributing these kinds of things automatically.

Pointing CMake to find these configuration files is up to the caller of CMake. You could of course have a simple script that fills in the relevant paths to the call to CMake.

Calling a shell script from CMake to setup an environment is an awkward hack.

like image 41
rubenvb Avatar answered Jul 04 '26 01:07

rubenvb