Problem:
CLion doesn't output any console output for debugging purposes.
I'm using CLion with the MingW compiler and cmake. No matter whether I use:
std::cout << "Testing" << std::endl;
Or:
printf("Testing");
I don't see any console output.
Attempts at Resolution:
1:
I've checked "Run", "Debug", "Terminal" and "Cmake". I've attempted to edit my configurations but "Debug" doesn't show up.
2:
Next, I went to Settings->Build,Execution,Deployment->CMake to edit the Generation types. I added Debug and RelWithDebInfo and still to no avail.
3:
I also attempted to add "-Debug" to Cmake, but I still have no output.
4:
The closest thing I've received for debugging is using GDB to view variable values at break points. This only works in the "RelWithDebInfo" generation.
Solution:
I ended up figuring out what the problem was.
I'm developing a Qt GUI application within CLion on Windows. You have to specify a console for console output to print onto.
Call this Console() function early in your main for a console prompt to open up. Now, whenever you run
QDebug() << <string>;
or
std::cout << <string> std::endl;
You'll see your debugging statements. Hope this helps somebody else out there with the same problem.
Code:
void Console()
{
AllocConsole();
FILE *pFileCon = NULL;
pFileCon = freopen("CONOUT$", "w", stdout);
COORD coordInfo;
coordInfo.X = 130;
coordInfo.Y = 9000;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),ENABLE_QUICK_EDIT_MODE| ENABLE_EXTENDED_FLAGS);
}
Source:
I found a solution here:
[0] Console output in a Qt GUI app?
There's a simpler solution that doesn't require adding any code. Simply add the following environment variable in your debug configuration:
QT_ASSUME_STDERR_HAS_CONSOLE=1
With this, CLion shows QDebug and QML's console.*() when started with a debugger.
Related
I'm trying to run this code and im a beginner at this im really struggling I don't know what to do.
NB: A "MinGW" version of Code::Blocks was used here.
#include <stdio.h>
#include <omp.h>
int main() {
printf ("Hello, world:");
#pragma omp parallel
printf (" %d", omp_get_thread_num ());
printf ("\n");
return 0;
}
Change a debugger setting in the following way.
From the Settings menu click on Debugger..., then on the left of the screen select Default, a debugger type. This should show Executable paths set to:
C:\MinGW\bin\gdb.exe
change this to:
C:\Program Files\Codeblocks\MinGW\bin\gdb.exe
You should then be able to use the debugger.
NB: Other Information
The above type of fix is required if you install the "MinGW" version of 'Code::Blocks 20.03' on Windows, using 'codeblocks-20.03mingw-setup.exe' ( the version relevant on 8th April 2021 , with the default installation type used ).
When doing a std::cout << "Start" << std::endl; in vscode for Mac it always prints #"Start\r\n" to the debug console.
Is there a way to get rid of the #"\r\n" characters so that it would only print Start?
Best, Hu
You probably currently using llbd , try using externalConsole instead, by adding "externalConsole": true to launch.json, which will send the output to a separate terminal window.
Currently I work with RPi2 and remote connection to NetBeans 8.1 on my laptop.
I can write programs, run it, debug it and go step-by-step through the lines. This tutorial helps me to setup this: Installing NetBeans For C++ Remote Development on A Raspberry Pi
First test: I tried a simple "Hello World" program like this:
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int i = 0;
printf("Hallo World!\n");
i++;
printf("Hallo World!\n");
i++;
printf("Hallo World!\n");
i++;
printf("Hallo World!\n");
i++;
}
and debug it stepwise with NetBeans. Result: Variable i increments fine, but no printf output comes up! If I run the program without debugging, all works fine. I tried additional fflush and so on but nothing works.
What way can I do to solve the problem?
OK, I found he answer by myself...
First: Disable buffering by
setbuf(stdout, NULL); // disable buffering on stdout
Next: Setup the project by: Right Click on the project name > Properties > Categories = "Run" and here Console type = "Standard output".
This works for me with stepwise debug every printf line-per-line.
Mike.F.
I have the following C# program (test.cs) which I want to debug:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Console.WriteLine( "Hello, World!" );
List<int> list = new List<int>();
list.Add(123);
list.Add(234);
list.Add(345);
list.Add(456);
int number = 4;
++number;
Console.WriteLine(" number " + number); // <--- L:16 I want to set a breakpoint here :)
Console.WriteLine("Number of elements" + list.Count);
foreach (Object obj in list)
{
Console.WriteLine(" " + obj);
}
Console.WriteLine("Bye");
}
}
And below is the debug session using lldb and mono (I am in OSX). I can start the session and run the program however I am not able to setup any breakpoint. I guess that when using mono as the executable file things are different. How can I achieve this?
$ mcs -debug test.cs
$ lldb
(lldb) target create mono
Current executable set to 'mono' (i386).
(lldb) b test.cs:16
Breakpoint 1: no locations (pending).
WARNING: Unable to resolve breakpoint to any actual locations.
(lldb) run --debug test.exe
Process 15191 launched: '/usr/bin/mono' (i386)
Hello, World!
number 5
Number of elements4
123
234
345
456
Bye
Process 15191 exited with status = 0 (0x00000000)
(lldb)
I've tried with GBD as this old guide suggests but it is worse, apparently there is something broken in mono in OSX that makes it impossible to debug unless the soft debugger is used (which is exactly what I want to avoid. MonoDevelop debugger is really really unstable/unreliable/slow). This is what I have tried with GBD. No luck at all.
And help is appreciated.
Isn't mono the interpreter for your language and test.exe the binary? lldb only knows about mono -- any breakpoints you try to set would be in the mono interpreter. e.g. if you were debugging an issue with mono itself.
This is a unique enough environment that you'll need to do a little analysis to understand what lldb sees. What do you get for
(lldb) image list test.exe
? Of course if you do
(lldb) im li mono
You'll see mono listed -- because that's a binary that lldb knows about. (you can use plain image list aka im li to see all the binaries that lldb knows)
You can ask lldb to list all of the line table entries based on a source filename with a command line
(lldb) target modules dump line-table test.cs
if lldb has any debug information for a test.cs, you'll see a line table. Without a line table, file and line breakpoints (b test.cs:16) won't work.
For what it's worth, I don't think trying to use lldb (or gdb) to debug your C# program will work. You'll probably need to use some facility of the mono runtime itself to set breakpoints and examine program state.
I am using microsoft visual studio to do C++.
I don't see std::err and std::out in the output console of the IDE.
Is there a way to redirect them ?
You can indeed redirect std::out and std::err. Simply right click on your project in the solution explorer and select Properties. Then select Configuration Properties -> Debugging and put the appropriate arguments into the Command Arguments field. For example, to redirect std::err to a file, I would type in 2> ErrorLog.txt.
The things you type in Command Arguments simply get appended as command line arguments when Visual Studio runs your program, just like you had manually typed them in to the console. So, the above example simply tells VisualStudio to run your program with the command <programName>.exe 2> ErrorLog.txt instead of just <programName>.exe.
I know this is an old thread but I can't help but giving the answer since I can't believe there still is no real answer. What you can do is redirect the cout to a ostringstream of your choice. To do this, derive a new class from streambuf that will send the stream to OutputDebugString (Lets call this class OutputDebugStream) and create an instance of the class, myStream. Now call:
cout.rdbuf(&myStream)
I used cout for an example. This same technique can be used with cerr, just call
cerr.rdbuf(&myStream).
Stdout is a little more difficult if you are not using cout. You can redirect stdout during runtime by using freopen(), but it must be to a file. To get this to redirect to the Debug screen is a little more difficult. One way is to use fmemopen() if it is available (it is not standard) and write a streambuf to output this data to the Debug screen. Alternatively, you can redirect to a file and write a stream to open as input and redirect to the debug stream. A bit more work, but I think it is possible.
I use the following macro for output to the visual studio console
#ifdef _MSC_VER
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <opencv/cxcore.h>
#define DBOUT( s ) \
{ \
std::wostringstream os_; \
os_ << s; \
OutputDebugStringW( os_.str().c_str() ); \
}
#else
#define DBOUT( s ) \
{ \
std::cout << s; \
}
#endif
Now if I could only get it to work within a cuda kernel?!