日期:2014-05-16  浏览次数:20632 次

一个简单的Makefile问题
当前目录下有三个文件夹 common decomp comp

每个文件夹下有一堆c文件和h文件 假设common下有文件common1.c common1.h common2.c common2.h 其他依次类推……
文件夹内部各文件间乱七八糟的依赖


在当前目录下写一个测试程序 test.c 其中用到了上述三个文件夹中的程序

现在想在当前目录下写一个makefile 生成一个可执行文件test

怎么写?

新手,欢迎鄙视



------解决方案--------------------
C/C++ code

C_SRCS += \
    src1/common1.cpp \
    src2/common2.cpp \
    src3/common3.cpp \
        test.c
        

OBJS += \
    src1/common1.o   \
    src2/common2.o \
    src3/common3.o \
        test.o


all: test

test: $(OBJS) 
    @echo 'Building target: $@'
    g++  -o test $(OBJS) -L./ $(LIBS)  -lxml2  

%.o: src1/%.cpp
    @echo 'Building file: $<'
    g++ -c $<  -MD "-D_DEBUG" "-D__cplusplus"  -L./lib -I/usr/include/ -I/usr/include/libxml2 `
    @echo 'Finished building: $<'

%.o: src2/%.cpp
    @echo 'Building file: $<'
    g++ -c $<  -MD "-D_DEBUG" "-D__cplusplus"  -L./lib -I/usr/include/ -I/usr/include/libxml2 `
    @echo 'Finished building: $<'

%.o: src3/%.cpp
    @echo 'Building file: $<'
    g++ -c $<  -MD "-D_DEBUG" "-D__cplusplus"  -L./lib -I/usr/include/ -I/usr/include/libxml2 `
    @echo 'Finished building: $<'