Here is my problem: I need to design a program in F# that, from a C program, performs simple 3D vector calculations. I don't share the calculations on the codes below because the problem stops at creating the vectors according to the printfn.
So I think it's an error, but my schoolmate on macOS has no problem with this and can run the code correctly. I deduced that it was a Windows problem, probably because of the pointers. So I'm relying on you to help me.
Thanks a lot.
PS: dotnet version 7.0.101 clang version 15.0.5
Vector3.cpp
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
return 0;
}
class Vector3
{
public:
double X;
double Y;
double Z;
Vector3(double x, double y, double z) : X(x), Y(y), Z(z) {}
};
extern "C" Vector3* CreateVector3(double x, double y, double z)
{
Vector3* v = new Vector3(x, y, z);
return v;
}
Program.fs
open System.Runtime.InteropServices
[<StructLayout(LayoutKind.Sequential)>]
printfn("test 1: Launching the program")
type Vector3 =
val mutable X: double
val mutable Y: double
val mutable Z: double
new(x, y, z) = { X = x; Y = y; Z = z }
[<DllImport("compiledVector3.exe")>]
extern Vector3 CreateVector3(double x, double y, double z)
[<DllImport("compiledVector3.exe")>]
extern double GetX(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double GetY(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double GetZ(Vector3 v)
[<DllImport("compiledVector3.exe")>]
extern double distanceTo(Vector3 v,Vector3 v2)
[<DllImport("compiledVector3.exe")>]
extern void vectorMovement(Vector3 v,double plusx, double plusy, double plusz)
[<DllImport("compiledVector3.exe")>]
extern Vector3 midpoint(Vector3 v,Vector3 v2)
[<DllImport("compiledVector3.exe")>]
extern double percentDistance(Vector3 pos1, Vector3 pos2, double percent)
printfn("test 2: DLL imported")
let FstVector= CreateVector3(0.0, 0.0, 0.0)
let SndVector= CreateVector3(1.0, 2.0, 3.0)
printfn("test 3: Vectors created")
I tried to launc the Program.fs with a basic dotnet run command, expecting to see in my terminal the following lines:
test 1: Launching the program
test 2: DLL imported
test 3: Vectors created
but there isn't the third line.
Terminal
PS C:\Users\AlexisLasselin\Documents\GitHub\2022-2023-project-3-harfang3d-binding-Project-4-group\CppToFs\Vector3> dotnet run
test 1: Launching the program
test 2: DLL imported
PS C:\Users\AlexisLasselin\Documents\GitHub\2022-2023-project-3-harfang3d-binding-Project-4-group\CppToFs\Vector3>
CodePudding user response:
What you have is a calling convention mismatch. C 's default calling convention for free functions is Cdecl regardless of platform, but the DllImport
attribute's default calling convention is platform-dependent and it so happens to be Cdecl on macOS but not Windows (there it is StdCall). Fortunately the fix is simple:
[<DllImport("compiledVector3.exe", CallingConvention = CallingConvention.Cdecl)>]
CodePudding user response:
I think the compilation of your dll file is made for architecture x32 and you want to compile with a x64 windows architecture :
To compile a DLL using g for a 64-bit architecture, you can use the following command:
g -shared -o <filename>.dll -m64 <source files>
Replace with the desired name of the DLL, and with the names of the source files that make up the DLL.
The -shared option tells g to create a shared library (i.e., a DLL), and the -o option specifies the name of the output file. The -m64 option specifies that you want to compile for a 64-bit architecture.