Questions:
My MWE:
// sample.h
#include <vector>
struct SimpleStruct {
std::vector<double> rewards;
};
void useStruct();
// sample.cpp
#include "sample.h"
void useStruct() {
SimpleStruct st;
st.rewards = {1,3,5};
}
import nimterop/cimport
static:
cDebug()
cDisableCaching()
cCompile(path="sample.cpp", mode="cpp")
cImport(filename="sample.h", mode="cpp")
echo "finished"
Output shows struct field confusion
{.push hint[ConvFromXtoItselfNotNeeded]: off.}
{.pragma: impsampleHdr, header: "<full_path...>/sample.h".}
{.experimental: "codeReordering".}
{.compile: "<full_path...>/sample.cpp".}
type
SimpleStruct* {.bycopy, impsampleHdr, importc: "struct SimpleStruct".} = object
a1*: rewards
proc useStruct*() {.importc, cdecl, impsampleHdr.}
{.pop.}
Error
<...> template/generic instantiation of `cImport` from here
Error: undeclared identifier: 'rewards'
You have a field in your nim type object that is of type rewards. You haven't declared such a type.
I'm pretty sure you can just use importc and let it do the rest, you don't have to declare the fields etc.
@ynfle Yes, but I didn't write that. This is the problem.
@Araq, ah okay, I'll use c2nim. Thank you for the direction. So far I'm struggling even installing c2nim. I wonder if c2nim does not work yet with v1.6? I'll poke at it some more.
> nimble update
...
› nimble install c2nim
Downloading https://github.com/nim-lang/c2nim using git
Verifying dependencies for [email protected]
tables.nim(234) raiseKeyError
Error: unhandled exception: key not found: url [KeyError]
› nimble install c2nim@#head
Downloading https://github.com/nim-lang/c2nim using git
Verifying dependencies for c2nim@#head
tables.nim(234) raiseKeyError
Error: unhandled exception: key not found: url [KeyError]
› nimble --version
nimble v0.13.1 compiled at 2021-10-23 01:24:00
git hash: d13f3b8ce288b4dc8c34c219a4e050aaeaf43fc9
› nim --version
Nim Compiler Version 1.6.0 [Linux: amd64]
Compiled at 2021-10-23
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 727c6378d2464090564dbcd9bc8b9ac648467e38
active boot switches: -d:release
Try this:
git clone https://github.com/nim-lang/c2nim
cd c2nim
nim c c2nim
cp c2nim somewhere_in_your_$PATH
c2nim has no dependencies anyway, no need to bother Nimble with it.