简体   繁体   English

Visual Studio 2010在计划结束后退出

[英]Visual studio 2010 exiting after program ends

I'm trying out Visual studio 2010 by compiling a C program. 我正在通过编译C程序来尝试Visual Studio 2010。 After it displays the solution in the "DOS" command window, the window is immediately closed. 在“DOS”命令窗口中显示解决方案后,窗口立即关闭。 In Visual studio 2008, one gets the message press any key to continue and pressing a key closes the command prompt window. 在Visual Studio 2008中,可以按任意键获取消息以继续并按下某个键关闭命令提示符窗口。 How do I set this behavior in 2010? 如何在2010年设置此行为?

After a bit of googling, I found this solution 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. 然后使用下拉列表选择Console(/ SUBSYSTEM:CONSOLE)并应用更改。

"Start without debugging" should do the right thing now. “没有调试就开始”应该做正确的事情。

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. 发生这种情况的原因是因为现在在VS 2010中,默认情况下可以创建一个空的通用C ++项目,而无需通过向导。 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没有正确设置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! 但是,您可以通过项目/设置自己和许多其他人设置此标志,因为上面的帖子已正确回答!

This is normal. 这个是正常的。 The "DOS" console window is attached to your program and is supposed to exit when your program finishes. “DOS”控制台窗口附加到您的程序,并且应该在程序完成时退出。 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. 您所要做的就是添加一个print语句,然后输入语句到程序的末尾。

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. “系统(”暂停“)”在stdlib.h头文件中。

Bring "Start without debug" to the Debug menu... 将“Start without debug”带到Debug菜单......

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. 使用“无需调试启动”将允许VS2010在退出前显示“按任意键继续”短语。

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? 那么,究竟你是如何让VS2010运行你的程序的呢? If you're using the "Ctrl-F5" shortcut, can you verify that Ctrl-F5 is bound to Debug.StartWithoutDebugging in "Options/Environment/Keyboard"? 如果您使用的是“Ctrl-F5”快捷方式,可以验证Ctrl-F5是否绑定到“选项/环境/键盘”中的Debug.StartWithoutDebugging

Also, can you try running your program using the menu ("Debug/Start Without Debugging")? 另外,您可以尝试使用菜单运行程序(“Debug / Start Without Debugging”)吗?

如果在没有调试器的情况下启动应用程序(“不启动调试”),控制台窗口将保持打开状态,直到您按下某个键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM