views:

1423

answers:

6

Is there any way to debug a single file in Visual Studio.NET? I'm still a noob with C++, but I want to start learning how to get comfortable with the debugger, and as of right now I am writing really small files.

It seems if there is only one source file, it won't let me debug, but the moment I add another one, I can. I am using VS.net 2008.

+1  A: 

Do you have a main routine defined in this cpp you are trying to debug? Does the cpp you add that makes it work have a main routine?

You can always compile a cpp file, but the language defines the entry point of your application to be

int main()
{
}

(or the signature with the argv/argc command line args, but thats not important here).

Doug T.
A: 

Create a Visaul C++ console application in VS.net 2008, and add that cpp file you want to debug into project.

Call the function defined in that cpp file from the main routine, which will be created for you by VS.Net 2008 when you create the console project.

J.W.
A: 

I debug by:

  • putting a breakpoint where I want.
  • building the app inside Visual Studio.
  • pressing F5 to start the app using the debugger (if its a dll you're debugging, you'll want to set the exe that calls the dll in the project properties).
  • Profit! :)

If the breakpoint turns from the usual red dot to a yellow triangle, then it means you're not debugging the code you think you are - usually this happens when your dll is not loaded. If you're debugging an exe, then you won't see this.

Also, make sure you've built (and have the project type) as Debug.

gbjbaanb
+2  A: 

It doesn't want another source file, it wants a project file.

Visual Studio needs a bunch of information to know how to compile and debug your source code. Things like which optimization settings to use, where to look for the boost headers, that sort of thing.

Try this: go to File->New->Project... and pick a win32 console application. In the next wizard, go to Application Settings, and check "Empty Project", and hit OK.

Now you have empty project and solution files which you can copy/paste wherever you want them; to debug, just open the .sln file, drag in your single .cpp file, and hit F5.

Ben Straub
I was thinking the same exact idea, the least amount of setup each time. If I know it's something I won;t debug, I will just crack open a text editor and compile from a command line, but this will work for the little bit bigger programs. Thanks!
patricksweeney
A: 

You need to crate a solution to be able to debug. After that it doesn't matter how many source files you are having. You can create a solution using File->New menu.

Naveen
A: 

Whenever I want to debug a console program quickly, I find the console is the best place to start. So I take one C++ program (blah.cpp):

int main ()
{
    for (int i = 0; i < 100; i++)
        printf ("Hello World ... etc \n");
}

Then set up my environment on the console (from cmd.exe):

vcvars32

Then compile my program (The Zi is so that I get a blah.pdp to debug with):

cl /Zi blah.cpp

And voila I have a blah.exe that I can run. If I want to debug blah.exe I just open a project from VS2008 and select blah.exe instead of a project file. I can open up blah.cpp in the IDE and run (F5), set breakpoints (F9) and generally debug to my hearts content (F10,F11).

Cannonade