在编译时将文件读入字符串 [英] Read a file into a string at compile-time

查看:34
本文介绍了在编译时将文件读入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个文件中写一些东西(我们称之为foo.cpp)并将它作为一个字符串包含在我的程序中在编译时,类似于 #include 的方式.

I would like to write something in a file (let's call it foo.cpp) and include it as a string into my program at compile time, similar to the way #include does it.

现在我正在使用这个 C 预处理器 #define:

Right now I'm using this C preprocessor #define:

#define toString(src) #src

将一堆代码转换为字符串,如本例所示:

to convert a bunch of code to a string, used as in this example:

const char* str = toString(
  int x;
  void main(){}
);

如果您愿意,可以在那里阅读宏字符串化.

You can read about macro stringification there if you want.

我想将该代码移动到一个外部文件,该文件将在编译时链接".我不希望文件必须与程序一起分发,如果我要在运行时读取它就会出现这种情况.

I would like to move that code to an external file, which would be "linked" at compile-time. I don't want the file to have to be distributed with the program, which would be the case if I were to read it a run-time.

我尝试使用如下所示的 #include 指令,但编译器拒绝了它:

I tried to use an #include directive as shown below, but the compiler refused it:

const char* str = toString(
#include "foo.cpp"
);

g++ 似乎完全糊涂了,但是 clang++ 给了我这个错误:

g++ seems to be completely confused, but clang++ gave me this error:

error: embedding a #include directive within macro arguments is not supported

有谁知道是否/如何做到这一点?

Does anyone know if/how this can be done?

注意:我正在使用它来编写我的 GLSL 着色器,尽管我怀疑这些信息是否有用.

PS:在你告诉我之前这是 这个问题,将我的代码放在自己文件中的巨大字符串中或使用外部工具(例如xxd)转储其十六进制表示不是解决方案"对我来说,因为它们并不比我目前的方法更好(即更容易/更清洁).

PS: Before you tell me this is a duplicate of this question, putting my code in a giant string in its own file or using an external tool (eg. xxd) to dump its hex representation are not "solutions" for me, as they are not better (ie. easier/cleaner) than my current method.

几年后更新:
我刚刚意识到我从来没有回答这个问题,因为它被关闭为重复.当我看到 this commit 时,我找到了我正在寻找的答案,它本身基于 对这篇文章的评论,并且一直在使用它.

Update, a few years later:
I just realized I never got to answer this question as it was closed as duplicate. I found the answer I was looking for when I saw this commit, itself based on a comment on this article, and have been using it ever since.

简而言之,一个小的汇编文件包含您想要的文件,并在给定的 NAME 下公开它们,其中包含三个变量 NAME_beginNAME_endNAME_len 允许您从 C 代码访问它们的内容.

In a nutshell, a small assembly file includes the files you want and exposes them under a given NAME, with the three variables NAME_begin, NAME_end and NAME_len allowing you to access their contents from your C code.

这样,你就有了一个只包含你想要的代码的普通文件,它会在编译时自动读取,而不必在运行时读取或跳过 xxd 循环.

This way, you have a normal file containing nothing but the code you want, and it gets read automatically at compile time, instead of having to read it at runtime or having to jump through xxd hoops.

推荐答案

我不太确定您要完成什么,但 Linux 命令行实用程序 xxd 可能是您正在寻找的:

I am not quite sure what you are trying to accomplish but but Linux command line utility xxd may be what you are looking for:

xxd -i [filename]

将生成一个 C 样式的头文件,其中包含一个数组,其中您的文件内容采用完全二进制编码和一个变量及其长度.

will generate a C style header file containing an array with your file contents in full binary encoding and a variable with its length.

例子:

xxd -i /proc/cpuinfo

制作一个文件

unsigned char _proc_cpuinfo[] = {
  0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x6f, 0x72, 0x09, 0x3a, 0x20,
  0x30, 0x0a, 0x76, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x09,
...
};
unsigned int _proc_cpuinfo_len = 654390;

您可以在代码中包含生成的标头,并通过这些变量访问数组和文件长度.

You can include the resulting header in your code and access the array and file length via those variables.

这篇关于在编译时将文件读入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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