You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

自定义编译Python2.7+Bazel调试时遇_Py_RefTotal未定义符号问题

Troubleshooting Your Bazel + Python 2.7 Debugging Issues on Ubuntu 16.04

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 dbg enables 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
    

    -g3 includes all debug details, and -O0 disables 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:

    1. Open CLion's Settings > Build, Execution, Deployment > Debugger > Source Paths.
    2. Click + to add a new path mapping.
    3. 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).
    4. For the Local path, select your actual local Python 2.7 source directory.
  • Verify debug symbols exist: Run file /path/to/your/bazel/binary and 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 install
    

    The --with-pydebug flag enables debug-specific symbols like _Py_RefTotal that your debug build is trying to reference.

  • Confirm the symbol is exported: After recompiling, verify _Py_RefTotal exists in your shared library:

    nm -D /path/to/your/custom/python/install/lib/libpython2.7.so | grep _Py_RefTotal
    

    You should see output like 00000000001eXXXX D _Py_RefTotal (the D means 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_PATH in debug mode. Add your Python library path to your Run/Debug configuration:

    1. Open your debug configuration in CLion.
    2. Under Environment Variables, add LD_LIBRARY_PATH=/path/to/your/custom/python/install/lib:$LD_LIBRARY_PATH.

Final Checks

After applying these steps:

  1. Rebuild your Bazel target with the updated flags.
  2. Test both debugging workflows again to confirm the issues are resolved.

内容的提问来源于stack exchange,提问作者Eric Cousineau

火山引擎 最新活动