I use VSCode to create the .exe file of the simple following C++ code:
#include <iostream>
using namespace std;
int main()
{
int x = 25;
cout << x << endl;
return 0;
}
When I open the .exe program created, the window appears and shuts down immediately. If I put getchar() before return 0, I can make the program wait for user input to fix it. However, if I use Dev-C or Codeblocks, this problem does not happen. In other word, I do not need to add getchar() to hold the window. Hope that someone can explain what problem occurs and how can I fix it to make the .exe created by VSCode run normally? Thank you so much.
Related
I am trying to use libgpm, looks like the program detects mouse clicks,
but as a side effect it prints something like this to the terminal:
^[[M <7^[[M#<7^[[M <7^[[M#<7^[[M <7^[[M#<7^[[M <7^[[M#<7^[[M Y=^[[M#M<^[[M !=^[[M#)=
Even if I remove any calls to Gpm_Getc(), leaving the code as simple as this:
#include <gpm.h>
int main(int argc, char *argv[])
{
Gpm_Connect conn;
int c;
conn.eventMask = 0;
conn.defaultMask = ~0;
conn.minMod = 0;
conn.maxMod = ~0;
if (Gpm_Open(&conn, 0) == -1)
printf("Cannot connect to mouse server\n");
while (1);
Gpm_Close();
return 0;
}
I still see those gibberish codes. I reckon, they represent the mouse events. But in my code there is no direct instruction to print them. Why are they printed to my terminal? How can this be avoided?
I use gnome-terminal on linux, not a real console, if that matters.
If the TERM environment variable is xterm, GPM won't try to open the Linux console.
Instead, it simply turns on xterm mouse-mode (which makes the terminal send escape sequences), and it turns out, expects the application to handle that rather than transforming the escape sequences into its protocol.
From ncurses' viewpoint, for instance, that makes its behavior in a terminal emulator less than useful, and the library checks for this case and ignores GPM (since 2010).
I am able to compile and run my visual c++ program. It is not a console application. I am modifying an existing MFC application. I am trying to troubleshoot my program. I cannot run in debug and get the traces I need because my program also reads the mouse cursor. I also tried to use a messagebox to output this string, but again, a message box interferes with the mouse.
In the output window, I right-click and make sure that Program Output is enabled.
cout << "something" << endl;
However, in the output window, I do not see anything.
Looking at SO posts, I tried
std::string stro = "something ";
OutputDebugString(stro);
error C2664: 'void OutputDebugStringW(LPCWSTR)' : cannot convert
argument 1 from 'std::string' to 'LPCWSTR'
So changed to std::wstring stro
and
OutputDebugString(stro.c_str());
to append an int, I had to
std::wostringstream wso;
wso << i;
stro = stro + wso.str();
OutputDebugString(stro.c_str());
But I cannot see output in the window when not running in DEBUG. Is there any way to see output in non-DEBUG? This is surprisingly frustrating.
In the comments, writing a separate overloaded class was suggested; this seems like overkill. Java has System.out.println even in GUI programs. Android has Log.v(). Such a pity the equivalent is not available here.
Console applications have console output, which is what cout sends to. Since this isn't a console application, you'll want to try another approach. It's strange you object "I don't see Debug output when I'm not in Debug", that's the point of it - don't use OutputDebugString and expect it to be for something other than debug output.
I think the best way to understand what your application is doing, without interacting with it under the debugger (I know the frustration of trying to debug event handlers that keep being re-triggered by your debugging activities) is to try tracepoints. I blogged about them back in 2006 and they're still a great technique. Reading the mouse cursor won't interfere with tracepoints, and you can turn them on and off without rebuilding your app or making any code changes.
Set up a breakpoint, then right-click the red dot and choose When Hit. You can adjust the default message that goes into the output window to show you any values you are interested in - you can even call functions in the trace message, as you can see in the blog entry where I call the size() method of a collection. If necessary, you can even debug a release build.
Actually OutputDebugStrng should work in release builds - as long as you're running the app from the debugger. However cout cannot route output to the VS output pane.
If you already have a lot of 'cout' style debugging code, the easiest route might be to replace it with a custom ostream overload which does print to the output pane. Here's one, here's another.
If you can rewrite the debugging code completely, some macro wrappers around OutputDebugString might be better for you.
Having a stdout/stderr console window for debugging is very useful, and I always miss it when working on WinMain based apps. I use this code fragment to create a console for windows apps (and chain it in with existing loggers, etc.) After running this, your cout/cerr should work fine. OutputDebugString output can be seen outside of DevStudio using the DebugView app, but I prefer this:
#include <io.h>
#include <fcntl.h>
...
// DOS box console for stdin/stdout/stderr
void makeConsole()
{
AllocConsole();
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long)handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 2);
*stdout = *hf_out;
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long)handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 2);
*stdin = *hf_in;
HANDLE handle_err = GetStdHandle(STD_ERROR_HANDLE);
hCrt = _open_osfhandle((long)handle_err, _O_TEXT);
FILE* hf_err = _fdopen(hCrt, "w");
setvbuf(hf_err, NULL, _IONBF, 2);
*stderr = *hf_err;
ios::sync_with_stdio();
}
I have just started using Visual Studio 2012, which I wish to use to write C++ code.
To get to know how the IDE works I have created a very simple 'add two numbers' program.
#include<iostream>
#include<cstdlib>
int add(int a , int b)
{
return a+b;
}
int main(int argc, char** argv)
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
std::cout << "The sum of the arguments is " << add(a,b) << std::endl;
return 0;
}
If I hard-code the values of 'a' and 'b' (ie no argv's require to be passed), then using F7 to compile and Ctrl F5 to run the executable makes the program run perfectly.
But how do I pass the argv's to the program if I want to specify them at run time?
According to the 2nd answer on this SO thread I have to use Project Tab-> Properties-> Configuration Properties-> Debugging and then enter in the Right Hand Side under (Command Arguments),
But this seems very inconvenient if I want to make several quick runs of the program and test out the executable for different 'argv's.
You can try CLI Args Made Easy from http://n0n4m3.codingcorner.net/?p=214.
There are versions for VS2010 and VS2012.
You may need to rename the downloaded file from .zip to .vsix, eg: CLIArgsMadeEasy2012.vsix
Double-click to install it.
Run Visual Studio.
Show the toolbar by right-clicking the toolbar area, and click CLIArgsMadeEasy
The toolbar appears with one textbox for command line arguments (CLIArgs) and a combobox (Startup Project)
Enter the argument in CLIArgs and remember to press the Enter key, otherwise the arguments will not be saved.
Run your program.
I'm just starting learning C++/XAML windows store app development but for the life of me I can't find a nice way to print variable values to the "Output" window in VS2012.
Debug.WriteLine() doesn't seem to exist for Windows Store apps and I can't find a way to print other than OutputDebugString() which I can't use to print variable values (without some heavy formatting).
Is there just an easy way to print the example line:
mouse position X: 12
for example, where 12 is an integer that comes from MouseDelta.
Thanks for your time,
Poncho
Not really, no. You could write a little function that formatted like printf and passed along the resulting string to OutputDebugString() but there's nothing more straightforward available.
I guess you could use ToString(), Platform::String::operator+, and Platform::String::Data() to accomplish this; though it's a little ugly:
OutputDebugString( ("mouse position X:" + MouseDelta.X.ToString())->Data() );
Here is a nice alternative: http://seaplusplus.com/2012/06/25/printf-debugging-in-metro-style-apps/, basically it allocates a console for your Windows Store App, obviously this will fail certification but given that this may be just for debug purposes only, it will fine. I'm copying the relevant code here:
// Include Windows.h for WINBASEAPI and WINAPI:
#include <Windows.h>
// Declare AllocConsole ourselves, since Windows.h omits it:
extern "C" WINBASEAPI int WINAPI AllocConsole();
auto main(Platform::Array<Platform::String^>^) -> int
{
AllocConsole();
std::wcout << L"Hello there!" << std::endl;
std::getchar();
return EXIT_SUCCESS;
}
However if you want to see such output inside your app, then you may want to use Console Class for Modern UI Apps which implements part of the .NET System.Console and can be safely used inside Windows Store apps.
This solution uses a wrapper around OutputDebugString:
void WinLog(const wchar_t *text, int n)
{
wchar_t buf[1024];
_snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);
OutputDebugString(buf);
}
which can be called as follows:
WinLog(L"The Answer is", 42);
I have a funny situation. I am sitting on a computer on which there is a shared folder, say, "My_Folder". So, I can access it using the address "\My_Box\My_Folder".
Now, if I write a small tool in C#, I can convert the "\My_Box\My_Folder" to "C:\My_Folder" which is its physically location.
The funny thing is that I don't know how to find the physically location of "My_Box\My_Folder" without my program ... And it's actually one of my friend's problem.
So if anyone knows how to find the local path from a network path with some simple basic Windows commands/operations (either Winxp, 2000, vista, 7, ...), please let me know.
Thanks,
Pete.
If all you need is a command line tool that will provide this information then you can simply use the net share command that’s built into Windows.
If you need to do it programmatically you can use the NetShareGetInfo function. The following example shows how to use it to query the path for \\localhost\share in C++.
#include <windows.h>
#include <lm.h>
#pragma comment (lib, "netapi32.lib")
#include <iostream>
using namespace std;
int main()
{
BYTE * buffer = nullptr;
auto error = NetShareGetInfo(nullptr, // local computer
L"share",
2, // level
&buffer);
if (ERROR_SUCCESS == error)
{
auto info = reinterpret_cast<SHARE_INFO_2 *>(buffer);
wcout << info->shi2_path << endl;
}
if (buffer)
{
NetApiBufferFree(buffer);
}
}
I would wrap the buffer in a class and call NetApiBufferFree from its destructor but that’s up to you.