我有兴趣使用除了 C 预处理器以外的东西来预处理我的C和Objective-C源代码。是否有好的替代品?
例如,有些工具能够在C代码中间跳出到Python或Perl代码片段,并将输出的C代码编译成正常的代码。
我有兴趣使用除了 C 预处理器以外的东西来预处理我的C和Objective-C源代码。是否有好的替代品?
例如,有些工具能够在C代码中间跳出到Python或Perl代码片段,并将输出的C代码编译成正常的代码。
<?
和?>
在标准C中不被使用(使用非标准C时,唯一会出问题的是旧版GCC扩展运算符返回min/max)但对于严肃的用途,让PHP打印#line指令以调试预处理代码是必要的。
<?php include_once "stdio.h"; ?>
int main()
{
<?php
for($i = 0; $i < 20; $i++)
echo 'printf("%d\n", '.$i.');';
?>
}
.h
文件。 - Star Brillianttry.php
,然后在终端中运行php -d include_path=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ try.php > try.c
。 - undefinedCog 并不是一个预处理器,但它可以内嵌在代码中,并且动态生成一些内容。
你可能需要考虑使用m4。
http://www.gnu.org/software/m4/
m4
真的非常灵活,但语法太过艰深。我真希望我们有一个更现代化的Unix宏处理器,不过最好是像备受尊敬的 m4
那样由C编译而成。 - J. M. Beckernames = { 'John', 'Paul', 'George', 'Ringo' }
local prog = [===[
#include <stdio.h>
main() {
$(local out = { }
for _, n in ipairs(names) do
table.insert(out, string.format([[ printf("The name is %%s\n", %q);]], n))
end
return table.concat(out, '\n ')
)
}
]===]
local function antiquote(s)
local body = s:match '^%$%((.*)%)$'
return assert(loadstring(body))()
end
prog = prog:gsub('%$%b()', antiquote)
io.stdout:write(prog)
: nr@curlycoat 1181 ; lua /home/nr/tmp/emit-c.lua
#include <stdio.h>
main() {
printf("The name is %s\n", "John");
printf("The name is %s\n", "Paul");
printf("The name is %s\n", "George");
printf("The name is %s\n", "Ringo");
}
<?
my @types = ('char', 'int', 'long');
foreach (@types) {
?>
<?= $_ ?> read_<?= uc($_) ?>(<?= $_ ?>* v);
<? } ?>
变成这样
char read_CHAR(char* v);
int read_INT(int* v);
long read_LONG(long* v);
语法类似于PHP,但是它使用的是Perl,并且可以将文本捕获到Perl字符串中。
编辑者:cxw — 在@d-ash的批准下,我也是perlpp的维护人员。如果您有问题,请随时联系我!
我曾经思考过这个问题。确保你能接受这样一个事实,那就是任何想要编译你的代码的人都需要新的预处理工具。如果只有你一个人会使用它,那么没有问题,但是如果你想让代码对其他人可用,那么你可能需要考虑是否添加工具要求是一个好主意。
./configure
?所有用于重新生成的源代码都在那里,但你没有必要去追寻正确版本的automake/autoconf。所以你所需要做的就是提供原始代码和已经处理过的版本。 - J. M. Becker