TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

A tiny self-remaking C program

119 点作者 goranmoomin9 个月前

14 条评论

d99kris9 个月前
I do something similar (but less portable and more verbose) in C++ sometimes when I want to prototype something. My boilerplate is something like this:<p><pre><code> #if 0 TMP=$(mktemp -d); c++ -std=c++11 -o ${TMP}&#x2F;a.out ${0} &amp;&amp; ${TMP}&#x2F;a.out ${@:1}; RV=${?}; rm -rf ${TMP}; exit ${RV}; #endif #include &lt;iostream&gt; int main() { std::cout &lt;&lt; &quot;Hello, world!\n&quot;; } </code></pre> (the trailing semi-colons in the script part is to make my editor indent the C++ code properly)
评论 #41487643 未加载
评论 #41489837 未加载
评论 #41491804 未加载
seanw2659 个月前
Based on the title, I was expecting this to be about quines [1].<p>If you aren&#x27;t familiar, quines are programs which produce their own source as their only output. They&#x27;re quite interesting and worth a dive if you haven&#x27;t explored them before.<p>My personal favorites are the radiation-hardened variety, which still produce the original pre-modified source even when any single character is removed before the program is run.<p>[1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Quine_(computing)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Quine_(computing)</a>
评论 #41490378 未加载
评论 #41491973 未加载
codethief9 个月前
&gt; the right conceptual basis for build systems: ”build is the first stage of execution”<p>I have long been thinking the same. And also: &quot;Running tests is the first stage of deploying in production&quot; etc.<p>In other words: There is often a whole dependency graph between various stages (install toolchain, install 3rd-party dependencies, do code generation, compile, link&#x2F;bundle, test, run, …) and each of those stages should ideally be a pure function mapping inputs to outputs. In my experience, we often don&#x27;t do a good job making this graph explicit, nor do we usually implement build steps as pure functions or think of build systems as another piece of software which needs to be subject to the same quality criteria that we apply to any other line of code we write. As a result, developer experience ends up suffering.<p>Large JavaScript projects are particularly bad in this regard. Dependencies, auto-generated code and build output live right alongside source code, essentially making them global state from the point of view of the build system. The package.json contains dozens of &quot;run&quot; commands which more often than not are an arcane mix of bash scripts invoking JS code. Even worse, those commands typically need to be run in juuust the right order because they all operate on the same global state. There is no notion of one command depending on another. No effort put into isolating build tasks from each other and making them pure. No caching of intermediate results. Pipelines take ages even though they wouldn&#x27;t have to. Developers get confused because a coworker introduced a new npm command yesterday which now needs be to run before anything else. Ugghhh.
评论 #41489160 未加载
评论 #41489861 未加载
actionfromafar9 个月前
&quot;TCC can also be used to make C scripts, i.e. pieces of C source that you run as a Perl or Python script. Compilation is so fast that your script will be as fast as if it was an executable.&quot;<p><a href="https:&#x2F;&#x2F;bellard.org&#x2F;tcc&#x2F;tcc-doc.html" rel="nofollow">https:&#x2F;&#x2F;bellard.org&#x2F;tcc&#x2F;tcc-doc.html</a>
评论 #41488389 未加载
LorenDB9 个月前
In D, this is a fully-supported used case. The DMD compiler provides an executable called rdmd that can be used as the shebang executable at the top of any D file, and the shebang itself is also codified as valid D syntax.
评论 #41489018 未加载
rwmj9 个月前
This is completely off topic for the actual article, but I had a bit of fun a while back working out how to make self-executing C and OCaml scripts&#x2F;programs.<p>It&#x27;s an interesting exercise working out what is both a comment in the target language but is also an executable shell script. For C it&#x27;s reasonably straightforward but for OCaml it&#x27;s quite subtle:<p><a href="https:&#x2F;&#x2F;libguestfs.org&#x2F;nbdkit-cc-plugin.3.html#C-plugin-as-a-self-contained-script" rel="nofollow">https:&#x2F;&#x2F;libguestfs.org&#x2F;nbdkit-cc-plugin.3.html#C-plugin-as-a...</a> <a href="https:&#x2F;&#x2F;libguestfs.org&#x2F;nbdkit-cc-plugin.3.html#Using-this-plugin-with-OCaml" rel="nofollow">https:&#x2F;&#x2F;libguestfs.org&#x2F;nbdkit-cc-plugin.3.html#Using-this-pl...</a>
lboc9 个月前
Seems to be replicating mainframe JCL and in-stream data sets. Processing instructions and input are combined in a single file. Used all the time for compiling, running utilities etc.<p>I&#x27;m guessing that this (IBM) example is setting the delimeter to &#x27;@@&#x27; to avoid problems with the comment - JCL also understands the &#x27;&#x2F;*&#x27; sequence. I&#x27;ve not seen it used with other languages (Cobol etc.)<p><pre><code> &#x2F;&#x2F;jobname JOB acctno,name... &#x2F;&#x2F;COMPILE EXEC PGM=CCNDRVR, &#x2F;&#x2F; PARM=&#x27;&#x2F;SEARCH(&#x27;&#x27;CEE.SCEEH.+&#x27;&#x27;) NOOPT SO OBJ&#x27; &#x2F;&#x2F;STEPLIB DD DSNAME=CEE.SCEERUN,DISP=SHR &#x2F;&#x2F; DD DSNAME=CEE.SCEERUN2,DISP=SHR &#x2F;&#x2F; DD DSNAME=CBC.SCCNCMP,DISP=SHR &#x2F;&#x2F;SYSLIN DD DSNAME=MYID.MYPROG.OBJ(MEMBER),DISP=SHR &#x2F;&#x2F;SYSPRINT DD SYSOUT=* &#x2F;&#x2F;SYSIN DD DATA,DLM=@@ #include &lt;stdio.h&gt; ⋮ int main(void) { &#x2F;* comment *&#x2F; ⋮ } @@ &#x2F;&#x2F;SYSUT1 DD DSN=... ⋮ &#x2F;&#x2F;* </code></pre> <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Job_Control_Language#In-stream_input" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Job_Control_Language#In-stream...</a>*
necovek9 个月前
The way I see it, this is something a true built-from-source system could do with their packaging system to enable no-effort code changes for any system utility and true trust in you running what you have source for (other than backdoored hardware).<p>Debian is pretty far off from this vision (if we also want performant execution), but I wonder how do the Gentoo, ArchLinux and Nix fare in this regard? Is this something that could be viably built with their current packaging formats?
评论 #41488381 未加载
o11c9 个月前
Sometimes it&#x27;s useful to do something like:<p><pre><code> &#x2F;*usr&#x2F;bin&#x2F;env echo &#x27;Hello World!&#x27; #*&#x2F; </code></pre> Even if, for some reason, there are multiple `usr` folders, the use of `env` means it will eventually call the executable.<p>As for getting rid of the shebang - swapping the ! with a &#x2F; means that the line and character counts don&#x27;t change so you get meaningful error messages.
评论 #41497242 未加载
评论 #41492308 未加载
hippich9 个月前
Somewhat adjacent- I recently discovered <a href="https:&#x2F;&#x2F;github.com&#x2F;rofl0r&#x2F;rcb2">https:&#x2F;&#x2F;github.com&#x2F;rofl0r&#x2F;rcb2</a> - it can take it quite far without using make file. And similarly to OP - it allows to keep relevant build info right in the source code. (Rcb2 is great at prototype stage, but obviously at some point makefiles are worth spending time on)
slippy9 个月前
Didn&#x27;t this exist conceptually anyway as the C shell (csh) where the scripting language was &quot;closer to&quot; C?<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C_shell" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C_shell</a><p>It seems like you are on your way to making the C++ shell.
评论 #41489436 未加载
trollied9 个月前
You might all enjoy The International Obfuscated C Code Contest <a href="https:&#x2F;&#x2F;www.ioccc.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.ioccc.org&#x2F;</a><p><a href="https:&#x2F;&#x2F;www.ioccc.org&#x2F;years.html" rel="nofollow">https:&#x2F;&#x2F;www.ioccc.org&#x2F;years.html</a>
评论 #41487949 未加载
评论 #41487876 未加载
dorianmariefr9 个月前
<p><pre><code> &quot;$0&quot;.bin: -c: line 0: unexpected EOF while looking for matching `&#x27;&#x27; &quot;$0&quot;.bin: -c: line 1: syntax error: unexpected end of file</code></pre>
z4ziggy8 个月前
&#x2F;&#x2F;bin&#x2F;env gcc $0 -g -o ${0%.<i>} &amp;&amp; .&#x2F;${0%.</i>} ; exit<p>thats the one i&#x27;ve been using. feel free to adopt and change.