首页 > 解决方案 > 如何使用 makefile 在子目录中创建目标文件 - C 语言

问题描述

我想在 /objects 等子目录中创建所有 .o 文件,以保持 /src 井井有条。

我已经有了这个:

progr : main.o manipulate_strings.o environment_set.o variables.o segment_private.o rectangle_private.o circle_private.o tree.o dynamic_list.o visibility.o query.o
    gcc -fstack-protector-all -std=c99 -g main.o manipulate_strings.o environment_set.o variables.o segments_private.o rectangle_private.o tree.o dynamic_list.o circle_private.o visibility.o query.o -o progr -lm
    rm *.o

main.o : main.c manipulate_strings.o
    gcc -fstack-protector-all -std=c99 -c -g main.c

manipulate_strings.o : manipulate_strings.c manipulate_strings.h
    gcc -fstack-protector-all -std=c99 -c -g manipulate_strings.c

environment_set.o : environment_set.c environment_set.h
    gcc -fstack-protector-all -std=c99 -c -g environment_set.c

variables.o : private/variables.c private/variables.h
    gcc -fstack-protector-all -std=c99 -c -g private/variables.c -lm

segment_private.o : private/segments_private.c private/segments_private.h
    gcc -fstack-protector-all -std=c99 -c -g private/segments_private.c -lm

rectangle_private.o : private/rectangle_private.c private/rectangle_private.h
    gcc -fstack-protector-all -std=c99 -c -g private/rectangle_private.c

circle_private.o : private/circle_private.c private/circle_private.h
    gcc -fstack-protector-all -std=c99 -c -g private/circle_private.c

tree.o : data_struct/tree.c data_struct/tree.h
    gcc -fstack-protector-all -std=c99 -c -g data_struct/tree.c

dynamic_list.o : data_struct/dynamic_list.c data_struct/dynamic_list.h
    gcc -fstack-protector-all -std=c99 -c -g data_struct/dynamic_list.c

visibility.o : visibility.c visibility.h
    gcc -fstack-protector-all -std=c99 -c -g visibility.c

query.o : query.c query.h
    gcc -fstack-protector-all -std=c99 -c -g query.c -lm

尽管它用 .o 文件填充了我的 src 目录,但它工作得很好。知道怎么做吗?

标签: makefile

解决方案


推荐阅读