Run C++ program from terminal. Get output in same terminal window - c++

When I run my c++ programs from Terminal (Mac OS X), output from programs is shown in a new Terminal window.
What can I do to prevent the new window, and just have the programs' output straight in the window thats already open?

I believe gcc comes with the XCode tools package.
If you have gcc installed, open terminal window, cd to the directory where you put your cpp file, and type:
g++ myTestFile.cpp -o main; ./main
Replace 'myTestFile' by the name of your file, naturally. you can also rename the 'main' which is just the name of the compiled module, which you need to run by typing ./main to retrieve the output of your code.

I guess you are using the open command.
$ open foo
This will open a new terminal window.
Don't use the open command if you want the program to run in the current terminal window.†
$ foo
† You should obviously leave out the dollar sign.

Related

Script for Notepad++ NppExec for C++ in ubuntu

I just switched to ubuntu and I wanted to setup notepad++ for CPP.
So I used the NppExec plugin to compile within notepad++,
My script was :
npp_save
g++ "$(FULL_CURRENT_PATH)" -o "$(CURRENT_DIRECTORY)\$(NAME_PART)obj"
./"$(NAME_PART)obj"
Here the "obj" I used is to just save the file with an "obj" keyword nothing else.
The last line ./"$(NAME_PART)obj" is to run the program.
But it looks not working in ubuntu, it produces this error:
NPP_SAVE: Z:\home\username\cpp\test.cpp
g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
; about to start a child process: "g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
CreatProcess() failed with error code 2:
File not found.
./"testobj"
; about to start a child process: "./"testobj""
CreatProcess() failed with error code 2:
File not found.
I have investigated some of what I think is the problem, so I think is the usage of / and \ in changing the directory.
I don't know how to fix that, so I can not be sure.
Any ideas? :) I am using vim btw in the same machine and it is working perfectly.
In theory it might be possible (see below), in practice it is rather convoluted and works only for simple compiles (like single file hello world type).
I would suggest you try a linux program, e.g.
an editor like
scite (same editing engine as notepad++) or
kate
or a real IDE like
kdeveloper or
qtcreator.
The problems with Notepad++ inside wine and g++ outside wine (from the linux install ) are this:
notepad++ inside wine under linux is still a windows program
NppExec can only do, what a cmd inside wine can do.
starting g++ directly inside cmd is an error due to g++ being a linux binary and not a windows binary
that is your CreatProcess() failed with error code 2, it means: you are trying to execute a linux program inside wine.
That does not work! (At least not so easy.)
Though you can start linux program inside cmd inside wine using start /unix ...
started this way, g++ wants linux paths and NppExec through its variables will provide only windows paths (whatever wine has set up as drives like Z:\home\username\src\hello.cpp)
though you can convert wine paths to linux paths via the winepath -u command.
g++ started through 'start /unix ... ' inside a cmd inside wine has no proper terminal to report errors to you
though you can start an xterm for g++ and have g++ reports its messages to the xterm
the downside is that g++ will report errors using the linux paths in the xterm, so you cannot double click on an error message an get to the corresponding filename and line.
You get the idea: its complicated not comfortable.
What worked for me for a helloword.cpp was this NppExec script:
NPP_SAVE
npp_run cmd /c start /unix /usr/bin/xterm -e "/usr/bin/winepath -u '$(FULL_CURRENT_PATH)' | xargs g++ -o /tmp/a.out && /tmp/a.out ; echo 'Press return'; read"
The second line
uses an xterm,
let winepath convert the Z:\home\... path to /home/... and
have that send to g++ for compilation using /tmp/a.out as binary
if compile is successfull, /tmp/a.out is executed
the echo and read are for keeping the xterm open so that you can read the output.
If you really want to use Notepad++ inside wine, one option might be using Gnu Make outside of wine and have NppExec run make all or make run similar to the g++ in my script example. That would work for more complicated compiles.

how do u want to open this file in cmd C++

I am still very new to c++ , And i cant seem to compile my very first c++ hello world program after writing my program i go to command prompt and change the directory to desktop because that is where i have saved my program then enter hello.cpp (hello.cpp is the name of my program)and instead of compiling it, It shows me a message saying how do u want to open this file? even thought i already installed mingw ,and changed the environment variables
compile your code to create .exe by below command.
g++ hello.cpp -o hello.exe
MinGW for First Time Users HOWTO

How do I view a long C++ output to console (Only displaying end portion)

I am running codeblocks on Linux Mint 17 and I have not had any issues until now. I am running a relatively long sequence of output and my terminal will only show the last 500 lines of output to Console (terminal). I am needing to view the beginnining portion of the sequence and I would like to know if there are any settings I could change within my OS or codeblocks itself to adjust the amount of output that is printed to console. Perhaps there is a way to save the console output to file (without coding for filestream)? Thank you! I appreciate your help greatly!
EDIT:
I forgot to mention I am using C++ and the console I am using is Linux Mint default terminal.
If you are using the terminal you could just ouput it to a text file like this:
./execution_file > text_file.out
The executable file should be in the same folder as the cpp file, but if you cant find it you could use:
g++ -o codefile.cpp executionfile
You can use less command in linux.
Ex : ./a.out (executable file) | less
In windows you have "more" command.
as like above linux command, you can pipe your output to "more" command and see full result.

How do redirect STDOUT out of XCode compiled Command Line executable?

I have a C++ program that writes to stdout that I am trying to compile into a command line utility in XCode. I need the program compiled into a release build (which it seems, XCode calls an Archive - go figure), not run it as debug.
I basically want to pipe the output of my program into a textfile like this
MyProgram > TextFile.txt
I can compile the program into a command line and run it like this via a Bash shell like this:
Open MyProgram
This spawns a new process (not what I want). However, I could live with that if this worked:
Open MyProgram > TextFile.txt
...But it doesnt :-(. If I try to just run MyProgram directly from Bash, I get the error: -bash: MyProgram: command not found.
What am I doing wrong? How can I compile my command line tool to NOT require the Open command under Mac OSX?
Thanks for any help you can provide. I am picking up C++ on the Mac platform and I am beginning to find it quite it a bit more troublesome than Visual Studio. Does it ever get less painful to work with? :-)
Make your project a Command Line Tool. This will make it so you can run it from the command line directly.
Run it by typing ./MyProgram or ./MyProgram > TextFile.txt not open MyProgram.

C++/G++ hello world application issue, maybe compilation

I am trying to compile my first c++ file on windows with the g++ compiler...
My cpp file is the following -
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
I type in this on command prompt to get to the directory
cd C:\Users\Mark
Then to compile my program I do
g++ hello.cpp
It creates a file name a.exe (default) but when I click on it the command prompt quickly flashes open then it's gone.
What did I do wrong? It should say Hello World! on the prompt and stay there, right?
What you did looks correct, but if you start the program by double clicking it in Windows, the new command prompt will be closed once it is finished. Since you already have a command prompt open for compilation, try to start the program from there, too:
g++ hello.cpp -o hello.exe
hello.exe
That's just normal Windows behavior of executing a command-line file from the UI -- as soon as the program exits (immediately in your case), the prompt window closes.
Running it from the command line will keep it up. To do that, Start > Run > cmd, then cd to the directory, then type a.exe.
Alternatively, you can wait for input from the user to keep the window open. That's fine for testing, but nobody who actually executes programs from the command line would want to have to hit a key to stop a program from running, when it should exit on its own correctly.
you didnt do anything wrong. There isnt a readline or anything at the end of your program, so when it is finishes executing it closes. Add a read character at the end of the program to make it wait for input to terminate.
You did everything right. Whether or not the command prompt stays open has nothing to do with your program. The command prompt is itself a program, and its default behavior is that when it launches in response to the execution of a console application, it closes immediately when the program is finished.
What you should do is launch the command prompt yourself and then run the program from within it, rather than launching the program by double-clicking its icon.
Do not fall into the bad practice of adding a call to some sort of pause function, or waiting for input at the end of the program. You should not get in the habit of hard-coding work-arounds for unwanted behavior of a particular shell into your application. Just use the shell the right way to get the behavior that you want.
The prompt has nothing to do with your program or the language. It has to do with your OS.
Your program should be run from the command line, which will (obviously) leave the command window up when it's finished.
There are tricks to make it stay up if you really want, but these are just tricks, and not necessarily good practice. One would be:
int main()
{
std::cin.get(); // waits for enter
}