to suggestions --> two suggestions
On Monday, June 22, 2015 at 12:23:18 AM UTC+9, Jan Strube wrote:
>
> Daniel,
>
> I tried your code and could reproduce the problem: I have to suggestions:
> -- Julia is 1-based, so drop the 0 in your unsafe_load. The default
> parameter is 1, which is what you want in this case.
> -- I can't really understand what your function is supposed to do (and my
> C is a bit rusty), but this simpler version works for me.
> I suggest to start there and add the additional functionality of
> particles_add bit by bit.
>
> Good luck.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int N = 7;
> int Nmax = 9;
>
> struct particle {
> double x;
> double y;
> double z;
> };
>
> struct particle* particles = NULL;
>
> void particles_add(struct particle pt){
> printf("%.2f\t%.2f\t%.2f\n", pt.x, pt.y, pt.z);
> }
>
>
>
> On Sunday, June 21, 2015 at 11:42:06 PM UTC+9, Daniel Carrera wrote:
>>
>> Hello,
>>
>> I'm trying to learn how to call C libraries from Julia. So I wrote a
>> simple C library that does nothing in particular and I am having trouble
>> talking to that library from Julia. Here is my library:
>>
>> ---------------------------------------------
>> % cat foo.c
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int N = 7;
>> int Nmax = 9;
>>
>> struct particle {
>> double x;
>> double y;
>> double z;
>> };
>>
>> struct particle* particles = NULL;
>>
>> void particles_add(struct particle pt){
>> while (Nmax <= N){
>> Nmax += 128;
>> particles = realloc(particles,sizeof(struct particle)*Nmax);
>> }
>> particles[N] = pt;
>> N++;
>> }
>>
>> ---------------------------------------------
>> % make
>> gcc -Wall -std=c99 -Wpointer-arith -c -fPIC foo.c
>> gcc -Wall -std=c99 -Wpointer-arith -shared -o libfoo.so foo.o
>> ---------------------------------------------
>>
>>
>> So, there are two things I would like to do from Julia: I'd like to read
>> the variable "N" and I'd like to create a "particle" struct and pass it to
>> the "particles_add()" function. I used the documentation on this page:
>>
>> http://julia.readthedocs.org/en/latest/manual/calling-c-and-fortran-code/
>>
>> Based on what I read, this is what I came up with:
>>
>> ---------------------------------------------
>> % cat foo.jl
>> const lib = "./libfoo.so"
>>
>> N() = unsafe_load(cglobal(("N" ,lib), Cint), 0)
>> Nmax() = unsafe_load(cglobal(("Nmax",lib), Cint), 0)
>>
>> immutable Particle
>> x ::Cdouble
>> y ::Cdouble
>> z ::Cdouble
>> end
>>
>> function add(pt::Particle)
>> ccall(("particles_add", lib), Void, (Particle,), pt)
>> end
>>
>> function add(;x=0,y=0,z=0)
>> add(Particle(x,y,z))
>> end
>> ---------------------------------------------
>>
>>
>> However, nothing here works: I cannot read "N" (I get gibberish) and I
>> cannot call "particles_add()" (I get a segfault):
>>
>> ---------------------------------------------
>> % julia
>> ...
>> julia> include("foo.jl")
>> add (generic function with 2 methods)
>>
>> julia> N()
>> 32570
>>
>> julia> Nmax()
>> 7
>>
>> julia> add(x=0)
>>
>> signal (11): Segmentation fault
>> particles_add at ./libfoo.so (unknown line)
>> julia_add_20314 at (unknown line)
>> jlcall_add_20314 at (unknown line)
>> unknown function (ip: -490800184)
>> unknown function (ip: -490804016)
>> unknown function (ip: -490736582)
>> jl_f_top_eval at /usr/bin/../lib/x86_64-linux-gnu/julia/libjulia.so
>> (unknown line)
>> eval_user_input at REPL.jl:53
>> jlcall_eval_user_input_20278 at (unknown line)
>> jl_apply_generic at /usr/bin/../lib/x86_64-linux-gnu/julia/libjulia.so
>> (unknown line)
>> anonymous at task.jl:95
>> jl_handle_stack_switch at
>> /usr/bin/../lib/x86_64-linux-gnu/julia/libjulia.so (unknown line)
>> julia_trampoline at /usr/bin/../lib/x86_64-linux-gnu/julia/libjulia.so
>> (unknown line)
>> unknown function (ip: 4199613)
>> __libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
>> unknown function (ip: 4199667)
>> unknown function (ip: 0)
>> zsh: segmentation fault (core dumped) julia
>> ---------------------------------------------
>>
>>
>> So, at this point I am really stuck. Clearly I have missed a lot of
>> fundamental issues in either my C code, or my Julia code, or both. Can
>> anyone figure out what I'm doing wrong?
>>
>>
>> Cheers,
>> Daniel.
>>
>