vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

[Linux] Cannot Pass Environment Variable To Debug Session

Open brandon654321 opened this issue 7 years ago • 5 comments

Describe the bug

  • OS and Version: Linux
  • VS Code Version: 1.31.0
  • C/C++ Extension Version: 0.22.0
  • Other extensions you installed (and if the issue persists after disabling them): None
  • A clear and concise description of what the bug is.

I am attempting to debug an application that has to link to some custom DLL's.

The application needs the proper path set in the LD_LIBRARY_PATH environment variable.

When I launch the application in debug mode, I receive the following output.

&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n" /home/myuser/git/myworkspace/bin/debug/Linux/3.10.0/x86_64/myprogram: error while loading shared libraries: mySharedLib.so: cannot open shared object file: No such file or directory

The problem is simple, I just need to get the debug session to have the proper environment variable setup prior to launching my application.

In my launch.json file, I have external console set to FALSE. When I echo the LD_LIBRARY_PATH variable from this terminal it has the correct path. If I launch my program from here (non debug mode) it works fine.

I have tried the following settings in launch.json...

"args": ["${env:LD_LIBRARY_PATH}"],

"environment": [ { "name": "LD_LIBRARY_PATH", "value": "/mypath/to/lib/" } ]

"setupCommands": 
[{ 
     "description": "Additional libs",
     "text": "set env LD_LIBRARY_PATH =/mypath/to/lib/",
}],

"additionalSOLibSearchPath" : "/mypath/to/lib/"

But I continue to receive the same error where it cannot find my libs. This seems like something trivial and should work. Anyone else with this issue?

brandon654321 avatar Feb 18 '19 19:02 brandon654321

environment in the launch.json will set the environment variable before it launches the program.

Calling getenv("LD_LIBRARY_PATH") with "environment": [ { "name": "LD_LIBRARY_PATH", "value": "/mypath/to/lib/" } ] set in the launch.json and it outputs /mypath/to/lib/.

To clarify, do you want your program's environment to have LD_LIBRARY_PATH set?

WardenGnaw avatar Feb 20 '19 01:02 WardenGnaw

Yes, I needed the shell which launched the program to have the proper LD_LIBRARY_PATH set. I was able to achieve this by using the setupCommands on a different machine. The setupCommands is just some commands which are run within GDB before it launches my program to be debugged. Still not sure how/why setting environment would not correctly setup the environment variable in the new shell (I think VSCode launches a new shell every time you start a debug session, regardless of external or internal).

Anyway I got tired of messing with the settings and just added the necessary initialization modules & paths I needed into my bash.rc so everytime VSCode launches a debug session in a new shell, the new shell by default has all the proper variables set.

brandon654321 avatar Feb 20 '19 13:02 brandon654321

https://github.com/microsoft/vscode-cpptools/issues/616

duzeev avatar Jun 13 '20 10:06 duzeev

https://github.com/microsoft/vscode-cpptools/issues/616

Also not solved there, just closed.

mbrunnen avatar Jul 28 '20 08:07 mbrunnen

I'm also having this issue now.

Ampaex avatar Mar 08 '23 10:03 Ampaex