Valgrind堆摘要显示???的原因及CMake编译配置修改方法咨询
???) via CMakeLists.txt Modifications It looks like you're hitting a common Valgrind issue where call stack entries show up as ??? because your program or dependent libraries lack debug symbols, or compiler optimizations are messing with stack tracing. Let's walk through how to adjust your CMake configuration to fix this for your code, plus handle system library gaps:
Step 1: Enable Debug Symbols in Your Build
Valgrind needs debug symbols to map memory addresses back to human-readable function names. Add these lines to your CMakeLists.txt to ensure symbols are generated:
# Generate full debug symbols and disable optimizations for Debug builds set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0") # Optional: Keep debug symbols in Release builds (useful if you need to debug optimized code) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g")
When rebuilding, make sure to specify the Debug build type to get unoptimized code with full symbols:
cmake -DCMAKE_BUILD_TYPE=Debug .. make clean && make
Step 2: Disable Frame Pointer Optimization
Modern compilers often omit frame pointers to boost performance, but this breaks Valgrind's ability to trace call stacks reliably. Add these lines to preserve frame pointers:
# Prevent compiler from omitting frame pointers, critical for accurate stack tracing set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer")
Step 3: Fixing System Library ??? Entries
In your Valgrind output, the ??? entries come from system libraries like libgobject-2.0.so and libglib-2.0.so—these don't ship with debug symbols by default. On Debian/Ubuntu-based systems, install their debug packages with:
sudo apt-get install libglib2.0-dev-dbg libgobject-2.0-dev-dbg
This gives Valgrind the symbols it needs to resolve those system library function calls.
Why This Works
- The
-gflag tells the compiler to generate debug symbols, which Valgrind uses to translate memory addresses into function names and line numbers. -O0disables compiler optimizations (for Debug builds), which can rearrange code and stack frames in ways that make call stacks unreadable.-fno-omit-frame-pointerensures the compiler keeps frame pointers in registers, which Valgrind relies on to walk the call stack correctly.
After making these changes, recompile your program and run your Valgrind command again—you should see full, resolved call stacks instead of ???.
内容的提问来源于stack exchange,提问作者WHU lajislam

