Hello everyone,

I would like to know if and how Nim could be used as a C replacement to create 
native dynamic system libraries which can be distributed and used like any 
library written in C. This is where all the supposed C replacements like Rust 
or Go fall flat. Only C++ so far seems suitable for that, but it's... well, C++.

Here is what I would like:

  * Write a library in Nim
  * Compile to a `.so` file, a DLL of whatever
  * Generate the accompanying header file for distribution



I have been able to accomplish the first two points. Here is the Nim source 
file:
    
    
    # src/add5.nim
    proc add5*(b: int): int {.exportc, dynlib.} =
       b + 5
    
    Run

I compiled it as `nim c -d:release --noMain --header:a.h --app:lib -o:lib/a.so 
src/add5.nim`, this gave me the dynamic library which I was able to load into a 
Python script:
    
    
    from ctypes import cdll
    
    a = cdll.LoadLibrary('./lib/a.so')
    x = a.add5(4)
    print(x)
    
    Run

This works, but in order to make it properly distributable I need the header 
file. The `--header:a.h` option does create a header file in 
`~/.cache/nim/add5_r`, but this file looks like an intermediate build artifact, 
not like something that can be distributed to `#include` in a C project.
    
    
    /* Generated by Nim Compiler v1.6.0 */
    #ifndef __a__
    #define __a__
    #define NIM_INTBITS 64
    #define NIM_EmulateOverflowChecks
    
    #include "nimbase.h"
    #undef LANGUAGE_C
    #undef MIPSEB
    #undef MIPSEL
    #undef PPC
    #undef R3000
    #undef R4000
    #undef i386
    #undef linux
    #undef mips
    #undef near
    #undef far
    #undef powerpc
    #undef unix
    N_LIB_PRIVATE N_NOCONV(void, signalHandler)(int sign);
    N_LIB_PRIVATE N_NIMCALL(NI, getRefcount)(void* p);
    N_LIB_IMPORT N_CDECL(NI, add5)(NI b);
    N_LIB_IMPORT N_CDECL(void, NimMain)(void);
    #endif /* __a__ */
    
    Run

In particular, the included header file `nimbase.h` is stored under 
`/usr/lib/nim/lib/nimbase.h`, which is not a regular include path.

So my question is, is it possible to use Nim to create independent standalone 
dynamic C libraries? And if not, can this be added, or would such a feature be 
considered out of scope? 

Reply via email to