在编译时有没有办法在C中加载二进制文件作为const变量 [英] Is there a way to load a binary file as a const variable in C at compile time

查看:209
本文介绍了在编译时有没有办法在C中加载二进制文件作为const变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有办法通过包含或头文件或类似的东西加载外部二进制文件作为C中的变量。



例如,在我目前正在开展的一个项目中,我正在使用一个嵌入式系统,该系统具有使用ASCII数据和命令执行文本和小图形(框,线等)的图形显示。但是,它也将显示单色位图。所以我有一系列用于用户界面的静态显示,以及几个用于启动屏幕的位图。



现在我提到它是一个嵌入式系统是没有文件系统从只有RAM和程序存储器加载数据,所以我希望使用的任何预制造数据或表必须通过源文件或通过目标文件在编译时加载,使用链接器。不幸的是,IDE没有提供任何方式将任何形式的二进制文件真正加载到程序存储器中,以便以任何容易识别的方式用作数据缓存。



<如果没有做这些我已经解决的问题(使用十六进制编辑器将二进制文件读取为ASCII编码的十六进制,并将原始数据复制并粘贴到头文件中作为变量),是否有一种链接到一个文件或者包含一个可以在编译时作为一个常量变量加载的文件?



我正在使用的系统是MPLAB X for Microchip系列处理器和编译器是GNC。我主要是想知道是否有办法通过一些C命令或函数来做到这一点,然后再尝试通过特定的编译器/链接器软件寻找一种方法。

解决方案

不,你不能直接做到这一点。一种方法是将二进制文件转换为C源代码,并将这些文件包含到项目中。转换可以通过或某些第三方程序编写的简单程序来完成。



例如:



Binaray1.c (自动生成)

  unsigned char binaray_file1 [] = 
{
1,2,3,10,20,
....
};

Binaray2.c (自动生成)

  unsigned char binaray_file2 [] = 
{
0,0,10,20,45,32,212,
.. ..
};

SomeSourceFile.c

  extern unsigned char binary_file1 []; 
extern unsigned char binary_file2 [];

//这里使用binary_file1和binary_file2。


I was wondering if there is a way to load an external binary file as a variable in C through an include or a header file or something of the like.

For example, in a project I am currently working on I am working with an embedded system that has a graphic display that will do text and minor graphics (boxes, lines, etc.) using ASCII data and commands. But, it will also display monochrome bitmaps. So I have a series of static displays that I use for a user interface, and a couple of bitmaps for splash screens.

Now the reason I mention that it is an embedded system is that there is no file system to load data from, only RAM and program memory, so any "pre-fab" data or tables I wish to use must but loaded at compile time either through a source file or through an object file using the linker. Unfortunately, the IDE does not provide any means to load a binary, in any form really, into program memory for use as a data cache in this fashion in any readily recognizable fashion.

So short of doing what I already have to get around this, (use a hex editor to read the binary as ASCII encoded hex and copy and paste the raw data into a header file as a variable), is there a way to "link" to a file or "include" a file that could be loaded as a const variable at compile time?

The system I am working with is MPLAB X for the Microchip series of processors and the compiler is GNC. I am mainly looking to know if there is a way to do this through some C command or function before I go trying to look for a way specifically through their specific compiler/linker software.

解决方案

No you can't do this directly. One way is to convert your binary files into C source code and include these pieces into your project. The conversion can be done by a simple program written by you or by some third party program.

For example:

Binaray1.c (generated automatically)

unsigned char binaray_file1[] =
{
  1,2,3,10,20,
  ....
} ;

Binaray2.c (generated automatically)

unsigned char binaray_file2[] =
{
  0,0,10,20,45,32,212,
  ....
} ;

SomeSourceFile.c

extern unsigned char binary_file1[] ;
extern unsigned char binary_file2[] ;

// use binary_file1 and binary_file2 here.

这篇关于在编译时有没有办法在C中加载二进制文件作为const变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆