import C function in XOJO

Hello,
Is that possible to import C function in XOJO and how to ?
Thanks
OL

you can

  • compile C code to a dynamic library and access it with declares
  • put C code in a plugin
  • translate it line by line to Xojo

Thanks Christian,
I will try to compile dll.
How do you proceed to put it in a plugin ?

Hi Olivier, here is a very basic example of calling a C function from Xojo (on Windows) using the GCC (MinGW) C++ compiler:

fastmath.c

#include <stdint.h>

int32_t DoubleValue(int32_t x) {
        return 2 * x;
}

fastmath.h

#ifndef FASTMATH_H
#define FASTMATH_H

#ifdef __cplusplus
extern "C" {
#endif

int __declspec(dllexport) Double(int x);

#ifdef __cplusplus
}
#endif 

#endif  // FASTMATH_H

build.bat

gcc -shared -o fastmath.dll fastmath.c

Once you’ve built the fastmath.dll, you can then easily call if from Xojo using a declare like such:

  Soft Declare Function DoubleValue Lib "fastmath.dll" ( x As Int32 ) as Int32
  
  MsgBox Str(DoubleValue(20))

The example project (C files included) can be downloaded from www.xojo3d.com/Xojo_CLib_Example.zip

Really nice
I will try this

[quote=137395:@OLIVIER GOEDGEBEUR]Thanks Christian,
I will try to compile dll.
How do you proceed to put it in a plugin ?[/quote]

For DLL, see http://www.xojo.com/blog/en/2014/01/accessing-net-code-from-xojo.php

Just realized the function is declared incorrectly in the header file. It should read:

int32_t __declspec(dllexport) DoubleValue(int32_t x);

For this example you don’t even need the header file if you are only going to use the function in Xojo.