自定义编译Python2.7+Bazel调试时遇_Py_RefTotal未定义符号问题
Let's break down your two distinct debugging problems and walk through actionable solutions for each.
Issue 1: Missing Source Files When Debugging via gdbserver + CLion
The root cause here is usually either incomplete debug symbols in your Bazel-built binary, or CLion failing to map Bazel's sandboxed source paths to your local filesystem. Here's how to fix it:
Generate full debug symbols: While
-c dbgenables debug mode, you can enforce more granular source-level debug info by adding these flags to your Bazel command:bazel run -c dbg --copt=-g3 --copt=-O0 --run_under="gdbserver localhost:9999" //your:target-g3includes all debug details, and-O0disables optimizations—critical for smooth, predictable debugging.Map Bazel sandbox paths to local sources: Bazel copies sources into a temporary sandbox during builds, which CLion might not recognize automatically. Fix this:
- Open CLion's
Settings > Build, Execution, Deployment > Debugger > Source Paths. - Click
+to add a new path mapping. - For the
Remote path, enter the sandbox path for your Python sources (check Bazel's build logs—this will look like/tmp/bazel-<random-hash>/external/python2_7). - For the
Local path, select your actual local Python 2.7 source directory.
- Open CLion's
Verify debug symbols exist: Run
file /path/to/your/bazel/binaryand confirm the output includes "with debug_info". If not, double-check your Bazel build flags and Python compilation setup.
Issue 2: undefined symbol: _Py_RefTotal Error When Debugging via CLion's Bazel Run Debug
This error occurs because _Py_RefTotal is a debug-only internal Python symbol—it only exists in Python builds compiled with debug mode enabled. Here's how to resolve it:
Recompile Python 2.7 with debug support: If you originally built Python without debug flags, rebuild it with:
./configure --enable-shared --with-pydebug --prefix=/path/to/your/custom/python/install make && make installThe
--with-pydebugflag enables debug-specific symbols like_Py_RefTotalthat your debug build is trying to reference.Confirm the symbol is exported: After recompiling, verify
_Py_RefTotalexists in your shared library:nm -D /path/to/your/custom/python/install/lib/libpython2.7.so | grep _Py_RefTotalYou should see output like
00000000001eXXXX D _Py_RefTotal(theDmeans it's a defined, exported symbol).Force Bazel to link your custom debug Python: Update your Bazel BUILD file to explicitly target your debug Python library and set the correct rpath:
cc_binary( name = "your_target", srcs = ["your_source.cc"], deps = [...], linkopts = [ "-L/path/to/your/custom/python/install/lib", "-lpython2.7", "-Wl,-rpath=/path/to/your/custom/python/install/lib", ], includes = ["/path/to/your/custom/python/install/include/python2.7"], )Fix CLion's debug environment variables: Sometimes CLion overrides
LD_LIBRARY_PATHin debug mode. Add your Python library path to your Run/Debug configuration:- Open your debug configuration in CLion.
- Under
Environment Variables, addLD_LIBRARY_PATH=/path/to/your/custom/python/install/lib:$LD_LIBRARY_PATH.
Final Checks
After applying these steps:
- Rebuild your Bazel target with the updated flags.
- Test both debugging workflows again to confirm the issues are resolved.
内容的提问来源于stack exchange,提问作者Eric Cousineau




