C语言 如何在 Mac OS X 终端上使用 make 编译 C 程序

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

How to compile a C program with make on Mac OS X Terminal

cmacosterminalmakefile

提问by Raphael Teubner

I recently bought a Macbook Pro and I want to do some coding in C with the terminal.

我最近买了一台 Macbook Pro,我想用终端用 C 语言编写一些代码。

I'm able to compile the code with this command:

我可以使用以下命令编译代码:

gcc filename.c –o filename

But I want to compile it with the make command because I know it is best practice and I want to follow best practice.

但我想用 make 命令编译它,因为我知道这是最佳实践,我想遵循最佳实践。

make filename cc filename.c -o filename

This command is giving me the following output:

这个命令给了我以下输出:

make: Nothing to be done for `ex01'.
make: *** No rule to make target `cc'.  Stop.

Note that I have installed Xcode and Xcode developer command line tools and in the folder /usr/binI see the makeand makefileproperties.

请注意,我已经安装了 Xcode 和 Xcode 开发人员命令行工具,并且在文件夹/usr/bin 中我看到了makemakefile属性。

Does anyone have any hint on what should I do to be able to compile with makefile and cc argument?

有没有人对我应该怎么做才能使用 makefile 和 cc 参数进行编译有任何提示?

回答by David Ranieri

Create a file called Makefileon the same path with this content:

Makefile使用此内容创建一个在同一路径上调用的文件:

CC = cc
CFLAGS = -std=c99 -pedantic -Wall
OBJECTS = filename.o

all: appname

filename.o: filename.c
    $(CC) $(CFLAGS) -c filename.c

appname: $(OBJECTS)
    $(CC) $(OBJECTS) -o appname

clean:
    rm -f *.o appname

Then run:

然后运行:

make

Of course, replace appnamewith the name of your program

当然,替换appname为你的程序名称

Note: There must be a "tab" (not spaces) before

注意:之前必须有一个“制表符”(不是空格)

$(CC) $(CFLAGS) -c filename.c

and

$(CC) $(OBJECTS) -o appname

回答by kly

I was following the same tutorial, and ran into a similar problem.

我正在遵循相同的教程,并遇到了类似的问题。

I don't know what exactly you did, but I think the mistake was running the wrong command. You typed make filename cc filename.c -o filename, but the tutorial instructed us to use make filename, without the cc filename.c -o filenamepart. Maybe you read an old version?

我不知道你到底做了什么,但我认为错误是运行了错误的命令。您输入了make filename cc filename.c -o filename,但教程指示我们使用make filename,而没有cc filename.c -o filename部分。也许你读的是旧版本?

And, make filenameworks fine, You don't need a Makefile.

而且,make filename工作正常,您不需要 Makefile



FYI, Here's how I ran into the problem and how I solved it:

仅供参考,这是我遇到问题的方式以及解决方法:

  1. typed the code below, and saved it in a file named "ex1"

     int main(int argc, char *argv[])
     {
         puts("Hello world.");
    
         return 0;
     }
    
  2. typed make ex1in terminal
  3. got error message make: Nothing to be done for 'ex1'.
  1. 输入下面的代码,并将其保存在名为“ex1”的文件中

     int main(int argc, char *argv[])
     {
         puts("Hello world.");
    
         return 0;
     }
    
  2. make ex1在终端输入
  3. 收到错误信息 make: Nothing to be done for 'ex1'.

As you can see, my mistake was that the file name of the source code should be ex1.c, NOT ex1.

如您所见,我的错误是源代码的文件名应该是ex1.c,而不是ex1

And as I change the file name to ex1.c, and executed make ex1, it worked.

当我将文件名更改为 ex1.c 并执行时make ex1,它起作用了。