C语言 如何在 Mac 上编译 .c 文件?

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

How do I compile a .c file on my Mac?

cmacos

提问by David

How do I compile a .c file on my Mac?

如何在 Mac 上编译 .c 文件?

采纳答案by Adam Batkin

You will need to install the Apple Developer Tools. Once you have done that, the easiest thing is to either use the Xcode IDE or use gcc, or nowadays better cc(the clang LLVM compiler), from the command line.

您将需要安装Apple Developer Tools。完成后,最简单的方法是从命令行使用 Xcode IDE 或使用gcc,或者现在更好cc(clang LLVM 编译器)。

According to Apple's site, the latest version of Xcode (3.2.1) only runs on Snow Leopard (10.6) so if you have an earlier version of OS X you will need to use an older version of Xcode. Your Mac should have come with a Developer Tools DVD which will contain a version that should run on your system. Also, the Apple Developer Tools site still has older versions available for download. Xcode 3.1.4 should run on Leopard (10.5).

根据 Apple 的网站,最新版本的 Xcode (3.2.1) 只能在 Snow Leopard (10.6) 上运行,因此如果您使用的是较早版本的 OS X,则需要使用较旧版本的 Xcode。您的 Mac 应该附带一张开发人员工具 DVD,其中包含应该在您的系统上运行的版本。此外,Apple Developer Tools 站点仍有旧版本可供下载。Xcode 3.1.4 应该在 Leopard (10.5) 上运行。

回答by OndraSej

You'll need to get a compiler. The easiest way is probably to install XCode development environment from the CDs/DVDs you got with your Mac, which will give you gcc. Then you should be able compile it like

你需要一个编译器。最简单的方法可能是从您随 Mac 获得的 CD/DVD 安装 XCode 开发环境,这将为您提供 gcc。然后你应该可以像这样编译它

gcc -o mybinaryfile mysourcefile.c

回答by Fattie

Just for the record in modern times,

只是为了记录在现代,

for 2017 !

2017年!

1 - Just have updated Xcode on your machine as you normally do

1 - 像往常一样在您的机器上更新 Xcode

2 - Open terminal and

2 - 打开终端和

$ xcode-select --install

it will perform a short install of a minute or two.

它将执行一两分钟的简短安装。

3 - Launch Xcode. "New" "Project" ... you have to choose "Command line tool"

3 - 启动 Xcode。“新建”“项目”...你必须选择“命令行工具”

Note - confusingly this is under the "macOS" tab.

注意 - 令人困惑的是这是在“macOS”选项卡下

choose this one

选择这个

Select "C" language on the next screen...

在下一个屏幕上选择“C”语言...

enter image description here

在此处输入图片说明

4- You'll be asked to save the project somewhere on your desktop. The name you give the project here is just the name of the folderthat will hold the project. It does not have any importance in the actual software.

4- 系统会要求您将项目保存在桌面上的某个位置。您在此处为项目指定的名称只是将保存该项目的文件夹的名称。它在实际软件中没有任何重要性。

5 - You're golden! You can now enjoy c with Mac and Xcode.

5 - 你是金子!您现在可以使用 Mac 和 Xcode 享受 c。

you're golden

你是金子

回答by aeb0

In 2017, this will do it:

在 2017 年,这将做到:

cc myfile.c

回答by AlBlue

You can use gcc, in Terminal, by doing gcc -c tat.c -o tst

您可以通过执行 gcc -c tat.c -o tst 在终端中使用 gcc

however, it doesn't come installed by default. You have to install the XCode package from tour install disc or download from http://developer.apple.com

但是,默认情况下它不会安装。您必须从 Tour 安装光盘安装 XCode 包或从http://developer.apple.com下载

Here is where to download past developer tools from, which includes XCode 3.1, 3.0, 2.5 ...

这里是从哪里下载过去的开发人员工具,其中包括 XCode 3.1、3.0、2.5 ...

http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1

http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1

回答by tvanfosson

Use the gcccompiler. This assumes that you have the developer tools installed.

使用gcc编译器。这假设您已安装开发人员工具。

回答by Alex Gray

Ondrasej is the "most right" here, IMO.
There are also gui-er ways to do it, without resorting to Xcode. I like TryC.

Ondrasej 是这里“最正确的人”,IMO。
还有一些gui-er方法可以做到这一点,而无需求助于Xcode。我喜欢TryC。

Mac OS X includes Developer Tools, a developing environment for making Macintosh applications. However, if someone wants to study programming using C, Xcode is too big and too complicated for beginners, to write a small sample program. TryC is very suitable for beginners.

Mac OS X 包括开发工具,这是一个用于制作 Macintosh 应用程序的开发环境。但是,如果有人想学习使用 C 编程,Xcode 对于初学者来说太大且太复杂,无法编写一个小的示例程序。TryC 非常适合初学者。

enter image description here

在此处输入图片说明

You don't need to launch a huge Xcode application, or type unfamiliar commands in Terminal. Using TryC, you can write, compile and run a C, C++ and Ruby program just like TextEdit. It's only available to compile one source code file but it's enough for trying sample programs.

你不需要启动一个巨大的 Xcode 应用程序,或者在终端中输入不熟悉的命令。使用 TryC,您可以编写、编译和运行 C、C++ 和 Ruby 程序,就像 TextEdit 一样。它只能编译一个源代码文件,但它足以尝试示例程序。