C语言 如何使用终端在 Mac OS X 上运行 C 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32337643/
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 to run C program on Mac OS X using Terminal?
提问by Bolat Tleubayev
I am new to C. Here is my "Hello,world!" program.
我是 C 的新手。这是我的“你好,世界!” 程序。
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
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$
Why?
为什么?
回答by Mark Setchell
First save your program as program.c.
首先将您的程序另存为program.c.
Now you need the compiler, so you need to go to App Storeand install Xcodewhich is Apple's compiler and development tools. How to find App Store? Do a "Spotlight Search"by typing ⌘Spaceand start typing App Storeand hit Enterwhen it guesses correctly.
现在你需要编译器,所以你需要去App Store安装Xcode,它是 Apple 的编译器和开发工具。如何找到应用商店?通过键入并开始键入并在正确猜测时点击来执行“Spotlight Search”。⌘SpaceApp StoreEnter
App Storelooks like this:
应用商店看起来像这样:
Xcodelooks like this on App Store:
Xcode在App Store上看起来像这样:
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 ⌘Spaceand start typing Terminaland hit Enterwhen it guesses Terminal.
然后您需要在Terminal 中安装命令行工具。如何启动终端?您需要再进行一次“Spotlight Search”,这意味着您输入⌘Space并开始输入Terminal并Enter在它猜测时点击Terminal。
Now install the command-line tools like this:
现在安装命令行工具,如下所示:
xcode-select --install
Then you can compile your code with by simply running gccas in the next line without having to fire up the big, ugly software development GUI called Xcode:
然后,您可以通过简单地gcc在下一行中运行来编译您的代码,而无需启动名为 的又大又丑的软件开发 GUI Xcode:
gcc -Wall -o program program.c
Note: On newer versions of OS X, you would use clanginstead of gcc, like this:
注意:在较新版本的 OS X 上,您将使用clang代替gcc,如下所示:
clang program.c -o program
Then you can run it with:
然后你可以运行它:
./program
Hello, world!
If your program is C++, you'll probably want to use one of these commands:
如果您的程序是 C++,您可能需要使用以下命令之一:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
回答by Eduard Grigorescu
First make sure you correct your program:
首先确保你更正了你的程序:
#include <stdio.h>
int main(void) {
printf("Hello, world!\n"); //printf instead of pintf
return 0;
}
Save the file as HelloWorld.cand type in the terminal:
将文件另存为HelloWorld.c并在终端中键入:
gcc -o HelloWorld HelloWorld.c
Afterwards just run the executable like this:
然后像这样运行可执行文件:
./HelloWorld
You should be seeing Hello World!
你应该看到 Hello World!
回答by Pieter Bruegel the Elder
A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal. You need a compilerfor that.
不应运行“C 程序”。它旨在编译成一个“可执行”程序,然后可以从您的终端运行。你需要一个编译器。
Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)
哦,您最后一个问题(“为什么?”)的答案是您尝试执行的文件没有设置可执行权限(编译器通常会自动对二进制文件进行设置,这让我们推断您正在尝试将源代码作为脚本运行,因此提示编译。)
回答by Antonio Cachuan
Working in 2019By default, you can compile your name.c using the terminal
在 2019 年工作默认情况下,您可以使用终端编译您的 name.c
cc name.c
and if you need to run just write
如果你需要运行就写
./name.out
回答by Ambati_prasanna_akhil
to compile c-program in macos simply follow below steps
在 macos 中编译 c 程序只需按照以下步骤
using cd command in terminal go to your c-program location
then
type the command present below
make filename
then type
./filename
在终端中使用 cd 命令转到您的 c 程序位置,
然后键入下面的命令
make filename
然后键入
./filename
回答by Simileoluwa Aluko
To do this:
去做这个:
open terminal
type in the terminal:
nano; which is a text editor available for the terminal. when you do this. something like this would appear.here you can type in your
Cprogramtype in
control(^) + x-> which means to exit.save the file by typing in
yto save the filewrite the file name; e.g.
helloStack.c(don't forget to add .c)when this appears, type in
gcc helloStack.c- then
./a.out: this should give you your result!!
打开终端
在终端输入:
nano; 这是一个可用于终端的文本编辑器。当你这样做时。这样的东西会出现。在这里你可以输入你的
C程序输入
control(^) + x-> 表示退出。通过键入
y保存文件来保存文件写入文件名;例如
helloStack.c(不要忘记添加.c)当出现这个时,输入
gcc helloStack.c- 那么
./a.out:这应该给你你的结果!!
回答by user11952272
For compiling a c program on your latest macOS just type the following in terminal after saving the file with a .c extension and on reaching the path where the file is saved :
要在最新的 macOS 上编译 ac 程序,只需在使用 .c 扩展名保存文件并到达保存文件的路径后,在终端中键入以下内容:
cc yourfilename.c
抄送你的文件名.c
Once you have checked all the errors after compilation (if any), type the following for executing the code :
在编译后检查所有错误(如果有)后,键入以下代码以执行代码:
./a.out
./a.out
These commands are tested on macOS Mohave and are working perfectly fine, cheers coding!
这些命令在 macOS Mohave 上进行了测试,并且运行良好,干杯编码!
回答by ashish8882829
1) First you need to install a GCC Compiler for mac (Google it and install it from the net )
1)首先你需要为mac安装一个GCC编译器(谷歌它并从网上安装它)
2) Remember the path where you are storing the C file
2)记住你存放C文件的路径
3) Go to Terminal and set the path
3)转到终端并设置路径
e.g- if you have saved in a new folder ProgramC in Document folder
例如-如果您已保存在 Document 文件夹中的新文件夹 ProgramC 中
then type this in Terminal
cd Document
cd ProgramC
4) Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)
4)现在你可以看到你在保存C程序的文件夹中(让你将程序保存为Hello.c)
5) Now Compile your program
5) 现在编译你的程序
make Hello
./hello


