I'm trying to wrap a C++ module but having some trouble with compilation. Any help will be appreciated.

File I'm wrapping: link. It contains the implementation code as well.

The wrapped fts_fuzzy_match.nim contents:

{.push importcpp.}
{.push header: "fts_fuzzy_match.h".}
{.compile: "nimfuzz/fts_fuzzy_match.h".}
proc fuzzy_match_simple*(pattern: cstring; str: cstring): bool
proc fuzzy_match*(pattern: cstring; str: cstring; outScore: var cint): bool
proc fuzzy_match*(pattern: cstring; str: cstring; outScore: var cint;
                 matches: ptr uint8; maxMatches: cint): bool
{.pop.}
{.pop.}

The test.nim file:

import fts_fuzzy_match

var i: cint = 0
echo fuzzy_match("ftw", "ForrestTheWoods", i)
echo i

The error when I compile with

> nim cpp --passC:"-DFTS_FUZZY_MATCH_IMPLEMENTATION" test.nim
tests\nimcache\nimfuzz_test.cpp: In function 'void NimMainModule()':
tests\nimcache\nimfuzz_test.cpp:143:14: error: request for member 'fuzzy_match' in '"ftw"', which is of non-class type 'const char [4]'
  T1_ = "ftw".fuzzy_match("ForrestTheWoods", i_9c2pue6RWUiHyC8NB9amjv0Q);
              ^~~~~~~~~~~

Thanks in advance.

2017-11-11 21:01:05

I submitted an example to the docs, but it is still a PR, so have a look at the PR.

And try something like the following (and remove the push and pop pragmas):

const
  hdr = "nimfuzz/fts_fuzzy_match.h"
proc fuzzyMatchSimple*(pattern: cstring; str: cstring): bool {.importcpp: "fuzzy_match_simple", header: hdr.}

which will add the line

#include "nimfuzz/fts_fuzzy_match.h"

to the nimcache/test.cpp file, and map the c++ function fuzzy_match_simple to the Nim proc fuzzyMatchSimple()

2017-11-12 21:26:57

I was using c2nim to do the conversion and since I wasn't providing the --header flag, it wasn't putting in the {.importcpp.} pragma itself. A global {.push importcpp.} wasn't working since these were under the fts namespace. For some reason, it was thinking fuzzy_match() was a proc without a namespace even though the .h file is clear about that.

Turns out, the following command works well and does the needful.

c2nim --cpp --header:headernimfuzz fts_fuzzy_match.h

Generated output is as follows. The const line defining the header file was added after the fact. Should give an idea of how to get this working in general when wrapping C++ code.

const headernimfuzz = "fts_fuzzy_match.h"
proc fuzzy_match_simple*(pattern: cstring; str: cstring): bool {.
    importcpp: "fts::fuzzy_match_simple(@)", header: headernimfuzz.}
proc fuzzy_match*(pattern: cstring; str: cstring; outScore: var cint): bool {.
    importcpp: "fts::fuzzy_match(@)", header: headernimfuzz.}
proc fuzzy_match*(pattern: cstring; str: cstring; outScore: var cint;
                 matches: ptr uint8; maxMatches: cint): bool {.
    importcpp: "fts::fuzzy_match(@)", header: headernimfuzz.}

The manual was also good about this.

Thanks for the help.

2017-11-13 04:08:49
Might want to flag procs as cdecl with --cdecl
2017-11-13 04:22:15