visual-studio 程序结束后退出 Visual Studio 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2639891/
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
Visual studio 2010 exiting after program ends
提问by yCalleecharan
I'm trying out Visual studio 2010 by compiling a C program. After it displays the solution in the "DOS" command window, the window is immediately closed. In Visual studio 2008, one gets the message press any key to continue and pressing a key closes the command prompt window. How do I set this behavior in 2010?
我正在通过编译 C 程序来试用 Visual Studio 2010。在“DOS”命令窗口中显示解决方案后,该窗口立即关闭。在 Visual Studio 2008 中,您会收到消息 press any key to continue 并按一个键关闭命令提示符窗口。如何在 2010 中设置此行为?
回答by vitavim
After a bit of googling, I found thissolution that does not involve modifying your code. It contains a workaround that involves modifying your .vcxproj file.
经过一番谷歌搜索,我发现这个解决方案不涉及修改您的代码。它包含一个涉及修改 .vcxproj 文件的解决方法。
To do this from within Microsoft Visual C++ 2010 Express (I'm assuming it is similar for Visual Studio 2010), open your project and navigate to the following menu:
要在 Microsoft Visual C++ 2010 Express 中执行此操作(我假设它与 Visual Studio 2010 类似),请打开您的项目并导航到以下菜单:
Project->$YOURPROJECTNAME Properties...
->Configuration Properties
->Linker
->System->SubSystem
Then use the dropdown to select Console (/SUBSYSTEM:CONSOLE)and apply the change.
然后使用下拉菜单选择控制台 (/SUBSYSTEM:CONSOLE)并应用更改。
"Start without debugging" should do the right thing now.
“不调试就开始”现在应该做正确的事情。
回答by linnx88
The reason this happens is because now in VS 2010 it is possible to create an empty, generic C++ project by default, without having to go through the wizard. This causes VS 2010 to not properly set the Console (/SUBSYSTEM:CONSOLE)flag and so VS2010 has no idea it is a console application for which it would send the usual "Press any key..." prompt.
发生这种情况的原因是,现在在 VS 2010 中,默认情况下可以创建一个空的通用 C++ 项目,而无需通过向导。这会导致 VS 2010 没有正确设置Console (/SUBSYSTEM:CONSOLE)标志,因此 VS2010 不知道它是一个控制台应用程序,它会发送通常的“按任意键...”提示。
This problem doesn't occur if you create a Console Application project type from the New Project menu.
如果您从“新建项目”菜单创建控制台应用程序项目类型,则不会发生此问题。
But then you can set this flag yourself and many others, through Project/Settings, as the above post has answered correctly!
但是你可以自己和许多其他人通过项目/设置来设置这个标志,因为上面的帖子已经正确回答了!
回答by gzdcnklc
If you want it to stay open, you need to add code to your program. Like it
如果您希望它保持打开状态,则需要将代码添加到您的程序中。喜欢它
#include <stdio.h>
#include <stdlib.h>
... main(...)
{
your codes
.
.
.
system("pause");
}
if you add this code ,your black screen stay at open. " system("pause") " inside the stdlib.h header file.
如果您添加此代码,您的黑屏将保持打开状态。" system("pause") " 在 stdlib.h 头文件中。
回答by Joel Coehoorn
This is normal. The "DOS" console window is attached to your program and is supposed to exit when your program finishes. If you want it to stay open, you need to add code to your program to keep it open. All you have to do is add a print statement and then input statement to the end of your program.
这是正常的。“DOS”控制台窗口附加到您的程序,应该在您的程序完成时退出。如果您希望它保持打开状态,则需要向程序中添加代码以使其保持打开状态。你所要做的就是在你的程序末尾添加一个打印语句,然后输入语句。
回答by Gem
Bring "Start without debug" to the Debug menu...
将“不调试启动”带到调试菜单...
Tools >> Customize >> Commands(Tab) >> Menu bar(drop down list) >> Debug(Controls Option) >> Add Command(button) >> Debug(Categories List) >> Start without debug
工具 >> 自定义 >> 命令(选项卡)>> 菜单栏(下拉列表)>> 调试(控件选项)>> 添加命令(按钮)>> 调试(类别列表)>> 无需调试即可启动
Using "Start without debug" Will allow VS2010 to display the "Press any key to continue" phrase before exiting.
使用“Start without debug”将允许VS2010在退出前显示“按任意键继续”短语。
回答by Michael Burr
FWIW, the "Start Without Debugging" command works in VS2010 exactly the same way it does for me in previous Visual Studio versions (that is, for a console project the console remains open when the process terminates with a "Press any key to continue . . ." prompt).
FWIW,“开始而不调试”命令在 VS2010 中的工作方式与它在以前的 Visual Studio 版本中对我的工作方式完全相同(即,对于控制台项目,当进程以“按任意键继续”终止时,控制台保持打开状态。 。 。“ 迅速的)。
So, exactly how are you getting VS2010 to run your program? If you're using the "Ctrl-F5" shortcut, can you verify that Ctrl-F5 is bound to Debug.StartWithoutDebuggingin "Options/Environment/Keyboard"?
那么,您究竟如何让 VS2010 运行您的程序?如果您使用的是“Ctrl-F5”快捷键,您能否验证 Ctrl-F5 是否已绑定到Debug.StartWithoutDebugging“选项/环境/键盘”中?
Also, can you try running your program using the menu ("Debug/Start Without Debugging")?
另外,您可以尝试使用菜单(“调试/启动而不调试”)运行您的程序吗?
回答by C?t?lin Piti?
If you start your application without debugger ("Start without debugging"), the console window will remain open until you press a key.
如果您在没有调试器的情况下启动应用程序(“启动而无需调试”),控制台窗口将保持打开状态,直到您按下某个键。
回答by Darel
You can add this function and call it right before returning from main():
您可以添加此函数并在从 main() 返回之前调用它:
void keep_window_open()
{
// clear buffer
fflush(stdin);
printf("Please enter a character to exit\n");
char ch;
ch = getchar();
return;
}
I adapted it from a C++ version in a utility header file at Bjarne Stroustrup's site: http://www.stroustrup.com/Programming/std_lib_facilities.h
我从 Bjarne Stroustrup 网站的实用程序头文件中的 C++ 版本改编而来:http: //www.stroustrup.com/Programming/std_lib_facilities.h

