Rainer
Rainer

Reputation: 193

How do I debug existing C programs with Visual Studio 2010 Professional?

how can I use the Visual Studio Debugger on existing C programs I found in a textbook? I want to debug these little examples one by one, but without the overhead of creating a full project for each example.

Example: Let's say I compile "helloworld.c" from the Visual Studio command prompt ("cl.exe helloworld.c"). This gives me helloworld.obj and helloworld.exe. I would like to know if there is a way to use the VS debugger on "helloworld.exe". So far, I have only worked with the debugger on full-blown projects; I have no idea how to debug small "stand-alone" test programs without the Visual Studio project overhead. (I hope this is not a dumb question, as the VS Debugger might only be available for the full project.)

Thank you for any ideas.

Upvotes: 4

Views: 11193

Answers (6)

Clifford
Clifford

Reputation: 93446

The "overhead" is hardly more effort that it took to post this question!

Simply start with the "Empty Project" template and add the single source file you wish to debug by right-clicking on the project browser sources folder. It takes mere seconds.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941218

It is not impossible, but you'll have to learn a lot more about how to exactly build a program from the command line to get the best debugging experience. There are a bunch of options that are real time savers and greatly improve the odds that you'll discover bugs.

Which is a little beside the point right now, learn C instead of spending time learning a boring tool with way too many options. Creating a new project takes 5 seconds after a wee bit of practice. Use the Win32 Console Application project template.

One small setting you have to change if you want to compile as C instead of C++. Right-click the project, Properties, C/C++, Advanced, Compile As = Compile as C Code.

Upvotes: 1

Daniel Rose
Daniel Rose

Reputation: 17638

The debugger is part of Visual Studio, so you'll have to start it up anyway. However, for the debugger to work properly with your source code, you need to also generate a PDB file (see here). You get this by including /Zi or /ZI as argument to cl.exe (see here).

Upvotes: 1

bobbymcr
bobbymcr

Reputation: 24167

If you're set on not using projects then you can do this. It just gets more difficult as the number of code files increases.

First, to do any meaningful source-level debugging, you will need to generate debug symbols. Add the "/Zi" switch to your cl.exe command line:

cl.exe /Zi helloworld.c

This will generate helloworld.exe, helloworld.obj, and helloworld.pdb (these are the debugging symbols).

Next you will open Visual Studio 2010. Use File -> Open -> File... and select helloworld.exe. It will generate a wrapping solution with one project for your EXE file.

In Solution Explorer, right click on the EXE file and select Debug -> Step Into new instance.

It should pop up a source window and show you the first line of your program. Now, debug away!

Upvotes: 1

pmg
pmg

Reputation: 108968

What I do when I have to use VS is to reuse the plain.c project I created a long time ago.

Just paste new code there and go. Throw the code in another file if you want to keep it.

Upvotes: 1

Philipp
Philipp

Reputation: 11814

Why don't you create one project for testing the sample codes? You can create a single .c-file for all the samples. This would look something like

void sampleA()
{
 //hello world
}

void sampleB()
{
 //hello everybody else
}

void main(int argc, char** argv)
{
//  sampleA(); 
  sampleB();
}

Upvotes: 3

Related Questions