.lib
)步骤(以 STC8H1K08T为例)Keil C51 是 Keil 针对 8051 微控制器(如 STC8 系列)的开发工具,与 Keil MDK(针对 ARM Cortex)不同。Keil C51 使用 C51 编译器(而不是 ARM Compiler),其生成库文件的步骤和输出格式也有所区别。在 Keil C51 中,生成的库文件扩展名是 .lib
(不是 .a
),并且仅适用于 Keil C51 环境,不兼容 GCC 或其他工具链。以下是以 STC8H1K08T(8051 架构芯片)为例,详细说明如何在 Keil C51 中生成 .lib
文件。
C:\Keil_v5\C51\BIN
)。.lib
文件,隐藏源代码。Help -> About μVision
查看版本。全部文件:
core.h
): // core.h
#ifndef _CIRCULAR_BUFFER_H_
#define _CIRCULAR_BUFFER_H_
#ifndef NULL
#define NULL ((void*)0)
#endif
// Structure to manage circular buffer data
typedef struct {
unsigned int* buffer; // Pointer to dynamically allocated buffer storage
unsigned long int sum; // Sum of all values in the buffer
unsigned char index; // Index of the oldest value to be replaced
unsigned char count; // Number of values currently stored (max: BUFFER_SIZE)
unsigned char size; // Actual buffer size
} CircularBuffer;
// Initializes the circular buffer with a given storage array and size
void init_circular_buffer(CircularBuffer* cb, unsigned int* storage, unsigned char size);
// Updates the buffer with a new value, maintaining the sliding average
void update_circular_buffer(CircularBuffer* cb, unsigned int new_value);
// Computes the average of the values currently in the buffer
unsigned int get_circular_buffer_average(CircularBuffer* cb);
#endif // _CIRCULAR_BUFFER_H_
D:\Library\core.h
。
unsigned char
等小数据类型。
core.c
):
// core.c
#include "core.h"
// Initializes the circular buffer with a given storage array and size
void init_circular_buffer(CircularBuffer* cb, unsigned int* storage, unsigned char size)
{
unsigned char i = 0;
if (cb == NULL || storage == NULL || size == 0) return;
cb->buffer = storage;
cb->sum = 0;
cb->index = 0;
cb->count = 0;
cb->size = size;
// Initialize buffer values to zero
for (i = 0; i < size; i++) {
cb->buffer[i] = 0;
}
}
// Updates the circular buffer with a new value
void update_circular_buffer(CircularBuffer* cb, unsigned int new_value) {
if (cb == NULL || cb->buffer == NULL) return;
// If the buffer is full, subtract the oldest value from sum
if (cb->count >= cb->size) {
cb->sum -= cb->buffer[cb->index];
}
else {
cb->count++;
}
// Add the new value to the buffer and update sum
cb->sum += new_value;
cb->buffer[cb->index] = new_value;
// Update index in a circular manner
cb->index = (cb->index + 1) % cb->size;
}
// Computes the average of the values stored in the buffer
unsigned int get_circular_buffer_average(CircularBuffer* cb) {
if (cb == NULL || cb->count == 0) return 0;
return cb->sum / cb->count;
}
D:\Library\core.c
。keil 全部文件:
新建项目:
Project -> New μVision Project
。D:\Library
。core.uvproj
(C51 使用 .uvproj
,不是 .uvprojx
)。Device
列表中,找到 STC 系列(如 STC8H1K08T
)。添加源文件:
Source Group 1
-> Add Existing Files to Group
。D:\Library\core.c
。配置项目为生成库:
Target 1
-> Options for Target
。
Memory Model
:选择 Small
(8051 常用,视需求可改 Large
)。Code Rom Size
:选择 Large: 64K Program
(STC8 支持)。D:\Library
(确保找到 core.h
)。Generate Debug Information
(保护代码)。Create Executable
。Create Library
。libcore
(生成 libcore.lib
)。编译生成 .lib
文件:
Build
(F7)或 Project -> Build Target
。D:\Library\Objects\core.lib
。命令行方式(可选):
cd D:\Library
"C:\Keil_v5\UV4\UV4.exe" -b core.uvproj -o build_log.txt
D:\Library\Objects\core.lib
。STC8H1K08T.PD3
)。C:\Keil_v5\C51\LIB
或通过 Project -> Manage -> Components
添加。.lib
(Keil C51 专有格式)。LX51
)。.a
或 .lib
不兼容。nm
检查,需依赖 Keil 环境。Options for Target -> C51
-> 取消勾选 Generate Debug Information
。D:\Library\core.h
D:\Library\Objects\core.lib
Large
模型,但需确保代码和数据空间分配合理。.lib
仅限 Keil C51 用户,若需支持 SDCC,需用 SDCC 重新生成。core.h
在用户项目可找到的路径。core.c
,配置为 Create Library
。libcore.lib
。.lib
,不是 .a
。.lib
)有了更深入的理解和认识。