如何使用终端在 Mac OS X 上运行 C 程序? [英] How to run C program on Mac OS X using Terminal?
问题描述
我是 C 的新手.这是我的你好,世界!"程序.
I am new to C. Here is my "Hello,world!" program.
#include <stdio.h>
int main(void)
{
printf("Hello, world!
");
return 0;
}
在我尝试使用终端运行它后,它说:
After I try to run it using Terminal it says:
MacBook-Pro-MacBook:~ macbook$ /Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied
MacBook-Pro-MacBook:~ macbook$
为什么?
推荐答案
首先将您的程序另存为 program.c
.
First save your program as program.c
.
现在你需要编译器,所以你需要去App Store并安装Xcode,这是Apple的编译器和开发工具.如何找到App Store?输入 ⌘Space 并开始输入 App Store
并按 Enter<进行 Spotlight Search"/kbd> 当它猜对了.
Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How to find App Store? Do a "Spotlight Search" by typing ⌘Space and start typing App Store
and hit Enter when it guesses correctly.
App Store 看起来像这样:
Xcode 在 App Store 上看起来像这样:
Xcode looks like this on App Store:
然后您需要在终端中安装命令行工具.如何启动终端?你需要再做一次 Spotlight Search",这意味着你输入 ⌘Space 并开始输入 Terminal
并点击当它猜到Terminal
时输入.
Then you need to install the command-line tools in Terminal. How to start Terminal? You need to do another "Spotlight Search", which means you type ⌘Space and start typing Terminal
and hit Enter when it guesses Terminal
.
现在像这样安装命令行工具:
Now install the command-line tools like this:
xcode-select --install
然后您可以通过简单地运行 gcc
来编译您的代码,就像在下一行一样,而无需启动名为 Xcode
的又大又丑的软件开发 GUI:>
Then you can compile your code with by simply running gcc
as in the next line without having to fire up the big, ugly software development GUI called Xcode
:
gcc -Wall -o program program.c
注意:在较新版本的 OS X 上,您将使用 clang
而不是 gcc
,如下所示:
Note: On newer versions of OS X, you would use clang
instead of gcc
, like this:
clang program.c -o program
然后你可以运行它:
./program
Hello, world!
<小时>
如果您的程序是 C++,您可能需要使用以下命令之一:
If your program is C++, you'll probably want to use one of these commands:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
这篇关于如何使用终端在 Mac OS X 上运行 C 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!