This question already has answers here:
Why do I get no output for very simple Hello World program?
(7 answers)
Closed 6 months ago.
I am new to C++ and coded my first main.cpp but I got an error, not exactly an error, it is a logical error, I guess because I wrote the following code:
#include <iostream> // including the iostream
using namespace std; // using the std namespace
int main() { // starting the main function
cout << "Hello World!" << endl; // My favorite line of code in all the of languages
return 0; // returning 0 to stop the function execution
}
In the terminal I expected
C:\Users\DELL\Desktop> g++ main.cpp
Hello World!
C:\Users\DELL\Desktop>
But it is just showing:
C:\Users\DELL\Desktop> g++ main.cpp
C:\Users\DELL\Desktop>
No output is coming, but when I try in Code::Blocks, it is working just fine! In vscode terminal, the same problem but when I run the file, it is working again! Why is it not working? I tried it in Command Prompt, installed the Windows Terminal, and tried it in that also (keeping the terminal as Command Prompt, cause I don't know what PowerShell is and how to use it) but any method is not working.
Please tell me what to do, I am new to c++ and know only some things amount it.
The command g++ main.cpp creates the file a.out or a.exe. After that file is created you need to run it by the command ./a.out on Linux and Mac or a.exe on Windows.
It's pretty simple, after g++ "file.cpp", you get an output file in your current working directory, which is the executable. C++ is compiled, not interpreted.
That output file is the executable, which by default will be "a.out", with gcc/g++. You can use the option "-o" to specify the output directory.
g++ main.cpp will compile the file and produce a.exe, you just need to type a.exe in the terminal and it will print Hello World!.
Related
I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++
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.
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
I’m trying to run my c++ files off the prompt, but nothing is showing, e.g.
C:\C++\mingw>g++ hello.cpp -o hello.exe
It seems to be bug free but nothing displays, in this case a simple hello to the terminal.
My code is a straightforward
#include <iostream>
using namespace std;
int main()
{
cout << "Hello \n" << endl;
return 0;
}
Of course, the simplest answer "Just run hello.exe" is correct. Here's some additional logic behind:
If you're used to interpreted languages, such as Python or Lua, you might have noticed that you execute them by supplying source file to the executable, as such:
python my_source.py
However, this works because each time you run python command, it reads the source file given, and then interprets it and executes appropriate machine instructions based on the file contents - it interprets the file.
C++, on the other hand, is a compiled language. The execution of g++, which is a compiler, generates said machine code for your platform, and stops there. Next time you don't need the compiler to run your program; every instruction is encoded as the machine code in the .exe file. That's why you can share your .exe file with your friend if he doesn't have a compiler, but he won't be able to execute python script without python environment installed.
g++ hello.cpp -o hello.exe // This command only produce the exe file
The executive file doesn't run automatically. You should run it by yourself.
hello.exe
hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. i compiled with following command
g++ 1.c -o abc
to run program i use ./abc
now my problem is that i write a another code in c++ like
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
SaveFile <<"<configuration>";
SaveFile<<endl;
SaveFile<<"<property>";
SaveFile<<endl;
savefile.close();
return 0;
}
now i want to run abc in this code how to do this ?
how to use or run abc in this file?
how to use ./abc in this program ?
Actually, your question title ("... using system ...") says it all. Use:
system ("./abc");
to run the ./abc program.
There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable.
A full sample program, testprog.cpp, to show what I mean:
#include <cstdlib>
int main (void) {
std::system ("ls -ald m*");
return 0;
}
Compiling this with:
g++ -Wall -Wextra -pedantic -o testprog testprog.cpp
and running the resultant executable testprog, this outputs (on my Ubuntu 10.04 box):
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder
In other words, it runs the ls -ald m* command from within the program itself.