macos 从终端运行 C++ 程序。在同一个终端窗口中获取输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10354085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run C++ program from terminal. Get output in same terminal window
提问by user1361521
When I run my c++ programs from Terminal (Mac OS X), output from programs is shown in a new Terminal window.
当我从终端 (Mac OS X) 运行我的 c++ 程序时,程序的输出显示在新的终端窗口中。
What can I do to prevent the new window, and just have the programs' output straight in the window thats already open?
我该怎么做才能防止出现新窗口,而只是让程序的输出直接出现在已经打开的窗口中?
回答by gregtronel
I believe gcc comes with the XCode tools package.
我相信 gcc 带有 XCode 工具包。
If you have gcc installed, open terminal window, cd to the directory where you put your cpp file, and type:
如果你安装了 gcc,打开终端窗口,cd 到你放置 cpp 文件的目录,然后输入:
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.
自然地,将“myTestFile”替换为您的文件名。您还可以重命名'main',它只是编译模块的名称,您需要通过键入 ./main 来运行它以检索代码的输出。
回答by user1361521
I guess you are using the open
command.
我猜你正在使用open
命令。
$ 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.?
open
如果您希望程序在当前终端窗口中运行,请不要使用该命令。?
$ foo
? You should obviously leave out the dollar sign.
? 您显然应该省略美元符号。