Reputation: 355
I am following along with some example code from JuliaAcademy (https://github.com/JuliaAcademy).
I am trying to run C code from within Julia. I am using Windows 10, 64-bit and I have gcc installed on my computer and the Atom IDE to run Julia.
Here is the snippet of code I am running:
using Libdl
C_code = """
#include <stddef.h>
double c_sum(size_t n, double *X) {
double s = 0.0;
for (size_t i = 0; i < n; ++i) {
s += X[i];
}
return s;
}
"""
const Clib = tempname() # make a temporary file
# compile to a shared library by piping C_code to gcc
# (works only if you have gcc installed):
open(`gcc -fPIC -O3 -msse3 -xc -shared -o $(Clib * "." * Libdl.dlext) -`, "w") do f
print(f, C_code)
end
# define a Julia function that calls the C function:
c_sum(X::Array{Float64}) = ccall(("c_sum", Clib), Float64, (Csize_t, Ptr{Float64}), length(X), X)
a = rand(10^7)
c_sum(a)
the open gcc command executes and each line of code appears to run fine until the last line when I call c_sum(a).
Once I call the c_sum() function, Julia exits and produces the following error message:
Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x88804 -- unknown function (ip: 0000000000088804)
in expression starting at D:\Documents\Julia Projects\benchmarking.jl:67
unknown function (ip: 0000000000088804)
Allocations: 50200883 (Pool: 50186825; Big: 14058); GC: 46
I think that being able to run and execute C functions from within Julia is very useful so I would like to be able to have a working example on my computer.
Can anyone help me with this? Maybe it is that I am using a Windows PC? It has been a while since I had installed gcc on my windows machine. Should the above code work with gcc installed via MingGW or CygWin? Or does Julia assume the machine you are running is Unix/Linux?
Upvotes: 6
Views: 355