C++ 在调试模式下执行控制台应用程序后,如何使 Visual Studio 暂停?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/193469/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:35:40  来源:igfitidea点击:

How do I make Visual Studio pause after executing a console application in debug mode?

c++visual-studiounit-testingboostconsole

提问by Jason Dagit

I have a collection of Boost unit tests I want to run as a console application.

我有一组 Boost 单元测试,我想作为控制台应用程序运行。

When I'm working on the project and I run the tests I would like to be able to debug the tests, and I would like to have the console stay open after the tests run.

当我处理项目并运行测试时,我希望能够调试测试,并且希望在测试运行后控制台保持打开状态。

I see that if I run in release mode the console window stays up after the program exits, but in debug mode this is not the case.

我看到如果我在发布模式下运行,则程序退出后控制台窗口会保持打开状态,但在调试模式下情况并非如此。

I do not want to add 'system("pause");' or any other hacks like reading a character to my program. I just want to make Visual Studio pause after running the tests with debugging like it would if I were running in release mode. I would also like it if the output of tests were captured in one of Visual Studio's output windows, but that also seems to be harder than it should be.

我不想添加 'system("pause");' 或任何其他技巧,例如在我的程序中读取字符。我只想在运行调试测试后让 Visual Studio 暂停,就像我在发布模式下运行一样。如果测试的输出是在 Visual Studio 的输出窗口之一中捕获的,我也希望如此,但这似乎也比应有的困难。

How can I do this?

我怎样才能做到这一点?

采纳答案by Raz

Boost test offers the following usage recommendations for Visual Studiothat would enable you to run the unit tests automatically at the end of compilation and capture the output into the build window.

Boost 测试为 Visual Studio提供以下使用建议,使您能够在编译结束时自动运行单元测试并将输出捕获到构建窗口中。

The nice side effect of this trick is it enable you to treat test failures as compilation errors. "...you could jump through these errors using usual keyboard shortcuts/mouse clicks you use for compilation error analysis..."

这个技巧的好的副作用是它使您能够将测试失败视为编译错误。“......你可以使用通常用于编译错误分析的键盘快捷键/鼠标点击跳过这些错误......”

回答by Cichy

Try to run the application with the Ctrl+ F5combination.

尝试使用Ctrl+F5组合运行应用程序。

回答by Kirill Golikov

http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5

http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5

In the older versions it would default to the console subsystem even if you selected "empty project", but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "project_nameproperties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.

在旧版本中,即使您选择了“空项目”,它也会默认为控制台子系统,但在 2010 年不会,因此您必须手动设置它。为此,请在右侧或左侧的解决方案资源管理器中选择项目(可能已被选中,因此您不必担心)。然后从菜单栏下拉菜单中选择“project”,然后选择“ project_nameproperties”>“configuration properties”>“linker”>“system”并设置第一个属性,下拉“subsystem”属性为“console(/子系统:控制台)”。控制台窗口现在应该像往常一样在执行后保持打开状态。

回答by Kevin Timmerman

Set a breakpoint on the last line of code.

在最后一行代码上设置断点。

回答by Ming

I just copied from http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787/?prof=required:

我刚刚从http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787/?prof=required复制:

The following works for me :-)

以下对我有用:-)

/////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////// /////////////////////////////////////

Here is another reason the console may disappear. And the solution:

这是控制台可能消失的另一个原因。和解决方案:

With the new Visual Studio 2010 you might see this behavior even when you use Ctrl+ F5aka "start without debugging". This is most likely because you created an "empty project" instead of a "Win32 console application". If you create the project as a "Win32 console application" you can disregard this as it does not apply.

使用新的 Visual Studio 2010,即使您使用Ctrl+ F5aka“不调试启动”,您也可能会看到这种行为。这很可能是因为您创建了“空项目”而不是“Win32 控制台应用程序”。如果您将项目创建为“Win32 控制台应用程序”,您可以忽略它,因为它不适用。

In the older versions it would default to the console subsystem even if you selected "empty project", but not in Visual Studio 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this).

在旧版本中,即使您选择了“空项目”,它也会默认为控制台子系统,但在 Visual Studio 2010 中则不会,因此您必须手动设置它。为此,请在右侧或左侧的解决方案资源管理器中选择项目(可能已被选中,因此您不必担心)。

Then select "project" from the menu bar drop down menus, then select "project_nameproperties" → "configuration properties" → "linker" → "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.

然后从菜单栏下拉菜单中选择“project”,然后选择“ project_nameproperties”→“configuration properties”→“linker”→“system”并设置第一个属性,下拉“subsystem”属性为“console(/子系统:控制台)”。控制台窗口现在应该像往常一样在执行后保持打开状态。

/////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////// /////////////////////////////////////

回答by delhister

If it is a console application, use Ctrl+ F5.

如果是控制台应用程序,请使用Ctrl+ F5

回答by razzmatazz

In Boost.Test there is the --auto_start_dbgparameter for breaking into the debugger when a test fails (on an exception or on an assertion failure). For some reason it doesn't work for me.

在 Boost.Test 中有一个--auto_start_dbg参数,用于在测试失败时(异常或断言失败)进入调试器。出于某种原因,它对我不起作用。

See http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/usage-recommendations/dot-net-specific.html

请参阅http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/usage-recommendations/dot-net-specific.html

For this reason I have created my custom test_observer that will break into the debugger when there is an assertion failure or an exception. This is enabled on debug builds when we are running under a debugger.

出于这个原因,我创建了我的自定义 test_observer,当出现断言失败或异常时,它将中断调试器。当我们在调试器下运行时,这在调试版本上启用。

In one of the source files of my unit test EXE file I have added this code:

在我的单元测试 EXE 文件的源文件之一中,我添加了以下代码:

#ifdef _DEBUG

#include <boost/test/framework.hpp>
#include <boost/test/test_observer.hpp>

struct BoostUnitTestCrtBreakpointInDebug: boost::unit_test::test_observer
{
    BoostUnitTestCrtBreakpointInDebug()
    {
        boost::unit_test::framework::register_observer(*this);
    }

    virtual ~BoostUnitTestCrtBreakpointInDebug()
    {
        boost::unit_test::framework::deregister_observer(*this);
    }

    virtual void assertion_result( bool passed /* passed */ )
    {
        if (!passed)
            BreakIfInDebugger();
    }

    virtual void exception_caught( boost::execution_exception const& )
    {
        BreakIfInDebugger();
    }

    void BreakIfInDebugger()
    {
        if (IsDebuggerPresent())
        {
            /**
             * Hello, I know you are here staring at the debugger :)
             *
             * If you got here then there is an exception in your unit
             * test code. Walk the call stack to find the actual cause.
             */
            _CrtDbgBreak();
        }
    }
};

BOOST_GLOBAL_FIXTURE(BoostUnitTestCrtBreakpointInDebug);

#endif

回答by Die in Sente

You say you don't want to use the system("pause")hack. Why not?

你说你不想使用system("pause")黑客。为什么不?

If it's because you don't want the program to prompt when it's notbeing debugged, there's a way around that. This works for me:

如果是因为您不希望程序在调试时提示,则有一种解决方法。这对我有用:

void pause () {
    system ("pause");
}

int main (int argc, char ** argv) {
    // If "launched", then don't let the console close at the end until
    // the user has seen the report.
    // (See the MSDN ConGUI sample code)
    //
    do {
        HANDLE hConsoleOutput = ::GetStdHandle (STD_OUTPUT_HANDLE);
        if (INVALID_HANDLE_VALUE == hConsoleOutput)
            break;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if (0 == ::GetConsoleScreenBufferInfo (hConsoleOutput, &csbi))
            break;
        if (0 != csbi.dwCursorPosition.X)
            break;
        if (0 != csbi.dwCursorPosition.Y)
            break;
        if (csbi.dwSize.X <= 0)
            break;
        if (csbi.dwSize.Y <= 0)
            break;
        atexit (pause);
    } while (0);

I just paste this code into each new console application I'm writing. If the program is being run from a command window, the cursor position won't be <0,0>, and it won't call atexit(). If it has been launched from you debugger (any debugger) the console cursor position will be <0,0> and the atexit()call will be executed.

我只是将此代码粘贴到我正在编写的每个新控制台应用程序中。如果程序是从命令窗口运行的,光标位置不会是 <0,0>,也不会调用atexit(). 如果它已从您的调试器(任何调试器)启动,则控制台光标位置将为 <0,0> 并且atexit()将执行调用。

I got the idea from a sample program that used to be in the MSDN library, but I think it's been deleted.

我从曾经在 MSDN 库中的一个示例程序中得到了这个想法,但我认为它已被删除。

NOTE: The Microsoft Visual Studio implementation of the system() routine requires the COMSPEC environment variable to identify the command line interpreter. If this environment variable gets messed up -- for example, if you've got a problem in the Visual Studio project's debugging properties so that the environment variables aren't properly passed down when the program is launched -- then it will just fail silently.

注意:system() 例程的 Microsoft Visual Studio 实现需要 COMSPEC 环境变量来识别命令行解释器。如果这个环境变量搞砸了——例如,如果你在 Visual Studio 项目的调试属性中遇到问题,以至于在程序启动时环境变量没有正确传递——那么它只会默默地失败.

回答by Kibbee

It would actually be more effort, but you could just build in VS.Net, run it from the regular command line (cmd.exe), and then attach to the process after it starts running. This is probably not the solution you are looking for however.

这实际上会更费力,但您可以只在 VS.Net 中构建,从常规命令行 (cmd.exe) 运行它,然后在它开始运行后附加到该进程。然而,这可能不是您正在寻找的解决方案。

回答by dexblack

Or you could use boost_test "Test Log Output."

或者您可以使用 boost_test “测试日志输出”。

http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-output/test-log.html

http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-output/test-log.html

Then it won't matter whether the console window shows up at all AND your build logging can preserve the unit testing output as an artifact for examination on failed builds...

那么,控制台窗口是否出现都无关紧要,并且您的构建日志记录可以将单元测试输出保留为用于检查失败构建的工件...