Can't execute compiled C++ exe file - c++

I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.
EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.

Your program probably is working but the console window is closing before you can see anything.
Try adding an input at the end of the program so it will wait.
I.E.
int a;
cin >> a;

Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.

Related

Exe file not opening

I am trying to learn C++ now and created a Hello World program. When I compile it on Linux using g++ and it works perfectly fine. When I compile it on Windows using the Build tools, it still compiles the code into machine code, but I can't open the executable. I used the Microsoft build tools as a compiler. The code was:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
}
The output should be: Hello, World!
**Question already answered:
The program closes because it is not run in cmd. To prevent the program from crashing add
```system("pause");```
at the end**
The executable is showing the correct output on the terminal, but that terminal closes that fast that you don't even realise it.
I'd advise you to open a command prompt, go to the directory where the executable is located and launch it over there. You'll see the desired output.
it possibility because
the app closes immediately after ouputing
add system("pause");
on the end
Its not that you can't open that exe file.
Once you click on that file, it opens up and does its work and closes.
To prevent your exe file from getting closed after doing its operation, you can add following line at the end of the main function :
getchar();
And now once you open your exe file, it wont close automatically, you will need to press "enter" to close it.
#include<iostream>
using namespace std;
int main(){
cout<<"hello world";
getchar();
}

C++ run error when using endl

I've recently started to learn C++ and I'm trying to compile and run a very simple program.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "Hello Again!";
return 0;
}
The program itself compiles as it should without any errors, however, when the program runs, it seems to stop after cout << "Hello World!" << endl;. I find this very strange, as my friend is sitting right beside me, doing the exact same thing and it works for him. The same thing happens when I try to use the sizeof();; it does not return any value, however, when my friend does this, it works.
When I ran it in NetBeans, it first generated the error
RUN FAILED (exit value 255, total time: 2s)
And another time I ran it, it generated the same error, but with a different exit value. Although it is now back to 255.
When running debugger in NetBeans it produces
SIGILL (Illegal instruction)
a few times before it stops working.
I have installed the MinGW compiler at the default directory (C:\MinGW), and this is the compiler that NetBeans and any other program is using. I have also added the path to the System Environment Variables at the end of the "Path" variable:
;C:\MinGW\bin;C:\MinGW\msys\1.0\bin
Trying to run and compile the same code in Atom results in
Hello World!Press any key to continue . . .
I have tried reinstalling the compiler, and restarted my computer. None of which seems to work. I've also tried \n, which works.
My question is, is there anything wrong with my compiler or computer, or am I missing something obvious? And is it possible to fix this?
(Sorry if this is a duplicate, I've searched for a few hours, not able to find anything useful)
I found the culprit!
In my System Environment Variables, C:\MingGW\bin and C:\MingGW\MSYS\1.0\bin was at the bottom of the list. This meant that it was below C:\Program Files (x86)\GNU\GnuPG\pub (Which I think, if I'm not mistaken is another compiler).
Although all paths in NetBeans were correct, it seems like the system didn't like it when another compiler was listed above MinGW.
I solved the problem by moving the paths for MinGW up, above the GNU.

C++ compiling and running in Sublime Text 2

I really like Sublime Text 2 for HTML/CSS and Python and I'm starting to learn C++ and I want to use Sublime Text 2. I have looked at a few tutorials on installing g++ in order to run C++ in Sublime Text 2.
This is my code:
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
And when I run it it says [Finished in 1.5s] but nothing got printed. I have added the environment variables path but nothing gets printed.
The problem is that you are pressing build, which compiles your source code into an executable but does not run it. The [Finished in ...s] you are seeing is how long the program took to compile.
What you need to do is build like you currently are, but then go to the directory where your source code is and run the executable file that's in there*. There is a run option within the editor, but it doesn't always work on Windows**.
*if the program closes instantly, try running it from the console or adding std::cin.get() to the end of your program
**often due to incorrect configuration

Hello World C++

Every time I try and run the Simple Hello World program in C++, I build it and it says nothing to build for FirstProject. The code looks correct, I'm using MinGW as my compiler, etc. Every time I try to run the program, instead of printing the output, it just terminates. Anyone have a clue?
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
try these commands:
g++ -o hello_world.exe hello_world.cpp
./hello_world.exe
MinGW works same as gcc on linux so all commands which work on linux should work on MinGW
In cygwin, the following steps should work.
Save the contents to file HelloWorld.cc.
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe
If that doesn't work, something is really not right.
Are you running it from the command line or an IDE? Sometimes an IDE will open a terminal and close it too fast for you to see, or you'll just see a flash. Try running it from the command line so you'll be able to see if it printed anything before exiting the program.
Tried and tested using MinGW in windows 10 command prompt.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World!"<<endl;
system("pause");
return 0;
}
Image of command prompt output
Save the contents to file HelloWorld.cpp
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe

ofstream outputs in debug but not run

I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.