OK, I'm trying to work on a simpler example, to get a better feel for
what's happening. Now I'm running into another (simple?) problem I
don't understand.
I made a C file containing one function:
double dotprod(double *x, double *y, int n);
double dotprod(double *x, double *y, int n) {
int k;
double sum = 0;
for (k = 0; k < n; ++k) {
sum += x[k] * y[k];
}
return sum;
}
Compiled it using gcc -c.
I made the following Lisp file to load the object file:
(declaim (optimize (debug 3) (safety 3) (speed 0)))
(defpackage "MY-DOTPROD"
(:use "COMMON-LISP"))
(in-package "MY-DOTPROD")
(alien:load-foreign "/home/rif/Tmp/htk/dotprod.o")
(alien:def-alien-routine ("dotprod" dotprod)
double-float
(x (* double-float) :in)
(y (* double-float) :in)
(n integer :in))
This file compiles fine, but trying to load the compiled .x86f fails:
; Loading #p"/home/rif/Tmp/htk/dotprod.x86f".
Undefined foreign symbol: "dotprod"
Restarts:
0: [CONTINUE] Return NIL from load of "dotprod.x86f".
1: [ABORT ] Return to Top-Level.
Debug (type H for help)
(KERNEL::UNDEFINED-FOREIGN-SYMBOL-ERROR-HANDLER "<error finding name>"
#.(SYSTEM:INT-SAP #x3FFFE7A0)
#<Alien (* #) at #x3FFFE448>
(14))
Source:
; File: target:code/interr.lisp
(ERROR 'SIMPLE-PROGRAM-ERROR :FUNCTION-NAME NAME :FORMAT-CONTROL ...)
0] 0
NIL
*
It looks to me like it's trying to execute the def-alien-routine
before it has the symbols from dotprod.o. Indeed, if I manually first
execute the call
(alien:load-foreign "/home/rif/Tmp/htk/dotprod.o")
then the .x86f file loads fine. Any idea what's going on here?
Cheers,
rif
ps. This example is taken from section 8.7.5 of the CMUCL manual. As
far as I can tell, this example was never tested, as there are at least
three errors:
1. The definition of sum is wrong --- it's declared to return a
double and returns nothing.
2. The def-alien-routine call is wrong. It says (n int :in) but I'm
pretty sure means (n integer :in).
3. The call (system:int-sap (array-data-address x)) can't work. It
should be (system:vector-sap x).
Does anyone agree/disagree that this example is of pretty low quality,
and should probably be fixed up?