我开始在Visual c++和我想知道如何保持控制台窗口。
例如,这将是一个典型的“hello world”应用程序:
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
return 0;
}
我漏了哪一行?
我开始在Visual c++和我想知道如何保持控制台窗口。
例如,这将是一个典型的“hello world”应用程序:
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
return 0;
}
我漏了哪一行?
当前回答
用Ctrl+F5启动项目,而不是只用F5。
控制台窗口现在将保持打开,按任意键继续…程序退出后的消息。
注意,这需要Console(/子系统:Console)链接器选项,您可以如下方式启用该选项:
Open up your project, and go to the Solution Explorer. If you're following along with me in K&R, your "Solution" will be 'hello' with 1 project under it, also 'hello' in bold. Right click on the 'hello" (or whatever your project name is.) Choose "Properties" from the context menu. Choose Configuration Properties>Linker>System. For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column. Choose "Console (/SUBSYSTEM:CONSOLE)" Click Apply, wait for it to finish doing whatever it does, then click OK. (If "Apply" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won't work.)
CTRL-F5和子系统提示一起工作;它们不是独立的选项。
(来自http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6的DJMorreTX提供)
其他回答
在返回行上放置断点。
你在调试器中运行它,对吧?
只需在_tmain方法的右括号中添加一个断点。这是一种更简单的方法,而且你不需要为了调试而添加代码。
用Ctrl+F5启动项目,而不是只用F5。
控制台窗口现在将保持打开,按任意键继续…程序退出后的消息。
注意,这需要Console(/子系统:Console)链接器选项,您可以如下方式启用该选项:
Open up your project, and go to the Solution Explorer. If you're following along with me in K&R, your "Solution" will be 'hello' with 1 project under it, also 'hello' in bold. Right click on the 'hello" (or whatever your project name is.) Choose "Properties" from the context menu. Choose Configuration Properties>Linker>System. For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column. Choose "Console (/SUBSYSTEM:CONSOLE)" Click Apply, wait for it to finish doing whatever it does, then click OK. (If "Apply" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won't work.)
CTRL-F5和子系统提示一起工作;它们不是独立的选项。
(来自http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6的DJMorreTX提供)
对于makefile项目,被接受的解决方案失败了,原因是Visual Studio中的一个bug(这个bug至少在2012版之前都存在-我还没有测试2013版)。这里详细介绍了这个错误。
为了在makefile项目的程序终止后让控制台暂停,执行以下步骤(这可能与2010 - 2012以外的版本有所不同):
1)将/子系统:CONSOLE传递给链接器。-编辑:见下文。
2)在文本编辑器中打开项目文件(.vcxproj)。
3)在根<project>标签内,插入以下内容:
< ItemDefinitionGroup > <链接> < SubSystem >控制台< / SubSystem > < /链接> < / ItemDefinitionGroup >
4)重新加载解决方案中的项目。
5)不调试运行程序(CTRL + F5)。
编辑:
根据我下面的评论,设置链接器选项/子系统:CONSOLE实际上与makefile项目无关(如果你使用的是MSVC以外的编译器,甚至不一定可能)。重要的是将设置添加到.vcxproj文件中,就像上面的第3步一样。
标准的方法是在return语句之前使用cin.get()。
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
cin.get();
return 0;
}