I haven't fully reproduced your error, but I've faced with similar problems in 
the <https://forum.nim-lang.org/t/8352#53847> \- that is, I need to somehow 
link with the C++ library. From what I can understand, `dynlib` does not 
properly support C++ name mangling, or it does so in some weird manner that I 
can't understand.
    
    
    class WithConstructor
    {
      public:
        WithConstructor(int arg);
        WithConstructor(int arg1, int arg2);
    };
    
    
    Run
    
    
    #include "lib.hpp"
    #include <iostream>
    
    WithConstructor::WithConstructor(int arg) {
        std::cout << "Called with constructor, single argument\n";
    }
    
    
    WithConstructor::WithConstructor(int arg1, int arg2) {
        std::cout << "Called with constructor, two arguments\n";
    }
    
    
    Run

wrapped using
    
    
    const
      h = "lib.hpp"
      l = "liblib.so"
    
    type
      WithConstructor {.importcpp: "WithConstructor", header: h.} = object
    
    proc newWithConstructor(arg: cint): WithConstructor {.
      importcpp: "WithConstructor(@)", constructor, cdecl.}
    
    proc newWithConstructor(arg: cint, arg2: cint): WithConstructor {.
      importcpp: "WithConstructor(@)", constructor, cdecl, dynlib: l.}
    
    proc main() =
      let val1 = newWithConstructor(20)
      let val2 = newWithConstructor(20, 30)
    
    main()
    
    
    Run

generates proper constructor calls if compiled with `nim r --backend:cpp 
--passl:-llib nim_cxx_dynlib_constructor_main.nim` (where `lib` is a shared 
library compiled from the C++ code), but in order to properly work this has to 
be compiled with `-llib`, which means that you basically need to use regular 
dynamic linker and not `dynlib`.
    
    
    WithConstructor val1(((int) 20));
    WithConstructor val2(((int) 20), ((int) 30));
    
    
    Run

Reply via email to