Redefining low-level library functions to enable direct use of high-level library functions in the C library

If you define your own version of __FILE, your own fputc() and ferror() functions, and the __stdout object, you can use all of the printf() family, fwrite()fputs()puts() and the C++ object std::cout unchanged from the library.

These examples show you how to do this. However, consider modifying the system I/O functions instead of these low-level library functions if you require real file handling.

You are not required to re-implement every function shown in these examples. Only re-implement the functions that are used in your application.

Retargeting printf()

#include <stdio.h>
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef'd in stdio.h. */
FILE __stdout;
int fputc(int ch, FILE *f)
{
/* Your implementation of fputc(). */
return ch;
}
int ferror(FILE *f)
{
/* Your implementation of ferror(). */
return 0;
}
void test(void)
{
printf("Hello world\n");
}

Note

Be aware of endianness with fputc()fputc() takes an int parameter, but contains only a character. Whether the character is in the first or the last byte of the integer variable depends on the endianness. The following code sample avoids problems with endianness:

extern void sendchar(char *ch);
int fputc(int ch, FILE *f)
{
/* example: write a character to an LCD */
char tempch = ch; // temp char avoids endianness issue
sendchar(&tempch); // sendchar(&ch) would not work everywhere
return ch;
}

Retargeting cout

File 1: Re-implement any functions that require re-implementation.

#include <stdio.h>
namespace std {
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
FILE __stdout;
FILE __stdin;
FILE __stderr;
int fgetc(FILE *f)
{
/* Your implementation of fgetc(). */
return 0;
}
int fputc(int c, FILE *stream)
{
/* Your implementation of fputc(). */
}
int ferror(FILE *stream)
{
/* Your implementation of ferror(). */
}
long int ftell(FILE *stream)
{
/* Your implementation of ftell(). */
}
int fclose(FILE *f)
{
/* Your implementation of fclose(). */
return 0;
}
int fseek(FILE *f, long nPos, int nMode)
{
/* Your implementation of fseek(). */
return 0;
}
int fflush(FILE *f)
{
/* Your implementation of fflush(). */
return 0;
}
}

File 2: Print "Hello world" using your re-implemented functions.

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world\n";
return 0;
}

By default, fread() and fwrite() call fast block input/output functions that are part of the Arm stream implementation. If you define your own __FILE structure instead of using the Arm stream implementation, fread() and fwrite() call fgetc() instead of calling the block input/output functions.

来源

【ARM】重新定义低级库函数,以便能够直接使用 C 库中的高级库函数的更多相关文章

  1. Entity Framework 6 Recipes 2nd Edition(11-11)译 -> 在LINQ中调用数据库函数

    11-11. 在LINQ中调用数据库函数 问题 相要在一个LINQ 查询中调用数据库函数. 解决方案 假设有一个任命(Appointment )实体模型,如Figure 11-11.所示, 我们想要查 ...

  2. Raspberry Pi3 ~ Eclipse中添加wiringPi 库函数

    这篇是在博客园原创 转载注明出处啊 以前用单片机.STM32之类的时候都是在一个集成的开发环境下进行的 比如Keil.IAR等 那么linux下编程,eclipse是个不错的选择 关于树莓派的GPIO ...

  3. 编译在arm板上使用的sqlite3的静动态库

    采用的是sqlite-autoconf-3080002.tar.gz 解压 tar xvf sqlite-autoconf-3080002.tar.gz 进入 cd sqlite-autoconf-3 ...

  4. 标准c库函数与Linux下系统函数库 区别 (即带不带缓冲区的学习)

    我们都知道,C语言在UNIX/Linux系统下有一套系统调用(系统函数),比如文件操作open().close().write().read()等,而标准C语言的库函数中也有一套对文件的操作函数fop ...

  5. Entity Framework 中的Code First 中引入数据库函数

    1,在项目中添加CodeFirstStoreFunctions包: Install-Package EntityFramework.CodeFirstStoreFunctions 2,注册注册函数,F ...

  6. 《Android进阶》之第一篇 在Java中调用C库函数

    在Java代码中通过JNI调用C函数的步骤如下: 第一步:编写Java代码 class HelloJNI{ native void printHello(); native void printStr ...

  7. C#中List调用库函数sort进行升序排序

    private void button1_Click(object sender, EventArgs e) { List<int> demo2 = new List<int> ...

  8. 如何调用.so动态库中的函数,如何把自己的函数导出为.so的动态库函数供别人调用

    调用.so中的函数和平常的函数没有区别,只是在编译连接时加上-lxxxx就行了.要生成.so库,则编译时用下面的语句:gcc -shared -Wl,-soname,libmyfun.so -o li ...

  9. Arm Linux系统调用流程详细解析

    Linux系统通过向内核发出系统调用(system call)实现了用户态进程和硬件设备之间的大部分接口. 系统调用是操作系统提供的服务,用户程序通过各种系统调用,来引用内核提供的各种服务,系统调用的 ...

  10. python快速教程-vamei

    2016年10月26日 12:00:53 今天开始着手python的学习,希望能高效快速的学完! Python基础(上)... 7 实验简介... 7 一.实验说明... 8 1. 环境登录... 8 ...

随机推荐

  1. 华企盾DSC邮件服务器测试连接提示Bad login or password(账号密码错误)

    解决方法:出现该提示说明账号和密码有一个填错了,注意:这里的密码不是邮箱本身的密码,是授权码,具体可以在邮箱设置中查看,而且必须开启smtp服务才能正常使用.

  2. ​3D 高斯点染简介

    3D 高斯点染技术由 3D Gaussian Splatting for Real-Time Radiance Field Rendering 一文首次提出.作为一种栅格化技术,3D 高斯点染可用于实 ...

  3. 【C#】【字符串内插】关于$" "(字符串内插构造格式化字符串)的使用

    1.变量名插入使用 var num = 1; Console.WriteLine($"Output number:{num}"); // Output: Output number ...

  4. iMessage群发系统常见代码分享!

    随着iMessage的普及,越来越多的开发者开始关注如何利用iMessage进行消息群发,今天,我们就来分享一些常见的iMessage群发系统的代码示例,帮助大家更好地实现这一功能. 一.使用Swif ...

  5. DNS解析中CNAME和MX记录冲突

    转载:DNS中CNAME和MX记录的冲突 在DNS解析中,CNAME记录与其他记录往往是互斥的.最常见的是CNAME记录和MX记录的互斥.例如我们在http://example.com部署官网,通过C ...

  6. 扩展中国剩余定理(Excrt)笔记

    扩展中国剩余定理(excrt) 本来应该先学中国剩余定理的.但是有了扩展中国剩余定理,朴素的 CRT 就没用了. 扩展中国剩余定理用来求解如下形式的同余方程组: \[\begin{cases} x \ ...

  7. 26、Flutter中命名路由

    Flutter 中的命名路由 main.dart中配置路由 void main() { runApp(MaterialApp( theme: ThemeData( appBarTheme: const ...

  8. 【新春特辑】发压岁钱、看贺岁片、AI写春联……华为云社区给大家拜年了

    摘要:充电团聚云上见,顺便攒攒压岁钱. 春!节!倒!计!时!啦! 农历新年即将到来,热闹的过年氛围逐渐弥漫,华为云社区先给大家拜个早年,祝所有小伙伴们新春快乐,牛年大吉! 回望2020年,社区涌现了许 ...

  9. 基于DMS的数仓智能运维服务,知多少?

    摘要:GaussDB(DWS)使用DMS来承载数据库的智能运维体系,提供了数据库运维过程中的监控,分析,处理三大核心处理过程. 本文分享自华为云社区<GaussDB(DWS) 数据库智能监控运维 ...

  10. 拔掉电源会怎样?GaussDB(for Redis)双活让你有备无患

    摘要:GaussDB(for Redis)推出双活方案,助力全球化业务部署,为您的数据资产保驾护航! 本文分享自华为云社区<华为云GaussDB(for Redis)揭秘第22期:拔掉电源会怎样 ...