Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in Visual Studio 2017 RC1 using CMake?

I am using Visual Studio 2017 RC1 to load a CMake Project (C++) using the new "Open Folder" feature and try to set environment variables for the binary upon launching.

Since there is no solution file anymore when using Open Folder, the pre-VS-2017 way of using the debugging dialogue doesn't seem to be an option here.

What I've tried so far:

  • Using a simple Hello World project with one single source file
  • Right Click on the corresponding C++ file in the solution explorer, selecting "Debug and Launch Settings"
  • Added env: {} there, which should give the Hello World program an empty environment

Screenshot of Solution Explorer and launch.vs.json

However, if I try to run the binary a pop-up window shows up which says "Object must implement IConvertible" and the binary won't run. It works fine when omitting env: {}, though. I've also tried to add something to env, but the effect stays the same. Changing the working directory using currentDir works fine, so I assume that launch.vs.json is at least read and used.

Since the documentation on this seems quite sparse, I am not sure if this is even the intended way for setting Environment Variables when using the VS 2017 CMake integration.

like image 602
jth Avatar asked Oct 29 '22 15:10

jth


1 Answers

In order to set multiple environment variables for VS 2017 with CMake, use \u0000 as separator. For example, the put these in your launch.vs.json will set 2 environment variables VAR1=USA and VAR2=JAPAN

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "name": "main.cpp",
      "project": "src\\main.cpp",
      "env": "VAR1=USA\u0000VAR2=JAPAN" 
    }
  ]
}
like image 198
Kien Truong Avatar answered Dec 22 '22 18:12

Kien Truong