如何在Visual Studio中调试单个.cpp文件?

时间:2021-08-07 20:45:30

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.

有没有办法在Visual Studio.NET中调试单个文件?我仍然是C ++的菜鸟,但我想开始学习如何使用调试器,现在我正在编写非常小的文件。

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.

似乎只有一个源文件,它不会让我调试,但是当我添加另一个时,我可以。我正在使用VS.net 2008。

7 个解决方案

#1


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.

Visual Studio需要大量信息才能知道如何编译和调试源代码。像使用哪种优化设置,在哪里寻找增强头,这类事情。

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.

试试这个:转到File-> New-> Project ...并选择一个win32控制台应用程序。在下一个向导中,转到“应用程序设置”,选中“清空项目”,然后单击“确定”。

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.

现在您有空的项目和解决方案文件,您可以在任何地方复制/粘贴它们;要调试,只需打开.sln文件,拖动单个.cpp文件,然后点击F5。

#2


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?

您是否正在尝试调试此cpp中定义的主例程?你添加的cpp是否可以使用它有一个主程序?

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

您始终可以编译cpp文件,但该语言定义了应用程序的入口点

int main()
{
}

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

(或使用argv / argc命令行args的签名,但这在这里并不重要)。

#3


The thread is quite old but my answer might help others as I faced the same problem. I have used Bloodshed DevC++ to compile single C++ file. No need to create a whole project using Visual Studio.

线程很老了,但是当我遇到同样的问题时,我的回答可能对其他人有帮助。我使用Bloodshed DevC ++编译单个C ++文件。无需使用Visual Studio创建整个项目。

#4


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

在VS.net 2008中创建Visaul C ++控制台应用程序,并将要调试的cpp文件添加到项目中。

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.

从主例程调用该cpp文件中定义的函数,该例程将在创建控制台项目时由VS.Net 2008为您创建。

#5


I debug by:

我调试:

  • putting a breakpoint where I want.
  • 把断点放在我想要的地方。

  • building the app inside Visual Studio.
  • 在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).
  • 按下F5以使用调试器启动应用程序(如果你正在调试它的dll,你将需要在项目属性中设置调用dll的exe)。

  • 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.

如果断点从通常的红点变为黄色三角形,则表示您没有调试您认为自己的代码 - 通常这是在您的dll未加载时发生的。如果您正在调试exe,那么您将看不到这一点。

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

另外,确保已构建(并具有项目类型)作为Debug。

#6


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.

您需要创建一个能够进行调试的解决方案。之后,您拥有多少源文件并不重要。您可以使用File-> New菜单创建解决方案。

#7


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):

每当我想快速调试控制台程序时,我发现控制台是最好的起点。所以我采用一个C ++程序(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):

然后在控制台上设置我的环境(来自cmd.exe):

vcvars32

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

然后编译我的程序(Zi是这样我得到一个blah.pdp来调试):

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).

瞧,我有一个可以运行的blah.exe。如果我想调试blah.exe我只是从VS2008打开一个项目并选择blah.exe而不是项目文件。我可以在IDE中打开blah.cpp并运行(F5),设置断点(F9)并通常调试我心中的内容(F10,F11)。

#1


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.

Visual Studio需要大量信息才能知道如何编译和调试源代码。像使用哪种优化设置,在哪里寻找增强头,这类事情。

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.

试试这个:转到File-> New-> Project ...并选择一个win32控制台应用程序。在下一个向导中,转到“应用程序设置”,选中“清空项目”,然后单击“确定”。

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.

现在您有空的项目和解决方案文件,您可以在任何地方复制/粘贴它们;要调试,只需打开.sln文件,拖动单个.cpp文件,然后点击F5。

#2


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?

您是否正在尝试调试此cpp中定义的主例程?你添加的cpp是否可以使用它有一个主程序?

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

您始终可以编译cpp文件,但该语言定义了应用程序的入口点

int main()
{
}

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

(或使用argv / argc命令行args的签名,但这在这里并不重要)。

#3


The thread is quite old but my answer might help others as I faced the same problem. I have used Bloodshed DevC++ to compile single C++ file. No need to create a whole project using Visual Studio.

线程很老了,但是当我遇到同样的问题时,我的回答可能对其他人有帮助。我使用Bloodshed DevC ++编译单个C ++文件。无需使用Visual Studio创建整个项目。

#4


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

在VS.net 2008中创建Visaul C ++控制台应用程序,并将要调试的cpp文件添加到项目中。

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.

从主例程调用该cpp文件中定义的函数,该例程将在创建控制台项目时由VS.Net 2008为您创建。

#5


I debug by:

我调试:

  • putting a breakpoint where I want.
  • 把断点放在我想要的地方。

  • building the app inside Visual Studio.
  • 在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).
  • 按下F5以使用调试器启动应用程序(如果你正在调试它的dll,你将需要在项目属性中设置调用dll的exe)。

  • 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.

如果断点从通常的红点变为黄色三角形,则表示您没有调试您认为自己的代码 - 通常这是在您的dll未加载时发生的。如果您正在调试exe,那么您将看不到这一点。

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

另外,确保已构建(并具有项目类型)作为Debug。

#6


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.

您需要创建一个能够进行调试的解决方案。之后,您拥有多少源文件并不重要。您可以使用File-> New菜单创建解决方案。

#7


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):

每当我想快速调试控制台程序时,我发现控制台是最好的起点。所以我采用一个C ++程序(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):

然后在控制台上设置我的环境(来自cmd.exe):

vcvars32

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

然后编译我的程序(Zi是这样我得到一个blah.pdp来调试):

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).

瞧,我有一个可以运行的blah.exe。如果我想调试blah.exe我只是从VS2008打开一个项目并选择blah.exe而不是项目文件。我可以在IDE中打开blah.cpp并运行(F5),设置断点(F9)并通常调试我心中的内容(F10,F11)。