如何通过 Xcode 在终端中运行项目?

发布于 2024-12-29 15:40:49 字数 201 浏览 2 评论 0原文

我是否可以在 Xcode 中点击运行并使用 g++ 编译器编译我的项目,然后打开终端窗口并运行它?

所以当我点击运行时,我非常希望 Xcode 运行这些命令:

g++ [source]
./a.out

并且在某个时刻,终端窗口将打开,程序正在运行。

我怎样才能做到这一点(如果可能的话)?

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?

So pretty much I want Xcode to run these commands when I hit run:

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少年亿悲伤 2025-01-05 15:40:49

我不得不在 Xcode 中编码时保持打开的终端窗口中运行我的程序。每当我在 Xcode 中构建程序时,我都会让 Xcode 将程序编译为“a.out”文件。我通过运行“运行脚本”来做到这一点。我是这样做的:

  • 转到可以编辑构建设置的屏幕。
  • 在侧面菜单的“目标”下,单击您的项目
  • 转到“构建阶段”选项卡,然后单击“添加构建阶段”按钮
  • 从下拉列表中选择“添加运行脚本”
  • 然后输入您想要的内容就像 Xcode 在 shell 命令框下的框中构建程序时所做的那样。我的命令是这样的:

    cd [程序路径]

    g++ [program]

(我无法让块代码格式在这里工作)。

现在我所要做的就是在程序目录中保持终端窗口打开。每当我需要在终端中运行程序时,我都会运行 a.out 。并非完全自动化,但只比我希望的多了一步,这还不错。

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:

  • 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]

(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.

一口甜 2025-01-05 15:40:49

您可以通过更改构建设置来使用 g++ 进行编译(我确信其他人知道具体细节)。另外,您可以查看该方案以了解它在何处构建可执行文件,以及您可以从何处使用终端。但是,我不知道你为什么要这样做。调试器和日志还不够吗?

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?

初相遇 2025-01-05 15:40:49

实际上,这就是当你点击运行时 XCode 所做的事情。

该“终端”窗口就是您在“所有输出”窗口中看到的窗口(在 XCode 4 之前称为控制台)。

您可以在该窗口中输入输入,就像在终端窗口中输入一样......

编辑:

您可以在实际终端中看到应用程序的相同输出。当您运行该应用程序时,您将在“所有输出”窗口中获得如下输出:

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]

注意 tty,在本例中为 /dev/ttys010

现在,打开一个终端窗口并捕获 tty:

cat /dev/ttys010

您现在将看到来自的输出您的程序在该终端窗口中。

Actually, that is what XCode does when you hit run.

That "terminal" window is what you see in the "All output" window (was called console before 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

Now, open a terminal window and cat the tty:

cat /dev/ttys010

You will now see output from your program in that terminal window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文