Customizing wolfram API calls

After creating a package in wolfram, i would like the call the package directly from the library without from host to executed in C. How to link ( wstp) that package in C libraries
How to frane the question and give the solution

How can I directly link a Wolfram Mathematica package (.m or .wl) to a C program using Wolfram Symbolic Transfer Protocol (WSTP), such that the package can be loaded and executed within the C environment without explicitly requiring the host interface? What are the steps to integrate the package into a C library and use it seamlessly?

Solution

  1. Package Preparation
    Ensure the Mathematica package is well-structured and contains the functions you want to call. For example:

(* File: arithmetic.wl *)
BeginPackage[“Arithmetic`”]
Add::usage = “Add[x, y] adds two numbers.”
Subtract::usage = “Subtract[x, y] subtracts the second number from the first.”
Begin[“`Private`”]
Add[x_, y_] := x + y
Subtract[x_, y_] := x - y
End[]
EndPackage[]
Save this file as arithmetic.wl or arithmetic.m.

  1. Write the Wolfram Script for WSTP
    Create a Wolfram script (.wls) to expose the package’s functions over WSTP.

(* File: ArithmeticServer.wls *)
Needs[“Arithmetic`”]

Install[LinkCreate[“ArithmeticLink”]];

Add[x_, y_] := ArithmeticAdd[x, y]; Subtract[x_, y_] := ArithmeticSubtract[x, y];

While[LinkReadyQ[“ArithmeticLink”],
LinkRead[“ArithmeticLink”];
(* Process requests here *)
]

LinkClose[“ArithmeticLink”];
Run this script on the Mathematica kernel to expose the functions through WSTP.

  1. Generate the WSTP Template
    Use the mprep tool to generate a C header file for the interface.

Create a .tm file:

:Begin:
:Function: Add
:Pattern: Add[x_Integer, y_Integer]
:Arguments: { x, y }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:end:

:Begin:
:Function: Subtract
:Pattern: Subtract[x_Integer, y_Integer]
:Arguments: { x, y }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:end:
Run mprep:

mprep Arithmetic.tm -o Arithmetic.c
4. Write the C Program
Link your Mathematica package with the generated C code using the WSTP functions.

#include <wstp.h>
#include <stdio.h>

int main() {
WSLINK link;
int errorCode;

// Initialize WSTP
if (!WSInitialize(NULL)) {
    printf("Failed to initialize WSTP\n");
    return 1;
}

// Open the link
link = WSOpenString(NULL, "-linkname 'ArithmeticLink'", &errorCode);
if (link == NULL || errorCode != WSEOK) {
    printf("Failed to open WSTP link: %d\n", errorCode);
    return 1;
}

// Example: Call the Add function
WSPutFunction(link, "Add", 2);
WSPutInteger(link, 5);
WSPutInteger(link, 3);

WSEndPacket(link);
WSTestHead(link, "Integer", &errorCode);

int result;
WSGetInteger(link, &result);
printf("Result of Add: %d\n", result);

// Close the link
WSClose(link);
WSDeinitialize();
return 0;

}
5. Compile and Run the Program
Compile using gcc (or an appropriate compiler):

gcc -o ArithmeticClient Arithmetic.c -lWSTP
Run the Wolfram script (ArithmeticServer.wls) to initialize the WSTP link.

Run the compiled C program:

./ArithmeticClient

Iam getting WSTP linking problem .Could anyone guide me , how to link wstp with C code