Hi fellow Gforthers
I need to understand how to use a shared library in Gforth with a Gforth image! I Think this is possible because
i can access a c shared library in gforth from the command line or from code that is accessed in gforth via the #! method as a script.
I can not however access the c shared library from an image though. I think this is because i do not understand some part of gforthmi
or perhaps gforth itself i am missing some part of the puzzle. The issue seems to be a linkage issue to the shared library but i present the
steps i took to test this below. Note the "#####" indicates a file or screen capture.
This is all done on a Raspberry pi running Debian Wheezy. I am using gforth version 0.7.2 but have also tried 0.7.0 with same results.
I am just looking for some input if a c shared library can be assessed in a gforth image or not and if so how is it done correctly!
##### someclib.c
#include <stdio.h>
int mytest(void)
{
printf("Hello world");
return 99 ;
}
#####
##### someclib.h
#include <stdio.h>
int mytest(void);
#####
Now i compile the c and make it a shared library as follows:
##### from the raspberry pi ssh session
address@hidden ~/git/gforth_testing $ gcc -c -fPIC someclib.c -o someclib.o
address@hidden ~/git/gforth_testing $ ar rcs libsomeclib.a someclib.o
address@hidden ~/git/gforth_testing $ gcc -shared -W1,-soname,libsomeclib.so.1 -o libsomeclib.so.1.0.1 someclib.o
address@hidden ~/git/gforth_testing $ sudo cp libsomeclib.so.1.0.1 /usr/lib/
address@hidden ~/git/gforth_testing $ sudo ln -sf /usr/lib/libsomeclib.so.1.0.1 /usr/lib/libsomeclib.so
address@hidden ~/git/gforth_testing $ sudo ln -sf /usr/lib/libsomeclib.so.1.0.1 /usr/lib/libsomeclib.so.1
address@hidden ~/git/gforth_testing $ sudo ldconfig -n /usr/lib/
address@hidden ~/git/gforth_testing $ sudo cp someclib.h /usr/include/someclib.h
#####
The library is now a shared library and the system sees it and i can link to it with other c code.
Now i want to link to it with a wrapper function in gforth to allow usage of c library functions in gforth
but ultimately i want the gforth as fast as it can be so i would like to make an image for gforth to use.
I find this does not work! The steps are show below.
##### mywrapper.fs
clear-libs
c-library mybugtest
s" someclib" add-lib
\c #include "someclib.h"
\c int thecfunction(void) { return (mytest()); }
c-function forthword thecfunction -- n
end-c-library
######
###### mytest.fs
include mywrapper.fs
variable myvar
5 myvar !
: testword myvar @ . cr threading-method . cr version-string type cr forthword cr . cr ;
:noname testword bye ; is bootmessage
######
Now if i use gforthmi as follows then run the code as shown below it fails!
###### this is from the raspberry pi ssh session
address@hidden ~/git/gforth_testing $ sudo gforthmi mytest mytest.fs
data offset=-2AF20
code offset=-5CEF98
xt offset=-5CEFA8
30 B69A7000 B697C000
34 B69AC000 B6981000
38 B69B1000 B6986000
3C B69B6000 B698B000
1CCC B69AB000 B6980000
1CD0 B69B4C00 B6989C00
1CD4 B69B0000 B6985000
1CDC B69B4B98 B6989B98
1CE0 B69B4B94 B6989B94
1CEC 1099128 ACA128
2D40 B6F1A5E0 B6EEF5E0
2D60 B6F1A680 B6EEF680
970C BED8B5D8 BED475D8
9748 BED8B80C BED4780C
9F88 10885A8 AB95A8
B1AC 10999E0 ACA9E0
10460 1090490 AC1490
104C0 1088A00 AB9A00
16D20 1090A20 AC1A20
19048 1090B90 AC1B90
22304 1082166 AB3166
22358 1098D18 AC9D18
227B0 1099330 ACA330
28800 10992F8 ACA2F8
address@hidden ~/git/gforth_testing $ sudo gforth -i mytest
5
0
0.7.2
:0: Invalid memory address
Backtrace:
$B69EA770 lib-sym
$B69EA8C0 link-wrapper-function
$B69EE978 forthword
$B69EE9A8 testword
########
But if i simply do the following from command line the code with the shared library works!
Why does this work when it is compiled into the standard gforth image but not precompiled as an image?
I know from testing that i can use my own images made with gforthmi but i can not seem to have access to anything other then gforth code
in those images. How then can i have access to shared c library and yet get the performance of an precompiled image in gforth at the same time?
######## this is from the raspberry pi ssh session
address@hidden ~/git/gforth_testing $ sudo gforth mytest.fs
5
0
0.7.2
Hello world
99
address@hidden ~/git/gforth_testing $
########
Any help would be appreciated! PKS
--
\|||/
-----0oo---( o o )---oo0------
(_)
From the hand of PKS