如何编写一个makefile以在不同目录中生成目标文件和可执行文件? [英] How to write a makefile to generate object files and executable in different directory?
本文介绍了如何编写一个makefile以在不同目录中生成目标文件和可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在要继续进行,我需要更改现有Makefile的显示方式.
Now to proceed further I need to change the presentation of existing Makefile.
目前我正在使用:
~/Linuz/src: 1.c, 2.c, 3.c ...
~/Linuz/inc: abc.h, xyz.h
和makefile位于:~/Linuz/some_other_dir/
and makefile is in: ~/Linuz/some_other_dir/
但是需要更改结构.
- 想从(
~/Linuz/src/
和~/linuz/inc
)创建一个库 - 库将用于获取可执行文件.应用程序源文件(.c文件)位于
~/Linuz/app/
中
-
~/Linuz/bin/
应该在编译期间创建,以存储所有目标文件和可执行文件.
- Want to create a library from (
~/Linuz/src/
and~/linuz/inc
) - Library will be used to get executable. Application source files(.c files) are in
~/Linuz/app/
~/Linuz/bin/
should be created during compilation to store all the object files and executable file.
任何建议??
我的makefile如下:
all: Library.a
%.o: ../src/%.c
$(CC) $(CFLAGS) -I../inc/ -c -o $@ $^
Library.a: $(SRC_DIR)/1.c $(SRC_DIR)/2.c $(SRC_DIR)/3.c $(SRC_DIR)/4.c $(SRC_DIR)/5.c
$(CC) $(LDFLAGS) -o $@ $^
all: prog
%.o: ./*.c
$(CC) $(CFLAGS) -ILibrary.a -c -o $@ $^
prog: $(APP_DIR)/app1.c $(APP_DIR)/app2.c $(APP_DIR)/app3.c
clean:
rm -f *.o my_program
推荐答案
假设您的代码体系结构如下:
Let assume that your code architecture looks like that:
└── linuz
├── app
│ ├── app1.c
│ ├── app2.c
│ └── app3.c
├── bin
├── inc
│ └── any.h
├── some_other_dir
│ └── Makefile
└── src
├── 1.c
├── 2.c
└── 3.c
因此您的Makefile可能是:
So your Makefile could be:
all: ../bin/libmy_lib.a ../bin/my_program
../bin/my_lib_%.o: ../src/%.c
$(CC) $(CFLAGS) -I../inc -c -o $@ $^
../bin/libmy_lib.a: ../bin/my_lib_1.o ../bin/my_lib_2.o ../bin/my_lib_3.o
ar rcs $@ $^
../bin/my_app_%.o: ../app/%.c
$(CC) $(CFLAGS) -I../inc -c -o $@ $^
../bin/my_program: ../bin/my_app_app1.o ../bin/my_app_app2.o ../bin/my_app_app3.o ../bin/libmy_lib.a
$(CC) $(LDFLAGS) -L../bin/ -lmy_lib -o $@ $^
clean:
rm -f ../bin/*.o ../bin/libmy_lib.a ../bin/my_program
For the explanation, refer to your previous question
这篇关于如何编写一个makefile以在不同目录中生成目标文件和可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文