how to write a simple makefile for c

后端 未结 3 1480
無奈伤痛
無奈伤痛 2021-01-07 03:43

I need to write a simple make file for my.c, and so after

make

then my program can be run by

./my
3条回答
  •  不要未来只要你来
    2021-01-07 04:18

    Just a minor correction: put the -lm to the linking step, and there after all object files.

    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.

提交回复
热议问题