binfmt_misc C 脚本接口
Binfmtc – binfmt_misc C scripting interface

原始链接: https://www.netfort.gr.jp/~dancer/software/binfmtc.html.en

Binfmtc 允许 C 程序员使用 C 语言执行通常由 Perl 或 Shell 等语言完成的脚本任务。它解决了创建 makefile 和 C 语言执行所需的其他设置的不便。通过利用 Linux 的 binfmt_misc,binfmtc 可以即时编译和执行 C 脚本。只需在脚本中添加一个神奇的关键字`/*BINFMTC: compile-time options`,赋予其执行权限,binfmtc 就会处理剩下的事情。该软件包包含一个“真正的 csh”示例,演示了用于系统管理任务的 C 风格语法。在基于 Debian 的系统上安装它需要向 `sources.list` 文件添加一个特定的仓库并使用 `apt-get`。该工具旨在实现“C 无处不在”,未来的计划包括支持其他编译器,如 gcj、g77、gpc,以及 C# 等其他语言。

这个Hacker News讨论围绕着`binfmtc`工具展开,该工具允许直接执行C代码作为脚本,绕过传统的编译步骤。最初的帖子重点介绍了一种使用shell脚本技巧(在C文件中嵌入gcc编译)来实现类似效果的替代方法。然而,评论者指出了参数处理方面的潜在问题,并建议使用`tcc`(Tiny C Compiler)作为一种更简单、更快的解决方案,无需中间文件。其他方法包括将编译器二进制文件嵌入C代码本身。讨论深入探讨了这些方法之间的权衡,包括可移植性、代码有效性和对特定shell或编译器的依赖性。最终,该讨论探讨了使C代码更像脚本的各种方法,以方便性为代价牺牲了严格的C标准合规性。
相关文章

原文
binfmtc - binfmt_misc C scripting interface

Introducing the binfmt_misc C scripting interface

"I love C". "I enjoy writing in C". "I don't feel well when I have passed a day without coding a line of C". "My conversation with my wife simply doesn't flow without the C language". "I want to write everything in C, even the everyday scripting, but due to the steps required for writing make files and other things, I tend to choose interpreted languages". A good news for the C programmers suffering from these symptoms. binfmtc is a hack to allow you to use the C language for cases where script languages such as perl and shell were the languages of choice.

Also included is a "real csh" as an example, allowing you to use the C language style for executing everyday sysadmin work. Please experience the real C shell, which has a slightly different tint to the original C shell we've been used to for more than 10 years.

Screenshots

C assembly C++ real csh

Examples of execution in C, assembly, and C++. The last entry is the one for real csh.

Usage

Simply add a magic keyword /*BINFMTC: compile-time options and add execution permission to the C-script. Every time you invoke the script, the compiler will compile and execute the program for you.

How to install

For sid add the following line to /etc/apt/sources.list and do apt-get update; apt-get install binfmtc.

deb http://www.netfort.gr.jp/~dancer/tmp/20050523 ./
    

Mechanics behind it

By registering magic through Linux binfmt_misc, binfmtc-interpreter will be invoked every time a C script is invoked. binfmtc-interpreter will parse the specified script file, and will invoke gcc with the required options, and compile to a temporary binary, and invoke the binary.

Future plans

Do you actually find it, ... useful?

  • gcj support 0.3 done
  • g77 support 0.5 done
  • gpc support 0.5 done
  • gnat support
  • gobjc support
  • chill support
  • mono/pnet c# support
  • use binfmt-support done

References


Junichi Uekawa

$Id: binfmtc.html.en,v 1.11 2006/04/16 03:03:16 dancer Exp $

联系我们 contact @ memedata.com