C++ 如何在 Visual Studio 中调试单个 .cpp 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I debug a single .cpp file in Visual Studio?
提问by patricksweeney
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。
回答by Ben Straub
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 需要大量信息才能了解如何编译和调试源代码。诸如要使用哪些优化设置、在哪里查找boost
标题之类的事情。
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 控制台应用程序。在下一个向导中,转到Application Settings
并选中“Empty Project”,然后点击 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.
现在您有空的项目和解决方案文件,您可以将它们复制/粘贴到您想要的任何位置;要调试,只需打开.sln
文件,拖入您的单个.cpp
文件,然后按 F5。
回答by Doug T.
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 命令行参数的签名,但这在这里并不重要)。
回答by Nadjib
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 创建整个项目。
回答by Naveen
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 菜单创建解决方案。
回答by RedBlueThing
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)。
回答by J.W.
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 将为您创建该函数。
回答by gbjbaanb
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! :)
- 在我想要的地方放置一个断点。
- 在 Visual Studio 中构建应用程序。
- 按 F5 以使用调试器启动应用程序(如果它是您正在调试的 dll,您需要在项目属性中设置调用 dll 的 exe)。
- 利润!:)
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。