C++ Hello World freezes on Windows - c++

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.

Related

Getting stdout using vscode mingw gcc

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;
}

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.

"undefined reference to `WinMain#16'" Error in gcc editor

I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.

Trying to set up the GNU C++ COMPILER, but I get an error I don't understand when I try to compile hello.cpp

So I bought this book called C++ Programming In Easy Steps by Mike McGrath online.
In the instructions it specifies to create a source file written in C++, the infamous "helo world". So I created my cpp file through sublime text editor and moved it to a file called MyPrograms in my C directory.
The code is as follows:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "hello world"<< endl ;
return 0 ;
}
I have also tried:
#include
using namespace std;
int main()
{
cout << "hello world"<< endl ;
return 0 ;
}
Ok so I saved this file as hello.cpp in C:\MyPrograms.
Then here is where the error occurs....
I open cmd.
I do "c++"
I receive the message "c++: no input files".Which is what I'm supposed to recieve according to the book.
I proceed to do "cd\myprograms" to enter into the MyPrograms directory.
Once in that directory I do "c++ hello.cpp". According to the book this is supposed to compile my source file and create an executable file next to it. Instead I get a long error message that end in collect 2: 1d returned 1 exit status.
When I visit MyPrograms no executable file has been made next to the original cpp file.
I have also tried to do "c++ hello.cpp -o hello.exe" but it gives me the error again. All of this is done on the command prompt.
Please help :(
It looks to me like MinGW isn't installed properly.
First, it looks like you are trying to use version 4.0.3 but it may be conflicting with a version 3.4.5 that you installed previously (one in c:\mingw and the other in e:\p\giaw\src\pkg).
The latest version of MinGW is 4.7.2.1 which you can install from here: http://www.mingw.org/wiki/InstallationHOWTOforMinGW
But it looks like you're just starting out and it may be better to work with something that's better optimized for Windows (unless you're trying to compile Free Software). You can get a copy of Visual Studio Express for free here:
http://www.visualstudio.com/downloads/download-visual-studio-vs#d-express-windows-desktop
There are older versions available as well if you scroll down (VSE 2010).

Compiling simple Hello World program on OS X via command line

I've got a simple hello world example that I'm trying to compile on OS X, named hw.cpp:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'd like to compile it using gcc, but I've had no success. I'd also like to hear the other options, like using Xcode ?
Try
g++ hw.cpp
./a.out
g++ is the C++ compiler frontend to GCC.
gcc is the C compiler frontend to GCC.
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
Though I prefer a slightly more verbose approach:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
g++ hw.cpp -o hw
./hw
user#host> g++ hw.cpp
user#host> ./a.out
Compiling it with gcc requires you to pass a number of command line options. Compile it with g++ instead.
Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).
The new version of this should read like so:
xcrun g++ hw.cpp
./a.out
Use the following for multiple .cpp files
g++ *.cpp
./a.out
You didn't specify what the error you're seeing is.
Is the problem that gcc is giving you an error, or that you can't run gcc at all?
If it's the latter, the most likely explanation is that you didn't check "UNIX Development Support" when you installed the development tools, so the command-line executables aren't installed in your path. Re-install the development tools, and make sure to click "customize" and check that box.