打印
[开发工具]

Keil中使用MicroLib,以及malloc

[复制链接]
476|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tabmone|  楼主 | 2024-12-21 23:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


MicroLIB

The C Standard Library (stdlib.h) contains a number of useful and common tools, including:

  • string conversion
  • pseudo-random sequence generation
  • dynamic memory management
  • environment
  • searching and sorting
  • integer arithmethics
  • multibyte characters
  • multibyte strings

as well as some constants

EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX NULL RAND_MAX

and type definitions

div_t ldiv_t lldiv_t size_t

More information about the C standard library is available at cplusplus.com reference pages.

The standard library and standard input/output (stdio.h) are implemented in libc, and this is the one library that is, by default, linked to when you compile C code; if you want use the math library, you must explicitly link it with -lm, and using the pthread library requires -lpthread, but you never need include -lc if you require something in either stdlib.h or stdio.h.

The MDK-ARM that comes with μVision does not have a complete implementation of the standard library, and that library is not included by default. Instead, they provide a package called Microlib that implements many of the features in the standard library.

In order to include the Microlib, it is necessary to toggle a setting in the options, which you may select by either entering Alt-F7 or selecting Project→Options for Target 'Target name' or the icon . This will bring up the Options for Target 'Target name' dialog. Select the check box next to Use MicroLIB in the Code Generation panel, as shown in Figure 1.


Figure 1. The check box selection for using the MicroLIB.

Simple example

Now you can take your previous example project and now include stdlib.h at the top and replace the body of void main(void)with

int main( void ) {  int i, *array;                SystemInit();        while ( 1 ) {                array = (int *) malloc( N * sizeof( int ) );                                for ( i = 0; i < N; ++i ) {                        array = i;                }                free( array );        }}Example using a linked list

This is a rather boring example, so let's have a little more fun. In the C tutorials, there is an example that builds a singly linked list. In the source directory, you will find the header file and source file for this data structure. Save these in the same directory as your other source file for the project above. You will then need to add single_list.c to the Group Source code. You do not have to add the header file to the group, but it must still exist in the same directory as the source file.

You will now replace the source file with

#include <LPC17xx.h>#include <stdlib.h>#include "single_list.h"#define N 100int main( void ) {        // All local variables must be declared here (C89)        int r;        single_list_t list;                SystemInit();        while ( 1 ) {                // If the list is empty, add something to it.                // If the list is full (size == N), remove something from it.                // Otherwise, flip a coin as to whether or not something should                // be added or removed.                switch( list.size ) {                        case 0:                                single_list_push_front( &list, rand() );                                break;                        case N:                                single_list_pop_front( &list );                                break;                        default:                                r = rand();                                if ( r & 1 ) {                                        single_list_push_front( &list, r );                                } else {                                        single_list_pop_front( &list );                                }                }        }}
What's next?

Next, if you are in the laboratory, you may want to create a project that prints to the liquid-crystal dispaly (LCD): see Hello world!.

Alternatively, if you want to see a real-time memory allocator where all allocations and de-allocations have a Θ(1) run time (good for Project 2), see Dynamic memory allocation.





使用特权

评论回复
沙发
小明的同学| | 2024-12-23 14:06 | 只看该作者
我很少用后面那个函数,没主动用过,程序也是运行的好好的啊。

使用特权

评论回复
板凳
一路向北lm| | 2024-12-24 09:16 | 只看该作者
1. 安装和配置MicroLib库
首先,确保你的Keil环境中已经安装了MicroLib库。如果没有安装,你可以从Keil的官方网站或者通过Keil软件安装过程中进行安装。在安装之后,你需要将MicroLib库添加到你的项目中。这通常可以通过在项目设置中添加库文件路径来完成。

2. 使用malloc函数
MicroLib库包含了基础的C库函数,包括malloc用于动态内存分配的函数。你可以在代码中使用malloc来分配动态内存。例如:

int *ptr = malloc(sizeof(int)); // 为一个整数分配内存
if (ptr == NULL) {
    // 内存分配失败的处理
} else {
    // 使用分配的内存
}

使用特权

评论回复
地板
V853| | 2024-12-24 13:45 | 只看该作者
小明的同学 发表于 2024-12-23 14:06
我很少用后面那个函数,没主动用过,程序也是运行的好好的啊。

malloc函数其实用得少也挺好的,不容易出问题。

使用特权

评论回复
5
Amazingxixixi| | 2024-12-27 16:14 | 只看该作者
过来学习学习

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

27

主题

1424

帖子

0

粉丝