简体   繁体   English

如何为c写一个简单的makefile

[英]how to write a simple makefile for c

I need to write a simple make file for my.c, and so after 我需要为my.c编写一个简单的make文件,之后

make

then my program can be run by 然后我的程序可以通过

./my

my.c can be compiled by this: my.c可以通过以下方式进行编译:

gcc cJ/cJ.c my.c -lcrypto -o my -lm

Thanks I put this in my makefile 谢谢,我把这个放在我的makefile中

all:my
my: cJ.o my.o
 gcc cJ.o -lcrypt my.o -o my
cJ.o: cJ/cJ.c
     gcc -c cJ/cJ.c
my.o: my.c
     gcc -c my.c -lm

help please 请帮助

Here it a simple tutorial you could follow: http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ 在这里,您可以按照以下简单教程进行操作: http : //www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

One very important thing with make is to get the tabs right! make一项非常重要的事情是正确设置标签!

Well, makefiles are just kind of special scripts. 好吧,makefile只是一种特殊的脚本。 Every is unique, for such simple task this would be sufficient: 每个都是唯一的,对于这样简单的任务,这已经足够了:

Makefile: 生成文件:

CC=gcc
CFLAGS=-lm -lcrypto
SOURCES=my.c cJ/cJ.c

all: my

my: $(SOURCES)
        $(CC) -o my $(SOURCES) $(CFLAGS)

Later you may want to use some other options such as wildcards %.c to compile in multiple files without having to write them in. 稍后,您可能需要使用其他一些选项(例如通配符%.c)来编译多个文件,而不必将其写入。

Alternatively: 或者:

CC=gcc
CFLAGS=-lm -lcrypto

MY_SOURCES = my.c cJ/cJ.c
MY_OBJS = $(patsubst %.c,%.o, $(MY_SOURCES))

all: my

%o: %.c
    $(CC) $(CFLAGS) -c $<

my: $(MY_OBJS)
    $(CC) $(CFLAGS) $^ -o $@

Note that lines following each target ("my:", ...) must start with tab (\\t), not spaces. 请注意,每个目标(“ my:”,...)之后的行必须以制表符(\\ t)开头,而不是空格。

Just a minor correction: put the -lm to the linking step, and there after all object files. 只是一个小小的修正:将-lm放在链接步骤,然后放在所有目标文件之后。

all: my
my: cJ.o my.o
    gcc cJ.o my.o -o my -lcrypt -lm
cJ.o: cJ/cJ.c
    gcc -c cJ/cJ.c
my.o: my.c
    gcc -c my.c

And then, you could work more with automatic variables : 然后,您可以使用自动变量进行更多操作:

all: my
my: cJ.o my.o
    gcc $^ -o $@ -lcrypt -lm
cJ.o: cJ/cJ.c
    gcc -c $^
my.o: my.c
    gcc -c $^

where $@ is the target of the current rule and $^ are the prerequisites. 其中$@是当前规则的目标,而$^是前提。

See also http://www.gnu.org/software/make/manual/make.html . 另请参见http://www.gnu.org/software/make/manual/make.html

simple make file for your program is 您的程序的简单make文件是

build : 
        gcc /your_full_path_to_c_file/cJ.c my.c -lcrypto -o my -lm

just copy this in one file keep name of that file as makefile then run as make build 只需将其复制到一个文件中,将该文件的名称保留为makefile然后作为make build运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM