美文网首页
R818-linux so库生成和demo打包

R818-linux so库生成和demo打包

作者: 狼少丷 | 来源:发表于2024-04-29 15:17 被阅读0次
    1.在package/目录中创建我们的文件 :
    这里我随便创建了些文件如图,看名字就知道很随意。 image.png

    层级关系:

    .
    ├── demo
    │   ├── Makefile
    │   └── my_test.c
    ├── files
    │   └── mytest_supplicant.conf
    ├── Makefile
    └── src
        └── core
            ├── include
            │   └── scan.h
            ├── Makefile
            └── myxpz.c
    
    2.package/testmanager/demo/Makefile ,相关的Makefile语法自己补充。
    INCLUDES += -I$(CONFIG_PREFIX)/usr/include \
                -I ../src/core/include
    install:
        -@mkdir -p $(CONFIG_PREFIX)/usr/bin
    
    #mytest
    mytest: my_test.c
        $(CC) -o $@ $^ $(CFLAGS) $(INCLUDES)  $(LDFLAGS) -L$(CONFIG_PREFIX)/usr/lib -lmyxpz
        -@cp -f mytest $(CONFIG_PREFIX)/usr/bin
    
    ####################################################################
    all: install mytest 
    clean:
        rm -rf mytest
    
    3.so库实际生成的mk

    package/testmanager/src/core/Makefile

    include $(TOPDIR)/rules.mk
    include $(BUILD_DIR)/package.mk
    
    target          = libmyxpz.so
    
    INCLUDES        += -I. -I./include
    LIBS            += -lpthread -ldl -lrt
    
    # 在同行的为同一进程,换行需用 \
    # SRCS =myxpz.c myxpz_event.c myxpz_scan.c \
    #       xpz.c myxpz_conf.c 
    
    # 遍历Makefile所在的当前文件夹内的所有.c文件
    SRCS = $(wildcard *.c)
    # SRCS += $(wildcard ./file/*.c)
    
    install:
        -@mkdir -p $(CONFIG_PREFIX)/usr/lib
        -@mkdir -p $(CONFIG_PREFIX)/usr/include
        @cp ./include/*.h $(CONFIG_PREFIX)/usr/include
    
    $(target): $(SRCS)
        $(CC) -fPIC -shared $(INCLUDES) $(LDFLAGS) $^ -o $@ $(LIBS)
        @cp $@ $(CONFIG_PREFIX)/usr/lib
    
    ####################################################################
    all: install $(target)
    clean:
        rm -rf $(target)
    
    4.so文件生成但还缺少一步来跟随打包进入系统镜像中。

    这里包含 menuconfig 的注册。

    include $(TOPDIR)/rules.mk
    include $(BUILD_DIR)/package.mk
    
    PKG_NAME:=testmanager
    PKG_VERSION:=0.0.1
    PKG_RELEASE:=1
    
    define Package/$(PKG_NAME)/Default
      SECTION:=utils
      CATEGORY:=Testmanager
      TITLE:=MY testmanager
      DEPENDS:=
    endef
    
    define Package/$(PKG_NAME)
      $(call Package/testmanager/Default)
      MENU:=1
      DEFAULT:=1
    endef
    
    define Package/$(PKG_NAME)-demo
      $(call Package/testmanager/Default)
      TITLE:=MY testmanager app demo
      DEPENDS := testmanager +testmanager
    endef
    
    define Package/$(PKG_NAME)/description
        Mytest test manager
    endef
    
    
    define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        $(CP) -r ./src $(PKG_BUILD_DIR)/
        $(CP) -r ./demo $(PKG_BUILD_DIR)/
    endef
    
    define Build/Configure
    endef
    
    TARGET_CFLAGS += -pthread
    
    define Build/Compile/$(PKG_NAME)
        # libmyxpz.so
        $(MAKE) -C $(PKG_BUILD_DIR)/src/core/ \
            ARCH="$(TARGET_ARCH)" \
            AR="$(TARGET_AR)" \
            CC="$(TARGET_CC)" \
            CXX="$(TARGET_CXX)" \
            CFLAGS="$(TARGET_CFLAGS)" \
            LDFLAGS="$(TARGET_LDFLAGS)" \
            CONFIG_PREFIX="$(PKG_INSTALL_DIR)" \
            all
    endef
    
    ifeq ($(CONFIG_PACKAGE_testmanager-demo),y)
        # testmanager-demo
    define Build/Compile/$(PKG_NAME)-demo
        $(MAKE) -C $(PKG_BUILD_DIR)/demo/ \
            ARCH="$(TARGET_ARCH)" \
            AR="$(TARGET_AR)" \
            CC="$(TARGET_CC)" \
            CXX="$(TARGET_CXX)" \
            CFLAGS="$(TARGET_CFLAGS)" \
            LDFLAGS="$(TARGET_LDFLAGS)" \
            CONFIG_PREFIX="$(PKG_INSTALL_DIR)" \
            all
    endef
    endif
    
    define Build/Compile
        $(Build/Compile/$(PKG_NAME))
        $(Build/Compile/$(PKG_NAME)-demo)
    endef
    
    define Build/InstallDev
        $(INSTALL_DIR) $(1)/lib
        $(INSTALL_DIR) $(1)/usr/include
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libmyxpz.so $(1)/lib
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/include/*.h $(1)/usr/include
    endef
    
    define Package/$(PKG_NAME)/install
        $(INSTALL_DIR) $(1)/lib
        $(INSTALL_DIR) $(1)/usr/include
        $(INSTALL_DIR) $(1)/etc
    
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libmyxpz.so $(1)/lib
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/include/*.h $(1)/usr/include
        $(INSTALL_DATA) ./files/mytest_supplicant.conf $(1)/etc
    endef
    
    define Package/$(PKG_NAME)-demo/install
        $(INSTALL_DIR) $(1)/bin
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/mytest $(1)/bin
    endef
    
    $(eval $(call BuildPackage,$(PKG_NAME)))
    $(eval $(call BuildPackage,$(PKG_NAME)-demo))
    
    
    5.需要再 menuconfig中选择我们的模块和lib库。 image-20240430150837190.png
        Testmanager  --->   
              <*> testmanager............................................... MY testmanager  --->  
                    <*>   testmanager-demo................................. MY testmanager app demo   
    
    6.最终产物如图: image-20240430150448162.png

    结束✿✿ヽ(°▽°)ノ✿!

    相关文章

      网友评论

          本文标题:R818-linux so库生成和demo打包

          本文链接:https://www.haomeiwen.com/subject/jogtfjtx.html