Visual Studio 2012 C ++标准输出 [英] Visual Studio 2012 C++ Standard Output
问题描述
在编译Win32应用程序时,fprintf(stdout / stderr)在Visual Studio中打印到哪里?我不断听到它的输出,但我看不到它!
Where does fprintf(stdout/stderr) print to in Visual Studio when compiling Win32 app? I keep hearing it goes to the output but I can't see it!.
在c ++中没有控制台窗口,打印输出日志的标准方式是什么?
Whats the standard way of printing to the output log without having a console window in c++?
推荐答案
如果你的程序与/ SUBSYSTEM:WINDOWS链接,你将不会看到控制台输出,除非你分配一个控制台。
If your program is linked with /SUBSYSTEM:WINDOWS you will not see the console output unless you allocate a console.
这里是用于分配控制台选项的代码。使用此方法,您不需要混乱链接器设置或创建WinMain。
Here is code for the allocate console option.With this method you should not need to mess with linker settings or create a WinMain.
static void OpenConsole()
{
int outHandle, errHandle, inHandle;
FILE *outFile, *errFile, *inFile;
AllocConsole();
CONSOLE_SCREEN_BUFFER_INFO coninfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 9999;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
outHandle = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
errHandle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE),_O_TEXT);
inHandle = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE),_O_TEXT );
outFile = _fdopen(outHandle, "w" );
errFile = _fdopen(errHandle, "w");
inFile = _fdopen(inHandle, "r");
*stdout = *outFile;
*stderr = *errFile;
*stdin = *inFile;
setvbuf( stdout, NULL, _IONBF, 0 );
setvbuf( stderr, NULL, _IONBF, 0 );
setvbuf( stdin, NULL, _IONBF, 0 );
std::ios::sync_with_stdio();
}
如果您不想直接分配控制台,还可以通过更改子系统的链接器设置将子系统从/ SUBSYSTEM:windows更改为/ SUBSYSTEM:CONSOLE。记住当/ SUBSYSTEM:CONSOLE被启用时,gui仍然会像以前一样工作,但是窗口会为你和应用程序创建一个控制台窗口。
If you do not want to allocate a console directly you can also change the subsystem from /SUBSYSTEM:windows to /SUBSYSTEM:CONSOLE via changing the Subsystem of the linker settings. Remember when /SUBSYSTEM:CONSOLE is enabled the gui will still work the same as before but windows will create a console window for you along with your application.
在我的Qt代码,这是所有需要的。但是,当我在VisualStudio中尝试MFC并通过链接器设置将子系统设置为控制台。我得到以下错误:
In my Qt code that is all that is needed. However when I tried MFC in VisualStudio and set the subsystem to console via the linker setting. I got the following error:
1>------ Build started: Project: MFCApplication1, Configuration: Debug Win32 ------
1> MFCApplication1.cpp
1>msvcrtd.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>X:\Test\VC.110\MFCTest\MFCApplication1\Debug\MFCApplication1.exe : fatal error LNK1120: 1 unresolved externals
入口点默认为main()在控制台应用程序和WinMain在Windows应用程序。要解决这个问题,我必须在高级链接器设置的入口点设置中添加以下内容:wWinMainCRTStartup
This is caused by the entry point defaulting to main() in console applications and WinMain in windows applications. To fix this I had to add the following to the Entry Point setting of the Advanced Linker settings: "wWinMainCRTStartup"
在评论中,Ben Voigt建议使用另一种方法。使用editbin更改子系统不需要更改入口点。这是真的。我把删除的入口点和放回windows作为子系统构建测试应用程序,然后使用editbin更改子系统使用以下命令:
In the comments Ben Voigt suggested an alternate method. Using editbin to change the subsystem does not require a change in the entry point. This is indeed the case. I put removed the entry point and put windows back as the subsystem built the test application then used editbin to change the subsystem using the following command:
X:\Test\VC.110\MFCTest\MFCApplication1\Debug>editbin MFCApplication1.exe /SUBSYSTEM:CONSOLE
Microsoft (R) COFF/PE Editor Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
我在控制台中得到了我预期的输出:
And I got the output I expected in the console:
X:\Test\VC.110\MFCTest\MFCApplication1\Debug>MFCApplication1.exe
Hello from a windows application!
注意:使用editbin方法,更新可执行文件。
Note: With the editbin method you need to renable this every time you update the executable.
最后在任何一个方法,一旦你有控制台printf或std :: cout将工作。例如在我的测试MFC应用程序中,我添加了以下行到CMFCApplication1App类的构造函数:
And finally on either method once you have the console printf or std::cout will work. For example in my test MFC application I added the following line to the constructor of the CMFCApplication1App class:
std::cout << "Hello from a windows application!" << std::endl;
这篇关于Visual Studio 2012 C ++标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!