如何让项目通过 Xcode 在终端中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9028639/
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
How would I get a project to run in Terminal through Xcode?
提问by Austin Moore
Is it possible for me to hit run in Xcode and have my project be compiled with the g++ compiler then open a Terminal window and run it?
我是否可以在 Xcode 中运行并使用 g++ 编译器编译我的项目,然后打开一个终端窗口并运行它?
So pretty much I want Xcode to run these commands when I hit run:
所以我几乎希望 Xcode 在我点击运行时运行这些命令:
g++ [source]
./a.out
And at some point a Terminal window will open with the program running.
在某个时候,终端窗口将随着程序的运行打开。
How could I do this (if it's possible)?
我怎么能做到这一点(如果可能的话)?
采纳答案by Austin Moore
I had to settle with running my program in a Terminal window that I kept open while coding in Xcode. I had Xcode compile the program to an 'a.out' file whenever I built the program in Xcode. I did this by running a 'Run Script'. Here's how I did it:
我不得不在终端窗口中运行我的程序,我在 Xcode 中编码时保持打开该窗口。每当我在 Xcode 中构建程序时,我都会让 Xcode 将程序编译为“a.out”文件。我通过运行“运行脚本”来做到这一点。这是我如何做到的:
- Go to the screen where you can edit the build settings.
- Under 'TARGETS' in the side menu, click on your project
- Go to the 'Build Phases' tab and click the 'Add Build Phase' button
- From the list that drops down select 'Add Run Script'
Then input what you would like Xcode to do when building the program in the box under the shell command box. My commands were like this:
cd [path to program]
g++ [program]
- 转到您可以编辑构建设置的屏幕。
- 在侧边菜单中的“目标”下,单击您的项目
- 转到“构建阶段”选项卡,然后单击“添加构建阶段”按钮
- 从下拉列表中选择“添加运行脚本”
然后在shell命令框下的框中输入您希望Xcode在构建程序时执行的操作。我的命令是这样的:
cd [path to program]
g++ [program]
(I can't get the block code formatting to work here).
(我无法在此处使用块代码格式)。
Now all I have to do is keep a Terminal window open in the directory of the program. I run a.out
whenever I need to run the program in Terminal. Not entirely automated, but there's only one extra step than I had hoped for, which isn't too bad.
现在我所要做的就是在程序目录中打开一个终端窗口。我跑a.out
每当我需要运行在终端程序。并非完全自动化,但只有比我希望的多一个步骤,这还不错。
回答by Daniel
You can have it compile with g++ by changing the build settings (Someone else knows the specifics, i'm sure). Also, you can look at the scheme to find out where it's building the executable, from where you can use the terminal. However, I don't know why you'ed do that.. Is the debugger and log not enough?
您可以通过更改构建设置来使用 g++ 进行编译(我敢肯定,其他人知道具体细节)。此外,您可以查看方案以找出它在哪里构建可执行文件,从哪里可以使用终端。但是,我不知道您为什么要这样做.. 调试器和日志还不够吗?
回答by Jeshua Lacock
Actually, that is what XCode does when you hit run.
实际上,这就是当你点击运行时 XCode 所做的。
That "terminal" window is what you see in the "All output" window (was called console before XCode 4).
“终端”窗口就是您在“所有输出”窗口(在 XCode 4 之前称为控制台)中看到的内容。
You can enter input in that window the same as you can a terminal window....
您可以在该窗口中输入与终端窗口相同的输入......
EDIT:
编辑:
You can see the same output from your app in an actual terminal. When you run the app you will get some output like this in the "All output" window:
您可以在实际终端中看到应用程序的相同输出。当您运行该应用程序时,您将在“所有输出”窗口中获得如下输出:
GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul 1 10:44:54 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys010
[Switching to process 22261 thread 0x0]
Note the tty, in this example it is /dev/ttys010
注意 tty,在这个例子中它是 /dev/ttys010
Now, open a terminal window and cat the tty:
现在,打开一个终端窗口并输入 tty:
cat /dev/ttys010
You will now see output from your program in that terminal window. Hope this helps!
您现在将在该终端窗口中看到程序的输出。希望这可以帮助!