Getting stdout using vscode mingw gcc - c++

I wanted to extent my c knowledge to c++. Using Win10, I installed VSCode and mingw following the tutorials.
Next I created a Hello World test file.
It compiles properly without errors. However when I run it from a terminal window, I do not get any output.
I am sure its a stupid beginners mistake...
#include <iostream>
using namespace std;
int main(){
std::cout << "Hello Moon!";
std::cout.flush();
return 0;
}
compiling:
Kompilierung wird gestartet...
D:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall "D:\CPLUSPLUS\programs\hello world\hello world.cpp" -o "D:\CPLUSPLUS\programs\hello world\hello world.exe"
Die Kompilierung wurde erfolgreich abgeschlossen.
console:
PS D:\CPLUSPLUS\programs\hello world> "hello world.exe"
hello world.exe
PS D:\CPLUSPLUS\programs\hello world>
so obviously it runs the exe without complaint, however I do not see any output...
Any hints/ideas?

Thanks quimby! that did the job!
actually ist not my c++ vscode ignorance but the one of powershell (coming from cmd...)
so you are right: powershell did NOT run my program but rather just echo the quoted string.
so ones needs the & operator to do the job.
Problem solved
Thanks again.

i guess you need to remove the std:: prefix because you already imported the std
try this
#include <iostream>
using namespace std;
int main(){
cout << "Hello Moon!";
return 0;
}

Related

Compiling C++ with VSCode on the Mac

Total novice C++ user going thru tutorials using Visual Studio Code on OSX. Barest bones Hello World program
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
Then I run-compile in VSCodes terminal using
$ g++ Foo.cpp -o foo
My question is, shouldn't I be seeing the Hello, World! out put in the terminal? Or will this only be visible if I compile and run in Windows?
I do see an executable after compiling but nothing in the VS Terminal window.
You actually have to run the file. So, in this case, since you named your file "foo" with the -o command, you have to run ./foo from the terminal.

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

C++ "Hello world" shows no output

I installed Codeblocks on my Windows 10 computer. To check that everything works fine, I first compiled the simple C program
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
That works without problem but when I try the C++ equivalent:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return(0);
}
Then the "command prompt" window opens but no output is shown. I can see in taskmanager that the program is running but as said without any visible output. I also tried running the program directly from the command line but with the same effect. Anyone any ideas?
I found the issue. There was still an older version of MinGW installed in a different folder. I deleted all instances of MinGW, and codeblocks as well. Adter I reinstalled codeblocks everything worked as it should.
This Guy solved similar problem with Codeblocks.
Remove the following Global compiler setting:
-Wl,-subsystem,windows

Run C++ program in Terminal

I'm trying to run this simple C++ code in Sublime Text on Terminal but it's not exactly working...
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'm getting this message instead:
"hello_world2.cpp:1:10: fatal error: 'iostream' file not found"
How can I fix this?
You most probably are missing development headers for your C++ standard library.
You didn't say anything about your environment, but if you were on Windows on Mac you would for sure get these together with your compiler, so let's assume Linux.
You need to install libstdc++-devel package or equivalent (libstdc++-4.8-dev etc.)

C++ Hello World freezes on Windows

The last days I began learning C++. As with every language I began making a Hello World program. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "I'm doomed in Windows :(" << endl;
}
I compiled it with g++ (from MinGW) like this:
g++ -c hello.cpp
g++ -o hello hello.o
The output is:
hello.exe
But when I open hello.exe it freezes / doesn't start. I tried both to open it via the command line as opening it with the GUI. It looks like this:
https://drive.google.com/file/d/0ByNRkSnhavxIaXh6a2JSaEpZV0k/view?usp=sharing
Does anybody know what's wrong? Thanks in advance!
PS: If you wonder why I didnt post the image inline, I can't. I need 10 reputation points for that ^^.
The problem is solved by deactivating Avast, thanks to Hans Passant. It is also possible to add the executable to the Avast exclusion list.