4

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:

starball
  • 20,030
  • 7
  • 43
  • 238
Arual
  • 41
  • 2
  • It's a big red flag if you need to set up an environment with a shell script. This usually happens because of some hacks or quick hot fixes and in big companies it stays like that, because no one is brave enough to make it properly work. That being said, have you tried to just to source && run code? I.e. `source scripts/env.sh && code` or `source scripts/env.sh; code`. I would assume that VSCode afterwards if it opens another shell it would open it with the same environment variables. – Milan Š. Jan 13 '23 at 17:36
  • The shell script is used mostly because the binaries of libraries are compiled in one place and they are distributed via network. I can not compile those binaries myself, or for different project different version of those libraries are required (backwards compatibility issues and stuff like that).. I tried to source and open vscode afterwards or source is it terminal in vscode and run it, but neither option worked, as CMakeTools opens new default shell. But saying that i am going to try to source script/env.sh in ~/.bashrc, that might work – Arual Jan 14 '23 at 11:06
  • That is one possibility. The other possibility would be to try and use `execute_process()` or `add_custom_command()` for a pre-build step. – Milan Š. Jan 14 '23 at 11:33
  • So it looks like adding source scripts/env.sh to ~/.bashrc does work, but it has multiple other problems (such that for every project I would need to change that line individually).. Thanks for the tip, I am going to take a look at that – Arual Jan 14 '23 at 12:53
  • `~./bashrc` is executed every time you open up a shell. Hence the reason why it works. The `add_custom_command()` is most likely what you want for a `prebuild` step – Milan Š. Jan 14 '23 at 13:13

1 Answers1

0

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 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.

starball
  • 20,030
  • 7
  • 43
  • 238