LiteOS-M内核LibC实现有2种,可以根据需求进行二选一,分别是musl libC和newlibc。本文先学习下Newlib C的实现代码。文中所涉及的源码,均可以在开源站点https://gitee.com/openharmony/kernel_liteos_m 获取。
使用Musl C库的时候,内核提供了基于LOS_XXX适配实现pthread、mqeue、fs、semaphore、time等模块的posix接口(//kernel/liteos_m/kal/posix)。内核提供的posix接口与musl中的标准C库接口共同组成LiteOS-M的LibC。编译时使用arm-none-eabi-gcc,但只使用其工具链的编译功能,通过加上-nostdinc与-nostdlib强制使用我们自己改造后的musl-C。
社区及三方厂商开发多使用公版工具链arm-none-eabi-gcc加上私有定制优化进行编译,LiteOS-M内核也支持公版arm-none-eabi-gcc C库编译内核运行。newlib是小型C库,针对posix接口涉及系统调用的部分,newlib提供一些需要系统适配的钩子函数,例如_exit(),_open(),_close(),_gettimeofday()等,操作系统适配这些钩子,就可以使用公版newlib工具链编译运行程序。
1、Newlib C文件系统
在使用Newlib C并且使能支持POSIX FS API时(可以在kernel\liteos-m\目录下,执行make meuconfig弹出配置界面,路径为Compat-Choose libc implementation),如下图所示。可以使用文件kal\libc\newlib\porting\src\fs.c中定义的文件系统操作接口。这些是标准的POSIX接口,如果想了解POSIX用法,可以在linux平台输入 man -a 函数名称,比如man -a opendir来打开函数的手册。

1.1 函数mount、umount和umount2
这些函数的用法,函数实现和musl c部分一致。
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data)
{
return LOS_FsMount(source, target, filesystemtype, mountflags, data);
}
int umount(const char *target)
{
return LOS_FsUmount(target);
}
int umount2(const char *target, int flag)
{
return LOS_FsUmount2(target, flag);
}
1.2 文件操作接口
以下划线开头的函数实现是newlib c的钩子函数实现。有关newlib的钩子函数调用过程下文专门分析下。
int _open(const char *path, int oflag, ...)
{
va_list vaList;
va_start(vaList, oflag);
int ret;
ret = LOS_Open(path, oflag);
va_end(vaList);
return ret;
}
int _close(int fd)
{
return LOS_Close(fd);
}
ssize_
LiteOS-M NewlibC实现

最低0.47元/天 解锁文章
999

被折叠的 条评论
为什么被折叠?



