Building sharable object libraries for CALL_EXTERNAL [message #14591] |
Fri, 12 March 1999 00:00  |
Octavi Fors
Messages: 13 Registered: March 1999
|
Junior Member |
|
|
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hello,
<p>This is my first trial calling a C-routine from IDL with CALL_EXTERNAL,
and I have some problems with building <i>routine.so</i> file. It's just
a test, so the routine it's very simple:
<p><font size=-1>#include <math.h></font>
<br><font size=-1>#include <stdio.h></font>
<br><font size=-1>#include <stdlib.h></font>
<br><font size=-1>#include <fcntl.h></font>
<br><font size=-1>#include <sys/types.h></font>
<br><font size=-1>#include <sys/stat.h></font>
<br><font size=-1>#include <string.h></font><font size=-1></font>
<p><font size=-1>float** routine(argc,argv)</font>
<br><font size=-1>int argc;</font>
<br><font size=-1>void *argv[];</font>
<br><font size=-1>{</font>
<br><font size=-1>int n, x, y;</font>
<br><font size=-1>float *input;</font>
<br><font size=-1>float **output;</font>
<br><font size=-1>int i,j;</font><font size=-1></font>
<p><font size=-1> input = (float *) argv[0];</font>
<br><font size=-1> n=*(int *) argv[1];</font>
<br><font size=-1> x=*(int *) argv[2];</font>
<br><font size=-1> y=*(int *) argv[3];</font><font size=-1></font>
<p><font size=-1> output=(float **)malloc(4*n);</font>
<br><font size=-1> for(i=0;i<n;i++) output[i]=(float*)malloc(4*x*y);</font><font size=-1></font>
<p><font size=-1>for(j=0;j<n;++j)</font>
<br><font size=-1>for(i=0;i<x*y;++i)</font>
<br><font size=-1>output[j][i]=input[i];</font><font size=-1></font>
<p><font size=-1>return(output);</font>
<br><font size=-1>}</font>
<p>First, I try to create routine.o with:
<br><font size=-1>$ cc -pic -fsingle -c routine.c</font>
<br>no problem.
<p>Second, when I try to create routine.so with:
<br><font size=-1>$ ld -o routine.so -z text routine.o (changing '-assert
pure-text' to '-z text' because it's a SunOS Version 5.5.1)</font>
<br><font size=-1> Undefined & amp;nbsp; & amp;nbsp; & amp;nbsp;
first referenced</font>
<br><font size=-1> symbol & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
in file</font>
<br><font size=-1> malloc & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
routine.o (symbol belongs to implicit dependency /usr/lib/libc.so.1)</font>
<br><font size=-1> .mul &n bsp; &n bsp; &n bsp; &n bsp; &n bsp;
routine.o (symbol belongs to implicit dependency /usr/lib/libc.so.1)</font>
<br><font size=-1> mcount & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
routine.o</font>
<br><font size=-1>ld: fatal: Symbol referencing errors. No output written
to routine.so</font><font size=-1></font>
<p>It seems to need /usr/lib/libc.so.1. So I add it in the command line:
<br><font size=-1>$ ld -o routine.so -z text routine.o /usr/lib/libc.so.1</font>
<br><font size=-1> Undefined & amp;nbsp; & amp;nbsp; & amp;nbsp;
first referenced</font>
<br><font size=-1> symbol & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
in file</font>
<br><font size=-1> mcount & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
routine.o</font>
<br><font size=-1>ld: fatal: Symbol referencing errors. No output written
to routine.so</font><font size=-1></font>
<p>Malloc error is gone, but mcount (?) remains. Does anybody know what's
wrong with it?
<p>Thans in advance,
<p>Octavi.
<pre>--
============================================================ =====
Octavi Fors Aldrich
Astronomy Department</pre>
<pre>Physics Faculty</pre>
<pre>Avgda. Diagonal 647
08028 Barcelona
SPAIN
Telf: 34-934021122
Fax: 34-934021133
e-mail: octavi@fajnm1.am.ub.es
============================================================ ===== </pre>
</html>
|
|
|
Re: Building sharable object libraries for CALL_EXTERNAL [message #14635 is a reply to message #14591] |
Wed, 17 March 1999 00:00   |
rmlongfield
Messages: 68 Registered: August 1998
|
Member |
|
|
In article <36E9649A.7F5C8B65@fajnm1.am.ub.es>,
Octavi Fors <octavi@fajnm1.am.ub.es> wrote:
> Hello,
>
> This is my first trial calling a C-routine from IDL with CALL_EXTERNAL,
> and I have some problems with building routine.so file. It's just a
> test, so the routine it's very simple:
Hello Octavi, I am a few days late, but do not know if you have solved your
problem. I work on an SGI and have the following Makefile,where grouptest is
your example program. I recall that it took a lot of testing and searching in
the SGI man pages to find the right flags. It was a lot simpler than the
sample programs provided by IDL. --------------------------------- LDFLAGS=
-lm
OBJS = grouptest.o
grouptest : $(OBJS)
ld -shared -o grouptest.so $(OBJS) $(LDFLAGS)
grouptest.o : grouptest.c
cc -c -KPIC grouptest.c -o grouptest.o
This compiles ok on my SGI. When run from IDL, the only problem is with the
returned value of output.
--IDL sample file ---->
pro grouptest
a = FLOAT(1.4)
b= LONG(2)
c = LONG(7)
d = LONG(9)
output = CALL_EXTERNAL('grouptest.so','grouptest',a,b,c,d)
print,'Returned value from group test: ',output
end
-------------------------
If you can search back in this newsgroup a couple of months (Dejanews, for
example, can be used to search archives) you will find a lot of discussion
about CALL_EXTERNAL and pointers. Maybe it is different for a SUN, but I am
pretty sure that you cannot pass a pointer with the C return statement. Note
also that all memory for variables sent through CALL_EXTERNAL must first be
specified in IDL. This may also be a source of error. Another note is that,
if you change your C code, you must exit IDL before running again from within
IDL.
I use CALL_EXTERNAL with C and Fortran programs in a simple way which works.
Please feel free to ask further questions and I will try to help.
Good Luck!
Rose
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
|
|
|
|
|
Re: Building sharable object libraries for CALL_EXTERNAL [message #14697 is a reply to message #14591] |
Mon, 22 March 1999 00:00  |
Dr. G. Scott Lett
Messages: 14 Registered: February 1998
|
Junior Member |
|
|
I don't think this is true. Functions like IDL_MakeStruct(),
IDL_ImportArray() and IDL_ImportNamedArray(),
are handy for building IDL variables with memory you allocate.
However, the following code will most likely not produce the desired
results. Reading the External Development Guide is a recommended first
step, especially the chapter on IDL Internals: Variables.
> output=(float **)malloc(4*n);
> for(i=0;i<n;i++) output[i]=(float*)malloc(4*x*y);
Scott
>
> I thought it was a no-no to allocate memory inside of an external
> routine, that is to be passed back to IDL. The Advanced Developer's
> Guide has always stated that all memory must be allocated prior to
> calling the routine. Is that not always the case?
>
> Even if you get this to compile, I would be very wary of the results.
>
> Dave
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
> David S. Foster Univ. of California, San Diego
> Programmer/Analyst Brain Image Analysis Laboratory
> foster@bial1.ucsd.edu Department of Psychiatry
> (619) 622-5892 8950 Via La Jolla Drive, Suite 2240
> La Jolla, CA 92037
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|
|
|
Re: Building sharable object libraries for CALL_EXTERNAL [message #14699 is a reply to message #14670] |
Mon, 22 March 1999 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Octavi Fors wrote:
> Taking a look at the routine.c code (it's in the first message), I
> noticed that the
> **pointer (output) I'm passing back to IDL is malloced in the
> C-routine, in line:
>
> output=(float **)malloc(4*n);
> for(i=0;i<n;i++) output[i]=(float*)malloc(4*x*y);
>
> Is there something wrong with this?
I thought it was a no-no to allocate memory inside of an external
routine, that is to be passed back to IDL. The Advanced Developer's
Guide has always stated that all memory must be allocated prior to
calling the routine. Is that not always the case?
Even if you get this to compile, I would be very wary of the results.
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2240
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|
|
|
Re: Building sharable object libraries for CALL_EXTERNAL [message #14727 is a reply to message #14591] |
Thu, 18 March 1999 00:00  |
Octavi Fors
Messages: 13 Registered: March 1999
|
Junior Member |
|
|
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
"Eric J. Korpela" wrote:
<blockquote TYPE=CITE>In article <36EAAE98.8F975873@fajnm1.am.ub.es>,
<br>Octavi Fors <octavi@fajnm1.am.ub.es> wrote:
<br>>
<br>>I tried your advice with -G flag:
<br>>
<br>>$ld -o routine.so -z text -G routine.o
<br>>Text relocation remains &am p;nbsp; &am p;nbsp; &am p;nbsp;
referenced
<br>> against symbol & ;nbsp; & ;nbsp;
offset in file
<br>><unknown>
0x1c &n bsp; routine.o
<br>><unknown>
0x20 &n bsp; routine.o
<br>> .mul &n bsp; &n bsp; &n bsp; &n bsp; &n bsp;
0xb0 &n bsp; routine.o
<br>> .mul &n bsp; &n bsp; &n bsp; &n bsp; &n bsp;
0x120 & nbsp; routine.o
<br>> mcount & ;nbsp; & ;nbsp; & ;nbsp; & ;nbsp;
0x28 &n bsp; routine.o
<br>>ld: fatal: relocations remain against allocatable but non-writable
sections
<p>Hmmmm. I just tried your routine with gcc, (we don't have Sun's
compilers)
<br>% uname -a
<br>SunOS sagan 5.6 Generic_105181-09 sun4u sparc SUNW,Ultra-5_10
<br>% gcc -fPIC -c routine.c
<br>% /usr/ucb/ld -o t1.so -z text -G routine.o
<p>Under gcc, -fPIC and -fpic generate different code. Could that
be the
<br>problem?
<p>Eric
<br>--
<br>Eric Korpela &am p;nbsp; &am p;nbsp; &am p;nbsp;
| An object at rest can never be
<br>korpela@ssl.berkeley.edu
| stopped.
<br><a href="<a href="http://sag-www.ssl.berkeley.edu/~korpela">http://sag-www.ssl.berkeley.edu/~korpela</a>">Click
for home page.</a></blockquote>
Hello!
<p>Talking with my system manager, he told me that the right compilation
option is SunSolaris instead SusOs (in IDL Users Guide 4.0 they are considered
separately). In that way, I proceed with the SunSolaris options, described
in the IDL Users Guide:
<p><font size=-1>$ uname -a</font>
<br><font size=-1>SunOS mizar 5.5.1 Generic_103640-05 sun4u sparc SUNW,Ultra-Enterprise</font>
<p><font size=-1>$ cc -G -Kpic -c routine.c</font>
<br><font size=-1>$ ld -G -o routine.so routine.o</font><font size=-1></font>
<p>and no problem!
<p>Thanks all!
<pre>--
============================================================ =====
Octavi Fors Aldrich
Departament d'Astronomia i Meteorologia
Facultat de Fisica
Avgda. Diagonal 647
08028 Barcelona
SPAIN
Telf: 34-934021122
Fax: 34-934021133
e-mail: octavi@fajnm1.am.ub.es
============================================================ ===== </pre>
</html>
|
|
|