【SemiDrive G9X】【PTG5.0】:G9 R 核实现编译及调用静态库

一、概述

静态库(Static Library)是一种包含已编译对象代码的文件集合,它可以被链接到一个可执行程序中。静态库的主要特点是在编译时被链接到可执行程序中,因此它的代码和数据会被完整地复制到最终的可执行文件中。这意味着可执行程序不再依赖于静态库的存在,可以独立地运行。总的来说,静态库提供了一种有效的方式来组织、重用和分发代码,同时提高了程序的性能和可移植性。本文主要讲述如何在芯驰 G9X  R 核的平台实现编译静态库及调用静态库。


二、编译出静态库

先编译出 xxx.a 文件。

1、在路径:/buildsystem/rtos/freertos_safetyos/application/sample 目录下新建文件夹 testlib;在 testlib 文件夹中新建 inc 文件夹、rules.mk、testlib.c 文件;其中,在 inc.mk 文件夹中新建 testlib.h 文件,其中文件内容如下:

#ifndef  _TESTLIB_H_
#define  _TESTLIB_H_  
void welcome(void);  
#endif

rules.mk 文件内容如下:

LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)
MODULE_DEFINES += MODULE_STATIC_LIB=1
GLOBAL_INCLUDES += \
    $(LOCAL_DIR)/inc \

MODULE_SRCS += \
    $(LOCAL_DIR)/testlib.c \


MODULE_COMPILEFLAGS += -Wno-format -fno-builtin -Wno-unused-variable \
               -Wno-sign-compare -Wno-format -Wno-int-to-void-pointer-cast

include make/module.mk

testlib.c 文件内容如下:

#include "testlib.h"
#include <stdio.h>
void welcome(void) {
     printf("welcome to testlib!\n");
}

2、在路径:/buildsystem/rtos/freertos_safetyos/application/rules.mk 添加以下代码:

ifeq ($(SUPPORT_TEST_LIB),true)
MODULE_DEPS += application/sample/testlib
endif

3、在路径:/buildsystem/rtos/freertos_safetyos/project/safety-g9x-ref.mk 下添加

SUPPORT_TEST_LIB := true

此时,可以编译了。

编译后可以在路径:/buildsystem/rtos/freertos_safetyos/build-safety-g9x-ref/application/sample/ 下生成 testlib.mod.a 这是就是生成的静态库,重命名为 xxx.a 静态库文件就可以了;


三、调用静态库

以下提供一个 demo,来调用 xxx.a 静态库。

1、在路径 /buildsystem/rtos/freertos_safetyos/application/sample 目录下新建 testlib_test 文件夹;在 testlib.test 文件夹中添加 lib  和  src 文件夹,rules.mk 文件将上面生成的 xxx.a 移植进 lib 文件夹下面;在 src 文件夹下面添加 testlib_test.c 文件,内容如下:

/*
 * 
 *
 * Copyright (c) 2021 Semidrive Semiconductor.
 * All rights reserved.
 *
 * Description:
 *
 * Revision History:
 * -----------------
 */
#include <app.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <debug.h>
#include <platform.h>
#include <lib/console.h>
#include <rtc_drv.h>
#include <platform/interrupts.h>
#include <irq_v.h>
#include <pmu_hal.h>
#include <arm_gic_hal.h>

#include <sys/types.h>
#include <arch/arch_ops.h>
#include <lk/init.h>
#include <kernel/thread.h>
#include "config.h"
#include <string.h>
#include <platform/interrupts.h>
#include <kernel/vm.h>
#include <reg.h>
#define WITH_LIB_CONSOLE 1
#include <testlib.h>

void App_testlib_test(void)

{

    welcome();

}
#if defined(WITH_LIB_CONSOLE)
STATIC_COMMAND_START
STATIC_COMMAND("testlib_test", "testlib Test", (console_cmd)&App_testlib_test)
STATIC_COMMAND_END(testlib_test);
#endif

APP_START(testlib_test)
.flags = 0,
APP_END

在 rules.mk 文件添加以下内容:

LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_SRCS := $(shell find -L $(LOCAL_DIR)/src -name \*.c)

MATH_DIR := $(GET_LOCAL_DIR)

GLOBAL_MODULE_LDFLAGS += -L$(MATH_DIR)/lib  

MODULE_COMPILEFLAGS += -Wno-format -fno-builtin -Wno-unused-variable -Wno-sign-compare -Wno-format -Wno-int-to-void-pointer-cast -Wno-unused-but-set-variable -Wno-maybe-uninitialized

include make/module.mk

2、在路径:/buildsystem/rtos/freertos_safetyos/application/rules.mk 添加以下代码:

ifeq ($(SUPPORT_TEST_LIB_TEST),true)
MODULE_DEPS += application/sample/testlib_test
endif

3、在路径:/buildsystem/rtos/freertos_safetyos/project/safety-g9x-ref.mk 下添加:

SUPPORT_TEST_LIB_TEST := true

重新编译烧录后,可以在 R 核串口运行 testlib_test 打印 welcome to testlib! 代表成功。


四、参考文档

1.《SD_G9_Quick_Start_Rev2.4.pdf》


五、总结

以上在 G9X R 核实现了静态库的编译及调用。

接下来将会更新更多关于 SemiDrive G9 系列的文章,感兴趣的可以在评论区评论或者关注。

★博文内容均由个人提供,与平台无关,如有违法或侵权,请与网站管理员联系。

★文明上网,请理性发言。内容一周内被举报5次,发文人进小黑屋喔~

评论