Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling StaticTools.dlopen vs StaticCompiler.dlopen #51

Open
mdmaas opened this issue May 25, 2023 · 1 comment
Open

Calling StaticTools.dlopen vs StaticCompiler.dlopen #51

mdmaas opened this issue May 25, 2023 · 1 comment

Comments

@mdmaas
Copy link

mdmaas commented May 25, 2023

Hi,

I'm trying to compile code which calls external shared libraries (like a static version of ccall, see also here)

### Let's define a C function and compile it
using Libdl
C_code= """
       double mean(double a, double b) {
         return ( a + b ) / 2;
       }
       """
Clib = "clib"
open(`gcc -fPIC -O3 -xc -shared -o $(Clib * "." * Libdl.dlext) -`, "w") do f
    print(f, C_code)
end

# Now the julia code

using StaticTools, StaticCompiler

function test()
    lib_path = c"/home/martin/Desktop/extras/static_compiler_test/static_ccall/clib.so"
    func_name = c"mean"

    lib = StaticCompiler.dlopen(lib_path)
    mean = StaticCompiler.dlsym(lib, func_name)
    a = 1.0
    b = 2.0
    c = @ptrcall mean(a::Float64, b::Float64) :: Float64
    printf(c"Result is %f:\n", c)

    return 0
end

test()

Of course, I would like to compile it, but if I replace StaticCompiler.dlopen with StaticTools.dlopen (which would compile) it doesn't work (I got a segfault):

[5684] signal (11.1): Segmentation fault
in expression starting at /home/martin/Desktop/extras/static_compiler_test/static_ccall/test_mean.jl:53
unknown function (ip: (nil))
Allocations: 1333178 (Pool: 1332179; Big: 999); GC: 2
Segmentation fault

Do you think there could be a problem with StaticTools.dlopen and/or with @ptrcall?

@mdmaas mdmaas changed the title Possible issue with StaticTools.dlopen and @ptrcall Calling StaticTools.dlopen vs StaticCompiler.dlopen May 25, 2023
@mdmaas
Copy link
Author

mdmaas commented May 25, 2023

Oh, I think there is no problem with @ptrcall, it's only that I'm being able to use StaticTools.dlopen properly, as it returns a null pointer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant