3

When I debug c++ (in clion), I can't watch global strings. I tried to check other types but it worked well.

Also, I tried local string and I can watch it too?!

this is a screenshot of the program

Meraj al Maksud
  • 1,402
  • 2
  • 20
  • 32
mohamed ahmed
  • 51
  • 1
  • 1
  • 3
  • 5
    Don't post links to images of text. Copy-paste the actual text, as text, into the question body instead. Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 25 '17 at 15:04
  • As for your problem, what version of Clion are you using? I remember there being some problems with `std::string` in the debugger view in early versions, but that's been fixed for quite some time now. – Some programmer dude Jan 25 '17 at 15:06
  • non const `char*` to string literals are deprecated ( to stop you trying to modify the string literal ), try `const char* f ="RBYG";` – George Jan 25 '17 at 15:07
  • Lastly, you definitely need to read [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Jan 25 '17 at 15:09
  • i use version: 2016.3.2 i think it is the latest one. – mohamed ahmed Jan 25 '17 at 15:11
  • i can watch char*f the problem is with y i can;t watch it – mohamed ahmed Jan 25 '17 at 15:12
  • I can replicate it. It must be a bug, and as such I recommend you go to [the Clion bug-tracker](https://youtrack.jetbrains.com/issues/CPP), search for it (to see if it have already been reported), and if not then add it. If it has been reported then +1 the existing issue. – Some programmer dude Jan 25 '17 at 15:15
  • Also please forgive the first part of my first comment, because the text (the code) is not really relevant in this case. The image is fine in this case. Though remember it for future questions. :) – Some programmer dude Jan 25 '17 at 15:17
  • no problem i will report for the bug and thanks for your help – mohamed ahmed Jan 25 '17 at 15:20

1 Answers1

6

Just in case, the issue is now tracked as CPP-8693.

The root cause is somehow related to libstdc++ dual ABI. Global symbols of std::string types are mangled differently, which in turn confuses GDB.

In the GCC 5.1 release libstdc++ introduced a new library ABI that includes new implementations of std::string and std::list. These changes were necessary to conform to the 2011 C++ standard which forbids Copy-On-Write strings and requires lists to keep track of their size.

Given the following code:

std::string global_var = "Hi there!";
static std::string static_var = "Hello";

Here's the related nm output:

0000000000602240 B _Z10global_varB5cxx11
0000000000602280 b _ZL10static_var

A possible workaround is to disable C++11 ABI. In CMakeLists.txt, add the following line, right after the set(CMAKE_CXX_STANDARD 11) line, if any:

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

This makes symbol names to be mangled differently, in a way GDB is happy again:

0000000000602238 B global_var
0000000000602248 b _ZL10static_var
Eldar Abusalimov
  • 21,255
  • 4
  • 60
  • 66
  • 1
    Hey Eldar! Thanks for the answer. Is there any other way I could get around the same problem without using -D_GLIBCXX_USE_CXX11_ABI=0. As I use one of the libraries compiled using -D_GLIBCXX_USE_CXX11_ABI=1, my compiled program crashes before the start giving segmentation fault. Thanks in advance! – krishna chaitanya Aug 17 '19 at 17:20
  • @krishnachaitanya not that I am aware of, unfortunately. – Eldar Abusalimov Aug 19 '19 at 12:36
  • this advice doen't help, because i get link errors with opencv using this flag – Stepan Yakovenko May 05 '20 at 20:13