简体   繁体   English

GNU Makefile,C编程

[英]GNU Makefile, c programming

my current makefile looks likes this 我当前的makefile看起来像这样

all: hello

hello: hello.o
    clang -o hello hello.o

hello.o: hello.c
    clang -Wall -std=c99 -c -o hello.o hello.c -lpthread

clean:
rm -f *.o *exe hello

How can I modify it to compile with the following: 如何修改它以进行以下编译:

clang -std=gnu99 -Wall -o hello hello.c -lpthread

Use 采用

hello: hello.c
    clang -std=gnu99 -Wall -o hello hello.c -lpthread

instead of the two rules you have for hello and hello.o now. 而不是您现在拥有hello和hello.o的两个规则。

When your program gets bigger, however, the separation of compilation to object files and linking may at some point be faster than compiling and linking everything in one go. 但是,当程序变大时,将编译到目标文件和链接的分离有时比一次编译和链接所有对象的速度要快。 With separated compilation and linking compilation units that are unmodified do not need to be recompiled every time. 对于单独的编译和链接,未经修改的编译单元无需每次都重新编译。

Try this - usually best to do the compiling is a few steps. 尝试此操作-通常最好的几个步骤就是进行编译。

all: hello

hello: hello.o
    clang -o hello hello.o -lpthread

hello.o: hello.c
    clang -Wall -std=c99 -c -o hello.o hello.c 

clean:
rm -f *.o  hello

Your modification requires just changing a single line; 您的修改只需要更改一行即可; but instead, you should use some variables to make it cleaner: 但是,您应该使用一些变量使其更简洁:

# C compiler
CC = clang
# Additional libraries
LIBS = -lpthread
# Compiler flags
CCFLAGS = -std=gnu99 -Wall $(LIBS)
# Output executable
OUT = hello

all: hello

hello: hello.o
    $(CC) $(CCFLAGS) -o $(OUT) hello.o

hello.o: hello.c
    clang $(CCFLAGS) -c -o hello.o hello.c

clean:
    rm -f *.o $(OUT)

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

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