Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As of ghc-7, the default mode is <code>--make</code>. You want to create a library, so you have to tell GHC with the <code>-c</code> flag. You don't need the <code>-no-hs-main</code> then.</p> <pre><code> ghc -c TestLib.hs -o test.o </code></pre> <p>works.</p> <p>An example:</p> <h3>clib.h:</h3> <pre class="lang-c prettyprint-override"><code>int doTest(void); </code></pre> <h3>clib.c:</h3> <pre class="lang-c prettyprint-override"><code>#include "clib.h" int doTest(void){ return 42; } </code></pre> <h3>TestLib.hs:</h3> <pre><code>{-# LANGUAGE ForeignFunctionInterface #-} module TestLib where foreign export ccall test_me :: IO (Int) foreign import ccall "clib.h" doTest :: IO ( Int ) test_me = doTest </code></pre> <h3>libtest.c:</h3> <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt; #include "TestLib_stub.h" int main(int argc, char *argv[]) { hs_init(&amp;argc, &amp;argv); printf("%d\n", test_me()); hs_exit(); return 0; } </code></pre> <h3>Compilation and execution:</h3> <pre><code>$ ghc -c -o clib.o clib.c $ ar -r -s libclib.a clib.o ar: creating libclib.a $ ghc TestLib.hs -c -o tlib.o $ ar -r -s libtlib.a tlib.o ar: creating libtlib.a $ ghc -o nltest libtest.c -no-hs-main -L. -ltlib -lclib $ ./nltest 42 </code></pre> <p>Note: That works as such with ghc >= 7.2; for ghc-7.0.*, you must also compile the generated <code>TestLib_stub.c</code> file and link with <code>TestLib_stub.o</code>.</p> <p>The important point is to tell ghc <strong>not</strong> to link when creating the libraries, only when finally the executable is created.</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