如何使用终端在Mac OS X上运行C程序?

时间:2022-09-08 20:25:06

I am new to C. Here is my "Hello,world!" program.

这是我的“你好,世界!”节目。

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

为什么?

7 个解决方案

#1


67  

First save your program as program.c.

首先将程序保存为program.c。

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?做一个“焦点搜索”输入⌘空间、开始键入应用程序商店和回车时正确的猜测。

App Store looks like this:

App Store看起来是这样的:

如何使用终端在Mac OS X上运行C程序?

Xcode looks like this on App Store:

Xcode在App Store上是这样的:

如何使用终端在Mac OS X上运行C程序?

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

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来编译您的代码,而不需要启动名为Xcode的大型、丑陋的软件开发GUI:

gcc -Wall -o program program.c

Note: On newer versions of OS X, you would use clang instead 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

#2


18  

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.c and 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!

你应该去看《你好世界》!

#3


4  

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 compiler for 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.)

哦,你最后一个问题的答案(“为什么?”)的文件你想执行没有执行权限设置(通常一个编译器会自动的二进制,推断出你正试图运行脚本的源代码,因此暗示编译)。

#4


0  

To do this:

要做到这一点:

  1. open terminal

    打开终端

  2. type in the terminal: nano ; which is a text editor available for the terminal. when you do this. something like this would appear.

    终端类型:nano;它是终端可用的文本编辑器。当你这样做。像这样的东西会出现。

  3. here you can type in your C program

    这里你可以输入你的C程序

  4. type in control(^) + x -> which means to exit.

    输入控制(^)+ x - >这意味着退出。

  5. save the file by typing in y to save the file

    输入y保存文件

  6. write the file name; e.g. helloStack.c (don't forget to add .c)

    写文件的名字;例如helloStack。c(别忘了加上。c)

  7. when this appears, type in gcc helloStack.c

    当出现这种情况时,输入gcc helloStack.c。

  8. then ./a.out: this should give you your result!!
  9. 然后,/。出局:这应该给你的结果!!

#5


0  

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程序位置,然后键入下面的命令,使文件名,然后键入。/文件名

#6


-2  

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

e。g-如果你已经在文件文件夹中保存了一个新的文件夹程序

   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

现在编译你的程序

   make Hello
   ./hello

#7


-6  

You need to set executable permission to the file (Untitled1).

您需要设置文件的可执行权限(Untitled1)。

chmod a+x Untitiled1

#1


67  

First save your program as program.c.

首先将程序保存为program.c。

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?做一个“焦点搜索”输入⌘空间、开始键入应用程序商店和回车时正确的猜测。

App Store looks like this:

App Store看起来是这样的:

如何使用终端在Mac OS X上运行C程序?

Xcode looks like this on App Store:

Xcode在App Store上是这样的:

如何使用终端在Mac OS X上运行C程序?

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

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来编译您的代码,而不需要启动名为Xcode的大型、丑陋的软件开发GUI:

gcc -Wall -o program program.c

Note: On newer versions of OS X, you would use clang instead 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

#2


18  

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.c and 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!

你应该去看《你好世界》!

#3


4  

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 compiler for 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.)

哦,你最后一个问题的答案(“为什么?”)的文件你想执行没有执行权限设置(通常一个编译器会自动的二进制,推断出你正试图运行脚本的源代码,因此暗示编译)。

#4


0  

To do this:

要做到这一点:

  1. open terminal

    打开终端

  2. type in the terminal: nano ; which is a text editor available for the terminal. when you do this. something like this would appear.

    终端类型:nano;它是终端可用的文本编辑器。当你这样做。像这样的东西会出现。

  3. here you can type in your C program

    这里你可以输入你的C程序

  4. type in control(^) + x -> which means to exit.

    输入控制(^)+ x - >这意味着退出。

  5. save the file by typing in y to save the file

    输入y保存文件

  6. write the file name; e.g. helloStack.c (don't forget to add .c)

    写文件的名字;例如helloStack。c(别忘了加上。c)

  7. when this appears, type in gcc helloStack.c

    当出现这种情况时,输入gcc helloStack.c。

  8. then ./a.out: this should give you your result!!
  9. 然后,/。出局:这应该给你的结果!!

#5


0  

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程序位置,然后键入下面的命令,使文件名,然后键入。/文件名

#6


-2  

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

e。g-如果你已经在文件文件夹中保存了一个新的文件夹程序

   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

现在编译你的程序

   make Hello
   ./hello

#7


-6  

You need to set executable permission to the file (Untitled1).

您需要设置文件的可执行权限(Untitled1)。

chmod a+x Untitiled1