Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Created April 25, 2025 09:41
Show Gist options
  • Save Foadsf/57682c0e6217112869fa46b0bcd93f8c to your computer and use it in GitHub Desktop.
Save Foadsf/57682c0e6217112869fa46b0bcd93f8c to your computer and use it in GitHub Desktop.
A comprehensive comparison of four approaches for integrating C code with Scilab on Windows using MinGW-w64/MSYS2, highlighting trade-offs in complexity, flexibility, and implementation details.

Scilab-C Integration Methods Comparison (MinGW/MSYS2 on Windows)

This document compares four different approaches for integrating C code with Scilab on Windows using the MinGW-w64 GCC compiler from MSYS2. Each approach offers different trade-offs in terms of complexity, flexibility, and maintainability.

Overview of the Four Approaches

  1. minimal-scilab-c-gateway: A minimalistic example of a Scilab C gateway using the modern API.
  2. scilab-mingw-c-gateway-mwe: A Minimal Working Example (MWE) for C/C++ gateways with Scilab.
  3. scilab-mingw-c-call-minimal-example: Direct DLL calling using Scilab's link and call functions.
  4. scilab-mingw-module (my_foo6): A more complex gateway example demonstrating multiple input/output types.

Detailed Comparison

Feature minimal-scilab-c-gateway scilab-mingw-c-gateway-mwe scilab-mingw-c-call-minimal-example scilab-mingw-module (my_foo6)
Approach Classic C gateway with modern API Comprehensive C/C++ gateway with explanatory structure Direct DLL loading and function calling Advanced gateway with multiple data types
Integration Style API-driven gateway Gateway with MWE structure Direct DLL calling (link/call) Gateway with manual compilation
Complexity Low to Medium Medium Low High
Code Structure Simple and concise Well-organized with clear separation Minimalistic approach Complex with multiple files
Build Process Simple batch file Detailed batch script with checks Comprehensive batch script Advanced batch script with debugging
Scilab Side Uses addinter function Uses addinter function Uses link and call functions Uses addinter with manual loader generation
C/C++ Components C and C++ mixed Well-separated C and C++ files Pure C C and C++ mixed with complex structure
Error Handling Basic Comprehensive Robust Advanced with detailed error messages
Maintainability Medium High Medium Complex
Documentation Quality Basic Excellent Good with detailed README Very detailed with notes on challenges
Function Registration Simple gateway registration Clear and well-explained No gateway, direct function call Advanced gateway with error handling
Header Handling Includes minimal headers Comprehensive headers with dummies Proper export control in headers Complex with stubs for missing headers
Best For Quick prototyping and simple functions Learning and understanding the gateway mechanism Simple direct function calls without gateways Complex functions with multiple data types

Implementation Details

1. minimal-scilab-c-gateway

Key Files:

  • gateway.cpp: The main C++ gateway file that bridges Scilab and C
  • simple_c_function.c: A simple C function that multiplies two numbers
  • build_and_run.bat: Windows batch file for compilation and testing

Integration Approach: This repository demonstrates a minimalistic approach to creating a Scilab gateway for a C function. It uses the modern Scilab API with functions like scilab_getDoubleArray and scilab_createDoubleMatrix2d. The gateway function sci_multiply handles parameter checking, data extraction, function calling, and returning results.

Compilation Process: The build script sets up include paths for Scilab headers, compiles the C function and C++ gateway separately, then links them into a DLL. The DLL is loaded in Scilab via the addinter function.

Pros:

  • Simple and straightforward implementation
  • Minimal code to understand the gateway process
  • Direct mapping between Scilab function and C function

Cons:

  • Limited error handling
  • Does not demonstrate multiple functions or complex data types
  • Basic documentation

2. scilab-mingw-c-gateway-mwe

Key Files:

  • src/minimal_gateway.cpp: Comprehensive C++ gateway implementation
  • src/minimal_lib.c & src/minimal_lib.h: C library implementation
  • build_and_run.bat: Detailed build script with environment checks
  • scilab_scripts/run_example.sce: Scilab testing script

Integration Approach: This repository provides a more structured approach with clear separation between the C library code and the C++ gateway. It demonstrates a well-organized project structure and thorough documentation of the gateway mechanism. The code includes comprehensive error checking and parameter validation.

Compilation Process: The build script includes robust environment checks, explicitly defined compiler and linker flags, and thorough error handling. The compilation process is well-documented with clear steps for compiling C and C++ components separately before linking.

Pros:

  • Excellent project structure and organization
  • Comprehensive documentation and comments
  • Clear separation of concerns between C library and gateway
  • Thorough error handling and parameter validation

Cons:

  • More complex than needed for very simple functions
  • May be overkill for quick prototyping

3. scilab-mingw-c-call-minimal-example

Key Files:

  • simple_math.c & simple_math.h: C implementation with two function variants
  • simple_math.def: Export definitions for the DLL
  • build_and_run.bat: Comprehensive build script
  • run_example.sce: Scilab script for loading and testing

Integration Approach: This repository takes a different approach, bypassing the gateway mechanism entirely. Instead, it demonstrates how to create a C DLL that can be directly loaded into Scilab using the link function and called using the call function. This approach is simpler but less integrated with Scilab's type system.

Key Innovation: The repository provides two versions of each function:

  1. Standard C version for normal C programs
  2. Scilab-compatible version with an underscore suffix (e.g., multiply_doubles_) that uses by-reference parameters matching Scilab's calling convention

Compilation Process: The build script compiles the C code into a DLL with proper exports, then links it. The DLL is loaded in Scilab using link with explicit function name, and called using call with specific parameter formats.

Pros:

  • Simplest approach for calling C functions directly
  • No gateway code required
  • Clear explanation of Scilab's function calling requirements
  • Easier to understand for those familiar with DLLs and direct function calls

Cons:

  • Less integrated with Scilab's type system
  • More manual work required for type conversion and parameter passing
  • Not suitable for complex functions with varied data types

4. scilab-mingw-module (my_foo6)

Key Files:

  • sci_foo6.c: Complex C gateway with multiple data types
  • build_lib.cpp, build_lib.h, build_lib.hxx: Gateway infrastructure
  • build_and_run.bat: Advanced build script with debug capabilities
  • stubs.c: Provides stub implementations for missing symbols
  • find_scilab_symbols.bat: Utility script for debugging

Integration Approach: This repository demonstrates a more complex gateway that handles multiple data types (double and boolean matrices). It shows more advanced techniques like manual compilation, handling linker errors, and creating stubs for missing symbols.

Compilation Process: The build process is the most complex of all four approaches. It uses a manual compilation approach instead of relying on Scilab's ilib_build. The process includes generating a loader script, compiling C and C++ files separately, and linking with necessary Scilab libraries. It also includes workarounds for linker errors when using MinGW.

Pros:

  • Demonstrates advanced techniques for complex gateway functions
  • Handles multiple data types and parameter validation
  • Provides solutions for common MinGW/Scilab integration challenges
  • Includes debugging utilities and detailed error handling

Cons:

  • Highest complexity among all options
  • May be overwhelming for beginners
  • Requires more maintenance and understanding of Scilab internals

Choosing the Right Approach

Best for beginners:

  • scilab-mingw-c-call-minimal-example: If you just want to call C functions from Scilab with minimal overhead.

Best for learning:

  • scilab-mingw-c-gateway-mwe: Provides the best documentation and structure for understanding the gateway mechanism.

Best for quick prototyping:

  • minimal-scilab-c-gateway: Simple approach for creating basic gateways quickly.

Best for complex functions:

  • scilab-mingw-module (my_foo6): Demonstrates how to handle multiple data types and complex gateway requirements.

Conclusion

For most users, scilab-mingw-c-gateway-mwe offers the best balance between clarity, structure, and functionality. It provides a well-documented approach with proper separation of components that can be extended for more complex functions.

If you need the absolute simplest approach and don't need gateway integration, scilab-mingw-c-call-minimal-example is your best choice.

For complex functions with multiple data types or when facing MinGW/Scilab integration challenges, scilab-mingw-module (my_foo6) provides valuable solutions and workarounds.

The minimal-scilab-c-gateway approach is suitable for quick prototyping and understanding the basic concepts but lacks the robust structure needed for larger projects.


Additional Tips for C-Scilab Integration with MinGW

  1. Include Paths: Always ensure you include all necessary Scilab header directories, especially:

    • %SCILAB_DIR%/modules/api_scilab/includes
    • %SCILAB_DIR%/modules/core/includes
    • %SCILAB_DIR%/modules/ast/includes/ast
    • %SCILAB_DIR%/modules/ast/includes/types
  2. Library Dependencies: Key Scilab libraries to link against:

    • api_scilab
    • core
    • ast
    • output_stream
  3. Linking with MinGW: Always use g++ for the final linking step, even if your code is mostly C. This ensures proper handling of C++ dependencies in Scilab's API.

  4. Error Handling: Don't neglect error checking in gateway functions, as it makes debugging much easier:

    • Check parameter counts
    • Validate parameter types
    • Check for memory allocation failures
    • Provide clear error messages with Scierror
  5. Function Naming: When using direct calls with the link/call approach, remember that Scilab expects function names with underscore suffixes (e.g., multiply_doubles_).

  6. Parameter Passing: Scilab passes by reference for its call interface, so your C functions should accept pointers rather than direct values.

  7. Build Script Organization: A good build script should:

    • Set up proper paths
    • Clean previous artifacts
    • Check for required tools
    • Compile C and C++ separately
    • Link with proper flags
    • Run tests automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment