简体   繁体   English

使用预编译器进行字符串连接

[英]String Concatenation using Precompiler

I have the following problem. 我有以下问题。 I want to include a multi-line textfile as #define , ie what I need is a constant that stores the value of the text file. 我想包含一个多行文本文件作为#define ,即我需要一个存储文本文件值的常量。 Example: 例:

File 'f.txt': This\nis\n\nsome\ntext

and I want to initialize a constant (at compile time) in the style of 我想以()的样式初始化常量(在编译时)

#define txtfile "This\\nis\\na\\ntextfile"

where the string "This\\nis\\na\\ntextfile" is obtained from concatenating the lines in file f.txt. 通过串联文件f.txt中的行获得字符串“ This \\ nis \\ na \\ ntextfile”。 Is there any way to achieve this using preprocessor commands/macros? 有什么方法可以使用预处理器命令/宏来实现这一目标吗?

看一下C程序中以char []形式出现的文本文件“ #include” ,这可能会满足您的需求。

This isn't directly possible, as the textfile needs processing first. 这不是直接可能的,因为文本文件需要首先处理。 You could write a fairly simple script that performed the appropriate escaping and added the #define creating a suitable header file. 您可以编写一个相当简单的脚本来执行适当的转义,并添加#define创建合适的头文件。

Typically you don't actually need the text file as a preprocessor macro expansion, though, you need the file data to appear in the object file and to be accessible as though it where an extern char[] . 通常,实际上,您实际上不需要文本文件作为预处理器宏扩展,但是您需要文件数据出现在目标文件中并且可以像访问extern char[]一样进行访问。

To do this there are two approaches. 为此,有两种方法。 The lightweight way is to use an assembler like yasm with an incbin directive to produce an object file which has the file data as a labelled section. 轻量级的方法是使用像yasm这样的汇编器和incbin指令来生成目标文件,该目标文件的文件数据作为标记的部分。 eg: 例如:

    global f_txt
f_txt:
    incbin "f.txt"

Then in the C file you can declare: 然后在C文件中可以声明:

extern char f_txt[];

The more portable way is to use a utility like xxd -i to convert the data into an C file with the char array written out 'long hand'. 更可移植的方式是使用诸如xxd -i类的实用程序将数据转换为C文件,并且将char数组写为“ long hand”。

What you're probably going to have to do is have a separate pre-processor program that takes your input file ( f.txt ) and a code section in your special source file ( program.cppx ) like: 您可能需要做的是有一个单独的预处理程序,该程序将输入文件( f.txt )和特殊源文件( program.cppx )中的代码段如下:

#superdefine txtfile f.txt

The pre-pre-processor you'll have to write yourself but it'll basically replace the #superdefine lines with the equivalent #define lines based on the file content, producing a file program.cpp which you can then feed into the real compiler. 您必须编写自己的预处理器,但是基本上会根据文件内容用等效的#define行替换#superdefine行,从而生成文件program.cpp ,然后您可以将其馈送到真正的编译器中。

This is quite easy to achieve under UNIXes if you're using a make variant. 如果使用make变体,这在UNIX下非常容易实现。 May not be so easy using an IDE. 使用IDE可能并不容易。

One might come up with an idea like the following: 一个人可能会提出以下想法:

f.txt: f.txt:

This \
is \
some \
text \

code.c: code.c:

#define txtfile "\
#include "f.txt"
"

But I don't think this would work. 但是我认为这不会起作用。 The include line would just be regarded as a String. 包含行将仅被视为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM