Hello, I like to set up a "Jester" (example.nim) to handle some data transmitted by a httpRequest and using a shared library (calc.so) made with C++ to do some calculation on the data before sending it back to client.

The Header file calc.h that seems to be necessary for nim-compilation looks like follows.

#include <vector>
std::vector< std::vector<float> > calculation(float paramOne, float paramTwo);

For wrapping C++ and getting request-data I use the following syntax in Nim, which compiles without error.

import asyncdispatch, jester, strutils

type vector {.importcpp: "std::vector", header: "<vector>".} [T] = object
proc calculation(paramOne: cfloat, paramTwo: cfloat): vector[vector[cfloat]] {.importcpp: "calculation", header: "../calc.h".}

routes:
  
  get "/":
    redirect "index.html"
  
  get "/request":
    let requestData = request.params
    let requestOne = requestData["one"].parseFloat
    let requestTwo = requestData["two"].parseFloat
    
    #some additional Nim code

runForever()

But if using the imported procedure with following syntax I get a compilation error.

let result = calculation(requestOne, requestTwo)

Error: execution of an external compiler program 'clang++ -c  -w  -I/Users/developer/nim/nim-0.17.0/lib -o /Users/developer/Documents/Example/src/nimcache/Example_example.o /Users/developer/Documents/Example/src/nimcache/Example_example.cpp' failed with exit code: 1

/Users/developer/Documents/Example/src/nimcache/Example_example.cpp:830:66: error: member reference base type 'float' is not a structure or union
                                (*colonenvP_).result8 = ((float) ((*colonenvP_).requestOne6)).calculation(((float) ((*colonenvP_).requestTwo7)));
                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

By the way, I am doing "nimble cpp src/example.nim" to compile.

Has anybody an idea how to do it right?

2017-12-06 22:09:18
Try importc, importcpp assumes the first parameter is a class instance.
2017-12-06 23:39:05

Unfortunately there is also an error when using "importc".

So I will check how to implement the interface between Nim and C++ with class instance as first parameter. ...the shared library will be modified related to Nims needs

Thanks so far!!

2017-12-07 17:42:43

Actually "importc" is working well. I just missed to tell Nim the direction to the shared library.

So adding {.link: "cpp/calc.so".} and using either importc: "calculation" or importcpp: "calculation(@)" seems to be the solution.

2017-12-07 21:59:36