Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sure, you put this in one of your your header files, say preinit.h:</p> <pre><code>class Init { public: Init() { initialize(); } }; Init init; </code></pre> <p>and then, in <em>one</em> of your compilation units, put:</p> <pre><code>void initialize(void) { // weave your magic here. } #include "preinit.h" </code></pre> <p>I know that's a kludge but I'm not aware of <em>any</em> portable way to do pre-main initialization without using a class constructor executed at file scope.</p> <p>You should also be careful of including more than one of these initialization functions since I don't believe C++ dictates the order - it could be random.</p> <p>I'm not sure of this "sketch" of which you speak but would it be possible to transform the main compilation unit with a script before having it passed to the compiler, something like:</p> <pre><code>awk '{print;if (substr($0,0,11) == "int main (") {print "initialize();"};}' </code></pre> <p>You can see how this would affect your program because:</p> <pre><code>echo '#include &lt;stdio.h&gt; int main (void) { int x = 1; return 0; }' | awk '{ print; if (substr($0,0,11) == "int main (") { print " initialize();" } }' </code></pre> <p>generates the following with the <code>initialize()</code> call added:</p> <pre><code>#include &lt;stdio.h&gt; int main (void) { initialize(); int x = 1; return 0; } </code></pre> <p>It may be that you can't post-process the generated file in which case you should ignore that final option, but that's what I'd be looking at first.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload