Some tips on calling C functions in GAUSS

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

  1. The C code must be compiled as a shared library (.dll on Windows, .so on Linux, .dylib on Mac).
  2. The shared library must be the same bit size (32 or 64-bit) as your copy of GAUSS.
  3. This shared library must be in the GAUSSHOME/dlib directory.
  4. Before it is available to be called inside GAUSS, the shared library must be loaded with the GAUSS function dlibrary.
  5. To call a function inside of the shared library, you prepend the function call with dllcall.
  6. All inputs to the C function need to be double pointers.
  7. 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;
    }
    

aptech

1,773

Your Answer

1 Answer

0
accepted
  1. The C code must be compiled as a shared library (.dll on Windows, .so on Linux, .dylib on Mac).
  2. The shared library must be the same bit size (32 or 64-bit) as your copy of GAUSS.
  3. This shared library must be in the GAUSSHOME/dlib directory.
  4. Before it is available to be called inside GAUSS, the shared library must be loaded with the GAUSS function dlibrary.
  5. To call a function inside of the shared library, you prepend the function call with dllcall.
  6. All inputs to the C function need to be double pointers.
  7. 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;
    }
    

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.