• 和菜鸟一起深入学习国嵌实验之简单Makefile


     

    1、首先是待make的程序:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
     
    int main(void)
    {
       printf("Hello Makefile!\n");
       
       return 0;
    }
     

     

    2、其次就是其makefile了:

     

    CC = $(CROSS_COMPILE)gcc
    LD = $(CROSS_COMPILE)ld
    STRIP = $(CROSS_COMPILE)strip
    OBJCOPY = $(CROSS_COMPILE)objcopy
    OBJDUMP = $(CROSS_COMPILE)objdump
    AR = $(CROSS_COMPILE)ar
    RANLIB = $(CROSS_COMPILE)ranlib
     
    CURTDIR = $(shell pwd)
    TARGET = my_make
     
    %.o:%.c
           $(CC)-c $(EXTRAFLAGS) $< -o $@
    %.o:%.S
           $(CC)-c $(EXTRAFLAGS) $< -o $@
     
    .PHONY: all clean
     
    $(TARGET): $(TARGET).o
           $(CC)  -o $@ $^
     
    clean:
           rm-rf $(TARGET) $(TARGET).o

     

    如果是要交叉编译的话,那么就如下:

    CROSS_COMPILE =/usr/local/arm/4.2.2-eabi/usr/bin/arm-linux-
     
    CC = $(CROSS_COMPILE)gcc
    LD = $(CROSS_COMPILE)ld
    STRIP = $(CROSS_COMPILE)strip
    OBJCOPY = $(CROSS_COMPILE)objcopy
    OBJDUMP = $(CROSS_COMPILE)objdump
    AR = $(CROSS_COMPILE)ar
    RANLIB = $(CROSS_COMPILE)ranlib
     
    CURTDIR = $(shell pwd)
    ROOTDIR = $(CURDIR)/../..
    TARGET = my_make
     
    #CFLAGS = -I$(ROOTDIR)/src/inc -EL
    #LDFLAGS = -EL -L$(ROOTDIR)/lib -lled
    CFLAGS = -I$(ROOTDIR)/src/inc
    LDFLAGS = -L$(ROOTDIR)/lib -lled
     
    %.o:%.c
           $(CC)-c $(CFLAGS) $(EXTRAFLAGS) $< -o $@
    %.o:%.S
           $(CC)-c $(CFLAGS) $(EXTRAFLAGS) $< -o $@
     
    .PHONY: all clean
     
    $(TARGET): $(TARGET).o
           $(CC)$(LDFLAGS) -o $@ $^
     
    clean:
           rm-rf $(TARGET) $(TARGET).o

     

    以上是编译成可执行文件的,如果要编译成.so文件呢?那么其makefile要怎么实现呢?

    1、 首先是最外面的makefile了

    CROSS_COMPILE=/usr/local/arm/4.2.2-eabi/usr/bin/arm-linux-
     
    CC =$(CROSS_COMPILE)gcc
    LD =$(CROSS_COMPILE)ld
    STRIP =$(CROSS_COMPILE)strip
    OBJCOPY =$(CROSS_COMPILE)objcopy
    OBJDUMP =$(CROSS_COMPILE)objdump
    AR =$(CROSS_COMPILE)ar
    RANLIB =$(CROSS_COMPILE)ranlib
     
    ROOTDIR =$(shell pwd)
    INCLUDEDIRS =-I$(ROOTDIR)/inc
     
    #LIB_DIR =/opt/mips-linux-gnu-4.3.-51/mips-linux-gnu/libc/el/usr/lib
     
    #CFLAGS = -O0-g3 -EL -Wall $(INCLUDEDIRS) -mhard-float -fPIC
    CFLAGS = -O0 -g3-EL -Wall $(INCLUDEDIRS) -fPIC
    AFLAGS = -O0 -g3-EL -Wall $(INCLUDEDIRS) -mmt -fPIC
    LDFLAGS = -EL-fPIC -shared -LLIB_DIR -lm -lpthread -lc -ldl
     
    export CC LDSTRIP OBJCOPY OBJDUMP AR RANLIB CFLAGS LDFLAGS AFLAGS INCLUDEDIRS ROOTDIR
     
    .PHONY : all
     
    all    : childdir
     
    childdir:
    @for dir in $(SUB_DIRS); do \
            $(MAKE) -C $$dir || exit 1;\
    done
     
    SUB_DIRS = led
     
    install:
    mkdir -p ../lib
    mkdir -p ../module
     
    dep:
    @for dir in $(SUB_DIRS); do \
                    $(MAKE) -C $$dir dep; \
    done
     
    clean:
    @for dir in $(SUB_DIRS); do\
            $(MAKE) -C $$dir clean; \
    done

     

    然后是其Rules.make

    $(MODULE) : $(OBJFILE) $(LLIBS)
           @echo[LD] $@
           $(CC)$(LDFLAGS) -o $(MODULE) $(OOBJFILE)
           mv$(MODULE) $(ROOTDIR)/../lib
     
    %.o:%.c
           $(CC)-c $(CFLAGS) $(EXTRAFLAGS) $< -o $@
    %.o:%.S
           $(CC)-c $(CFLAGS) $(EXTRAFLAGS) $< -o $@
     
    dep:
           $(CC)$(INCLUDEDIRS) -M $(SRC) > ./depend.mk
     
    clean:
           rm-rf $(OBJFILE)
           rm-rf $(MODULE)
           rm-rf depend.mk
     
    -include ./depend.mk

     

    接着你要编译的程序,比如上面的那个程序。

    MODULE = libmy_make.so
     
    SRS = $(wildcard *.S)
    SRC = my_make.c
     
    OBJFILE = $(SRC:%.c=%.o) $(SRS:%.S=%.o)
     
    include $(ROOTDIR)/Rules.make

     

     

  • 相关阅读:
    2021
    关于react,vue框架的搭建及bug处理
    centOS7使用
    git初探
    项目分享技术大会总结
    angular中的异常机制与异常之外的处理
    angluarjs中指令的使用方式
    Servlet知识总结
    有时候真的需要一点点改变
    story
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300024.html
Copyright © 2020-2023  润新知