0

I have code that embeds Python. That embedded Python uses NumPy and hence, I need to explicitly load libpython, to make NumPy work.

The driving code is in C++ (tests in Google Test). There is a bug somewhere, and I try to use gdb for debugging. However, something strange happens as embedded versions are different, when I simply run the executable and when I run the executable under gdb.

I find the path to libpython by instantiating sysconfig module under the initialized embedded Python and then using sysconfig.get_config_var("LIBDIR").

I log the found path to the libpython hen I simply run the executable:

Path to libpython is /home/dima/.conda/envs/um02-open-interfaces/lib

When I run the same executable under gdb:

Path to libpython is /home/linuxbrew/.linuxbrew/opt/[email protected]/lib

How to stop gdb from changing environment?

1 Answer 1

1

How to stop gdb from changing environment?

GDB doesn't change the environment on its own. However, it does invoke your $SHELL, and if you have ~/.bashrc or similar file changing the environment, than these changes will affect any process invoked by the GDB run command.

You can confirm whether that is happening by running $SHELL -c /path/to/your/program. If it prints the wrong path to libpython, this is likely the root cause.

The fix is to make .bashrc change the environment only for interactive shells.

You could also disable the intermediate $SHELL using GDB set startup-with-shell off (documentation).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Yes, the problem was with my ~/.zshenv file that was prepending to PATH the path to python3 from Linuxbrew. Apparently, this file is read on each shell invocation, so it can potentially prepend the same stuff to PATH multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.