6

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?

DarkLink
  • 355
  • 4
  • 16
  • you might need a `GC.@preserve` on `X` to prevent Julia from GCing it – Oscar Smith Jan 12 '22 at 15:59
  • 1
    @OscarSmith that's certainly not the issue, though, as the passed array is a global. I'd wager something with Windows; I've only ever tested this on Mac and Linux... and most Windows folks don't have GCC on the path to even make it this far. – mbauman Jan 12 '22 at 16:29
  • I have been doing some testing. I am able to ccall(:clock,Int32,()) without any errors. So the clock function in time.h appears to be working. This leads me to believe there must be some issue when creating the shared library.. – DarkLink Jan 12 '22 at 17:58
  • Maybe your CPU doesn't have the `sse3` instruction set? How old is it? What if you drop the `-msse3` flag? – mbauman Jan 12 '22 at 18:16
  • I have an intel i7-8700 about 4 years old. Dropping the `-msse3` flag doesn't show any changes as far as I can tell. – DarkLink Jan 12 '22 at 18:43
  • It looks like for arbitrary files, the `gcc` code creates a `.dll` file, which I believe are windows specific. Maybe this could be the culprit. Either how it is saved or maybe some compatibility issue with ccall.. How minggw works is a slight mystery – DarkLink Jan 12 '22 at 19:09
  • Could that be a linkage problem? In fact, you don't need to use gcc on Windows, you could prefix your function with `__declspec(dllexport)` and compile the code with MSVC. Then everything should work fine. – Gnimuc Jan 16 '22 at 06:41
  • @Gnimuc, thanks I will look into compiling the code with MSVC – DarkLink Jan 16 '22 at 18:34

0 Answers0