title: nim: Use a static library
body: <p>I've tried to get an audio library statically linked to my program. I use <a href="https://github.com/zacharycarter/soloud-nim" rel="nofollow noreferrer">this</a> nimble package. To get it run, i had to build the soloud library as described <a href="http://sol.gfxile.net/soloud/premake.html" rel="nofollow noreferrer">here</a>. For short after download i ran &quot;genie --with-miniaudio-only --platform=x64 vs2017&quot; in the &quot;build&quot; folder and got the source code to generate the dynamic and the static library. For now i can run the following demo program from the nimble package with the generated dll alongside:</p>
  <pre><code>import solouddotnim, times, os
  
  var i, spin = 0
  
  var sl : ptr Soloud
  
  sl = Soloud_create()
  
  discard Soloud_init(sl)
  
  Soloud_setGlobalVolume(sl, 1)
  
  
  var stream = WavStream_create()
  discard WavStream_load(cast[ptr Wav](stream), &quot;test.ogg&quot;)
  
  let currentTime = epochTime()
  let length = WavStream_getLength(stream)
  discard Soloud_play(cast[ptr Soloud](sl), cast[ptr Wav](stream))
  
  while epochTime() - currentTime &lt;= length:
    sleep(100)
  
  Soloud_deinit(sl)
  
  Soloud_destroy(sl)
  </code></pre>
  <p>Now to the static-link part. In the solouddotnim.nim file of the nimble package i use, i see this part:</p>
  <pre><code>when defined(windows):
    const
      libname* = &quot;libsoloud.dll&quot;
  elif ...
  </code></pre>
  <p>So i simple changed the windows part to the following, re-installed the nimble-package and placed the &quot;soloud_static_x64.lib&quot; alongside to the &quot;main.nim&quot; of the testproject:</p>
  <pre><code>when defined(windows):
    const
      libname* = &quot;soloud_static_x64.lib&quot;
  elif ...
  </code></pre>
  <p>But this doesent make it. (cant open &quot;soloud_static_x64.lib&quot; error when build)
  Evereywhere where the constant &quot;libname&quot; is used there are the pragmas &quot;cdecl&quot;, &quot;importc&quot; and &quot;dynlib&quot;. For example:</p>
  <pre><code>proc Soloud_create*(): ptr Soloud {.cdecl, importc: &quot;Soloud_create&quot;, dynlib: libname.}
  </code></pre>
  <p>So &quot;dynlib&quot; is telling nim to use a dll on windows. But was is the pragma for static libraries?
  In the nim documentations i only found <a href="https://nim-lang.org/docs/nimc.html#dynliboverride" rel="nofollow noreferrer">DynlibOverride</a> to link to static libraries, but i dont understand the example and here is where i stuck. I've tried the followings:</p>
  <pre><code>nim c --dynlibOverride:libname --passL:soloud_static_x64.lib &quot;examples\00-ogg\Example00_ogg.nim&quot;
  
  nim c --dynlibOverride:soloudtotnim --passL:soloud_static_x64.lib &quot;examples\00-ogg\Example00_ogg.nim&quot;
  </code></pre>
  <p>Firstly i dont know what parameter dynlibOverride expects and secondly both compiles, but dont work. It expects a dynamic library alongside the exe.
  My last try was to remove all dynlib pragmas from the nimble package. But now i cant compile it.</p>
  <pre><code>undefined reference to `Soloud_create'
  ...
  Error: execution of an external program failed: 'gcc.exe...
  </code></pre>
  <p>My knowlege ends here. Can someone help me?
  Thanks in advance.</p>
  <p><strong>Edit:</strong>
  I could not get any of your solutions work. I break down the problem as small as possible so everybody can reproduce this:
  &quot;foo.nim&quot; contains this:</p>
  <pre><code>proc add*(a, b: int): int {.cdecl, exportc.} = 
      a + b
  proc sub*(a, b: int): int {.cdecl, exportc.} = 
      a - b
  </code></pre>
  <p>The .lib is simply generated with this command: &quot;nim c --app:staticlib foo.nim&quot;</p>
  <p>Now to use it i created a file &quot;main.nim&quot; with this content:</p>
  <pre><code>{.passL:&quot;foo.lib&quot;.}
  proc add*(a, b: int):int {.cdecl, importc.}
  proc sub*(a, b: int):int {.cdecl, importc.}
  
  echo add(10, 5)
  echo sub(10, 5)
  </code></pre>
  <p>if i simply build it with &quot;nim c -r main.nim&quot;, i get the following output and  error:</p>
  <pre><code>P:\Nim\LearnCBinding&gt;nim c -r main.nim
  Hint: used config file 'C:\nim-1.5.1\config\nim.cfg' [Conf]    
  Hint: used config file 'C:\nim-1.5.1\config\config.nims' [Conf]
  ....CC: stdlib_io.nim
  CC: stdlib_system.nim
  CC: main.nim
  
  Hint:  [Link]
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x1f6): multiple definition of `PreMainInner'     
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x120): first defined here
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x20a): multiple definition of `PreMain'
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x134): first defined here
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x240): multiple definition of `NimMainInner'     
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x16f): first defined here
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x254): multiple definition of `NimMain'
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x183): first defined here
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x285): multiple definition of `main'
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x1b4): first defined here
  foo.lib(@mfoo.nim.c.o):@mfoo.nim.c:(.text+0x2da): multiple definition of `NimMainModule'
  C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o:@mmain.nim.c:(.text+0x209): first defined here
  collect2.exe: error: ld returned 1 exit status
  Error: execution of an external program failed: 'C:\nim-1.5.1\dist\mingw64\bin\gcc.exe   -o P:\Nim\LearnCBinding\main.exe  C:\Users\Peter\nimcache\main_d\stdlib_io.nim.c.o C:\Users\Peter\nimcache\main_d\stdlib_system.nim.c.o C:\Users\Peter\nimcache\main_d\@mmain.nim.c.o  foo.lib   '
  </code></pre>
  <p>Because of the multiple definition error i also tried to build foo.lib with parameter &quot;--noMain:on&quot;, but it doesnt make any difference.</p>
  <p>Do you have the same problem? By the way i use the current version of Nim &quot;nim-1.5.1&quot; and reinstalled MingW with the finish.exe from nim.</p>