I have looked at the manual chapter on calling functions written in C from GAUSS, but I was wondering if someone could give me a summary of the main points I need to keep in mind?
1 Answer
0
accepted
- The C code must be compiled as a shared library (.dll on Windows, .so on Linux, .dylib on Mac).
- The shared library must be the same bit size (32 or 64-bit) as your copy of GAUSS.
- This shared library must be in the GAUSSHOME/dlib directory.
- Before it is available to be called inside GAUSS, the shared library must be loaded with the GAUSS function dlibrary.
- To call a function inside of the shared library, you prepend the function call with dllcall.
- All inputs to the C function need to be double pointers.
- You should preallocate any inputs that will be assigned to inside of your C function in GAUSS before the call (usually the zeros function is the best choice). For example, if you have a C function that takes an input vector "A" and simply copies it to "B", your GAUSS code might look like this:
//load the shared library into GAUSS dlibrary mylibrary; //size of vectors nelems = 10; //Fill in 'A' with random normal numbers A = rndn(nelems, 1); //Pre-allocate 'B' to be the same size as 'A' B = zeros(nelems, 1); //Call your C function dllcall copyAtoB(A, B, nelems); //After the call above, 'B' should have the same contents as 'A'
The C program might look like this:
#include
int copyAtoB(double *A, double *B, double *nelems) { int i, n; n = (int)nelems[0]; for (i = 0; i < n; i++) B[i] = A[i]; return 0; }
Your Answer
1 Answer
0
accepted
- The C code must be compiled as a shared library (.dll on Windows, .so on Linux, .dylib on Mac).
- The shared library must be the same bit size (32 or 64-bit) as your copy of GAUSS.
- This shared library must be in the GAUSSHOME/dlib directory.
- Before it is available to be called inside GAUSS, the shared library must be loaded with the GAUSS function dlibrary.
- To call a function inside of the shared library, you prepend the function call with dllcall.
- All inputs to the C function need to be double pointers.
- You should preallocate any inputs that will be assigned to inside of your C function in GAUSS before the call (usually the zeros function is the best choice). For example, if you have a C function that takes an input vector "A" and simply copies it to "B", your GAUSS code might look like this:
//load the shared library into GAUSS dlibrary mylibrary; //size of vectors nelems = 10; //Fill in 'A' with random normal numbers A = rndn(nelems, 1); //Pre-allocate 'B' to be the same size as 'A' B = zeros(nelems, 1); //Call your C function dllcall copyAtoB(A, B, nelems); //After the call above, 'B' should have the same contents as 'A'
The C program might look like this:
#include
int copyAtoB(double *A, double *B, double *nelems) { int i, n; n = (int)nelems[0]; for (i = 0; i < n; i++) B[i] = A[i]; return 0; }