I am facing the following issue. I have the following code:
{.experimental: "codeReordering".}
{.experimental: "callOperator".}
include tkmath/tkmath, tkernel/tkernel
let
myWidth = 50.0
myThickness = 20.0
myHeight = 70.0
aPnt1 = newPnt( (25.0).cfloat, (2.0).cfloat, (3.0).cfloat ) #newPnt((-myWidth / 2.0).cfloat, (0.0).cfloat, (0.0).cfloat)
echo aPnt1.x
where newPnt is defined as:
proc newPnt*(xp: cfloat; yp: cfloat; zp: cfloat): Pnt {.cdecl, constructor,
importcpp: "gp_Pnt(@)", dynlib: tkmath.}
When compiling:
nim cpp ex01
it is generating the following:
What am I missing?
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);
};
#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";
}
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()
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));