Re: Pass C double to Eu shared library
- Posted by mattlewis (admin) May 12, 2012
- 1545 views
Jerome said...
I have a shared library made with EU 4 that I would like to access from C by passing an array of doubles. Looking through the manual I found this:
Euphoria shared dynamically loading library can also be used by C programs as long as only 31-bit integer values are exchanged. If a 32-bit pointer or integer must be passed, and you have the source to the C program, you could pass the value in two separate 16-bit integer arguments (upper 16 bits and lower 16 bits), and then combine the values in the Euphoria routine into the desired 32-bit atom.
Is there a clean way to do this?
My current code is essentially the following:
-- Eu Code export function MyFunc(sequence x) ? x end function
// C Code extern MyFunc(double x[]) int main() { double x[4] = {1.1,2.2,3.3,4.4}; MyFunc(x); return 0; }
I think the cleanest way is to use Use 32-bit Integers In Translated DLLs. It's kind of a hack, but I think it's the cleanest solution. It requires special wrappers that the external world should call, and it modifies your translated source.
It seems to almost work with 4.0.4. This required some updates to make_atom.exw for 4.0. And I modified test.exw to open the .so that I created on Linux, instead of a Windows dll.
$ euc -makefile -so test.ew Translating code, pass: 1 2 3 generating 4.c files were created. To build your project, type make -f test.mak $ eui make_atom.exw ~/eu/32bits$ make -f test.mak gcc -fomit-frame-pointer -fPIC -c -w -fsigned-char -O2 -m64 -I/usr/local/share/euphoria -ffast-math init-.c -o init-.o gcc -fomit-frame-pointer -fPIC -c -w -fsigned-char -O2 -m64 -I/usr/local/share/euphoria -ffast-math test.c -o test.o gcc -fomit-frame-pointer -fPIC -c -w -fsigned-char -O2 -m64 -I/usr/local/share/euphoria -ffast-math main-.c -o main-.o gcc -fomit-frame-pointer -fPIC -c -w -fsigned-char -O2 -m64 -I/usr/local/share/euphoria -ffast-math make_atom.c -o make_atom.o gcc -o test.so init-.o test.o main-.o make_atom.o /usr/local/lib/euso.a -m64 -shared -ldl -lm -lpthread $ eui test Passing signed integer: -225 Signed( -225 ) Passing unsigned integer: 4294967071 Unsigned( 4294967071 ) Done. Press any key to exit.
Matt