Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For reference, I am a JavaScript developer learning C++. The browser is a pretty powerful debugger, and I can easily place a breakpoint in my code, hover over a variable or expression and get the value of that expression. Is this even possible in C++ or am I in a different world entirely?

I'm starting to write a bit of C++ code for an online course, and debugging with Code::Blocks gives me very opaque information. For example, I see stuff like this: 0x8049bc3 push ebp in the 'watches' window. Even if I write something like int foo = 3;, I have found no way of telling that foo is 3 while I'm stepping through my code. Am I missing something?

share|improve this question
up vote 10 down vote accepted

You need to build a debug version of your program:

Make sure that the project is compiled with the -g (debugging symbols) compiler option on, and the -s (strip symbols) option off. This ensures that the executable has debug symbols included. Compiler optimization switches should be turned off, stripping symbols (-s) must be turned off.

C++ compiles into machine code, where variables are just memory addresses. Symbols, such as variable names, can be appended to the executable by using appropriate compiler flags.

share|improve this answer
1  
+1 when dealing with a machine code compiled language, you need a debug version. Machine instructions are just that, debug symbols are metadata which a debugger can convert into human-friendly symbols. – Snowman May 7 '14 at 5:55
1  
There's also a huge difference in how comfortable and easy debugging is across IDEs. Try using Visual Studio once - everything is sadly a step down from there. – Ela782 May 10 '14 at 19:21

Depending on the operating system you use the recommendation will be different.

If you are using Windows: Look into visual studio there is a free edition. Make sure to compile and link the program it happens with the build button. Inside the editor you can place a break point when you compile with debugging in order to have the debugger stop at the break point and at that time in the command line you will be able to ask for what is inside the variable, in the immediate window of the debugger you can type the name of the variable and it will give you the value that is in there.

If you are using Linux: Here things become a little more advanced because you have to know how to compile your files. Since you say you know how to compile them I will start from there. You need to add compile flag -g for gdb debugger to be able to debug your program. after files have been linked into executable. Type gdb yourfile and debugging will start. breakpoint command with line number will tell the compiler where to set the break point and after this typing run will run the debugger and it will stop at the break point. Typing print with the variable name will give you the value of the variable just before the break point. Here is some documentation on how to set break point in gdb.

http://www.delorie.com/gnu/docs/gdb/gdb_29.html Hope this helps!

share|improve this answer
    
Why would you recommend free version of VS over Code::Blocks? I don't use either, just curious. – hyde Jun 14 at 9:53
    
I suggested it because it is one click install. For codeblocks on Windows as far as I know you need cygwin and g++. But I could be wrong please do correct me. I use visual Studio because I am working also with Microsoft SQL server and now azure so it is pretty good! Plus I do like intellisence! Just my preference of course! – Alex Top Jun 14 at 14:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.