I want to write a C++ program in Xcode on my Mac. I wrote this basic one to test it. When I build, I get a "Build Successful" message.
However, I cannot run it! If I hit Command+R nothing happens. When I go to Project->Run the Run option is disabled.
#include <iostream>
using namespace std;
int main()
{
cout << "helo" << endl;
}
Launch XCode
In the "Choose template" box, pick Mac OS X, then Command Line Tool. Press Next
Give your project a name, select C++ as the type
You should see a new project with main.cpp
press the Run button
At the bottom of the screen, under All Output you should see:
Hello, World!
Program ended with exit code: 0
Related
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();
}
I'm attempting to debug a simple application in CDT using the MinGW, i.e. Hello World.
It builds and runs correctly;
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl;
return 0;
}
With the following options:
However, when I run the debugger, it simply jumps over any breakpoints with no output, errors or stopping whatsoever (making me think that it hasn't actually run at all) straight to: terminated
Any ideas whats causing this and how I can go about fixing it?
Thanks very much!
David
UPDATES
I don't think it can be an error with GDB? As when when run from the cmd line it appears to work correctly.
i am new to qt. Trying to run some simple application in QtCreator 3.2.1 which is based on QT5 on Debian system.
i create c++ plain project with cmake
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "please enter your name:\n";
cin >> name;
cout << "hello" << name << "\n";
return 0;
}
The problem is that when i launch the application, and input some string. The application console dont have any resp.
I confirm that the application can be run in my gnome-terminal.
After that, i check the same message from the stackoverflow.
there is a solution "set project run in terminal".
Then i look for that on the QtCreator 3.2.1. I cant find that menu. Only have the terminal setting.
unfortunately, whichever i choose,
1. /usr/bin/xterm -e
2. /usr/bin/gnome-terminal -x
3. /usr/bin/x-terminal-emulator -e
the problem still exist.
you must choose the "Run in Terminal" if you create CMake project with C++
I was just trying to use Xcode for a very small C++ project, and wanted to see
some prints in the console, the thing is I did not see anything.
I tried to run a very simple code instead:
#include <iostream>
int main (int argc, char * const argv[]) {
std::cout << "Hello, World!\n";
printf("here");
return 0;
}
but still, nothing in Xcode console.
any idea why?
EDIT:
adding snapshot of the program:
EDIT 2:
found this,
and it's working:
How do I run a C++ program in Xcode 4?
That should work fine. Are you sure that you had the console displayed? Try command-shift-C or choose View->Debug Area->Activate Console.
If that doesn't help, try running your program from a Terminal window. Does the program display the expected output?
It sounds like when you created a new project (File > New > Project... ), you selected "C/C++ Library". Since libraries don't output to the console directly, that explains why Run was greyed out for you and running it doesn't output to the console.
Instead, you need to create a new project and select "Command Line Tool" template in the Application section, and build your program from there.
Your image doesn't show that you ran the program, only that you built it. Look at the Log Navigator (the last one, ⌘7) and see if there are any logs for 'Debug one' after 'Build one'. To run the program use Product > Run or ⌘R.
Try pressing Shift+Command+R. That should compile your program and open it in a terminal window.
maybe you need to add "\n" after "here"
I don't know why but it works for me.
Hope someone can explain it for me.
I've been trying to use Eclipse CDT to do some c++ examples, i can run them just fine with the run command, but whenever i try to Debug, the console window freezes up, I'm able to input, but the program doesn't continue.
When I debug, i get the following output on the console window (no breakpoints, but breaks on main because of default settings):
Hello, world
put your name: 15^running
The continue button is disabled and doesn't do anything when I input something and hit enter. The 15 is a random number, sometimes its 16, 20 etc.
If I run the program under eclipse I get the input prompt just fine:
Hello, world
put your name: test
Hello test
this is the code I try to debug:
#include <iostream>
#include <string>
int main() {
std::cout << "Hello, world" << std::endl;
std::string name;
std::cout << "put your name: ";
std::cin >> name;
std::cout << "Hello " + name << std::endl;
return 0;
}
My path var:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Java\jdk1.6.0_14\bin;C:\MinGW\bin
Eclipse version: Helios Service Release 2
CDT version: 7.0.2
OS: windows xp
GDB version: GNU gdb (GDB) 7.2
How can I debug this small example under CDT, without issues?
15^running looks as a result record from gdb's Machine Interface. Normally it shouldn't appear in the Eclipse console.
I recommend trying a different Create Process Launcher. It can be changed in the following way:
In Main menu choose "Run" -> "Debug Configurations...".
In the opened "Debug Configurations" window shown below click "Select other..." opposite "Using GDB (DSF) Create Process Launcher".
In the opened "Select Preferred Launcher" window shown below check "Use configuration specific settings", select "Standard Create Process Launcher" in the list below and click OK.
Now go to the Debugger tab in the "Debug Configurations" window, select debugger, e.g. "MinGW gdb" and click Apply.
With the Standard Create Process Launcher I am able to debug your program although "put your name:" is printed only after I type something and hit Enter, because the output stream is not flushed.
Try having a look at this http://www.cprogramming.com/gdbtutorial.html and see if that helps. Like can you press CTRL-C to break?
Also Cannot enter input with gdb. Help! might be a pointer although related to apple.
the following is from the Eclipse website's FAQ:
http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows
Eclipse console does not show output on Windows In Eclipse CDT on
Windows, standard output of the program being run or debugged is fully
buffered, because it is not connected to a Windwos console, but to a
pipe. See bug 173732 for more details. Either add fflush calls after
every printf or add the following lines in the start of the main
function:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Seems like an expected bug on windows.