C语言 如何在 Mac OS X 上编译和运行 C 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13887947/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:44:49  来源:igfitidea点击:

How to compile and run C program on Mac OS X

c

提问by JT9

I am learning C for a class at my university and wish to write the program using the TextWrangler text editor on my Mac (running OSX Lion 10.7). Once I write the .c file, I compile it using gcc. I downloaded the compiler from Apple Developer Tools.

我正在我的大学学习 C 语言,并希望在我的 Mac(运行 OSX Lion 10.7)上使用 TextWrangler 文本编辑器编写程序。一旦我编写了 .c 文件,我就使用 gcc 编译它。我从 Apple Developer Tools 下载了编译器。

It is included in a command line tools download. I locate the file using Terminal, compile it using gcc filename.c where the a.out executable file is created. However, when I type "a.out" or "/a.out" I get the following messages: "-bash: a.out: command not found" or "-bash: /a.out: No such file or directory". I have successfully compiled and ran C programs on Linux systems before using this same method. What am I doing wrong on my Mac?

它包含在命令行工具下载中。我使用终端找到该文件,使用 gcc filename.c 编译它,其中创建了 a.out 可执行文件。但是,当我键入“a.out”或“/a.out”时,我收到以下消息:“-bash: a.out: command not found”或“-bash: /a.out: No such file or directory” ”。在使用相同的方法之前,我已经在 Linux 系统上成功编译并运行了 C 程序。我在 Mac 上做错了什么?

回答by MByD

You need to add a dot to indicate that the executable is in the current directory, as the current directory is not in the path:

您需要添加一个点来表示可执行文件在当前目录中,因为当前目录不在路径中:

./a.out

回答by beckah

You're just missing one thing! Instead of "/a.out" it should be "./a.out". Another useful thing is changing the output so that you can have multiple compiled programs. Simply, the only thing you need to put in the terminal is the following (fill in the blanks with what you would like)

你只是错过了一件事!而不是“/a.out”,它应该是“./a.out”。另一个有用的事情是更改输出,以便您可以拥有多个已编译的程序。简单地说,您需要在终端中输入的唯一内容是以下内容(用您想要的内容填写空格)

gcc nameofprogramyouwrote.c -o whatyouwanttheprogramtobenamed

good luck!

祝你好运!

回答by DrummerB

You have to add a dot in front of the slash:

您必须在斜线前面添加一个点:

./a.out

/a.outwould try to execute a program in the root folder (/).
a.outwill look for your program in all the folders defined in the PATH environment variable.

/a.out将尝试在根文件夹 ( /) 中执行程序。
a.out将在 PATH 环境变量中定义的所有文件夹中查找您的程序。

I have successfully compiled and ran C programs on Linux systems before using this same method. What am I doing wrong on my Mac?

在使用相同的方法之前,我已经在 Linux 系统上成功编译并运行了 C 程序。我在 Mac 上做错了什么?

You have to do the same on Linux.

你必须在 Linux 上做同样的事情。

回答by MichaelM

make sure to set your permissions executable with chmod +x a.out

确保使用 chmod +x a.out 设置您的权限可执行文件

回答by aFactoria

To build:

构建:

    gcc -Wall -o yourFile yourFile.c

Then to execute:

然后执行:

    ./yourFile

回答by Kent Aguilar

You'll need a compiler. The easiest way however is to install XCode. This way you'll get access to "gcc", Thus,

你需要一个编译器。然而,最简单的方法是安装 XCode。这样你就可以访问“gcc”,因此,

gcc -o mybinaryfile mysourcefile.c

Then run it like this.

然后像这样运行它。

./mybinaryfile

Hope this helps!.

希望这可以帮助!。