Last active
November 30, 2022 19:03
-
-
Save brenhinkeller/58288c27146dd7a2b869fc0240726207 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> using StaticCompiler, StaticTools | |
julia> hello() = println(c"Hello from compiled Julia!") | |
hello (generic function with 1 method) | |
julia> # Functions to compile into a single shlib | |
# add more (name, (argtypes..)) tuples to the array to add more functions | |
funcs = [(hello, ())] | |
1-element Vector{Tuple{typeof(hello), Tuple{}}}: | |
(hello, ()) | |
julia> # Compile into shared library | |
compile_shlib(funcs, "./", filename="libhello") | |
"/Users/cbkeller/libhello.dylib" | |
shell> ls -alh libhello.dylib | |
-rwxr-xr-x 1 cbkeller staff 32K Nov 30 11:31 libhello.dylib | |
shell> otool -tv libhello.dylib | |
libhello.dylib: | |
(__TEXT,__text) section | |
_julia_hello: | |
0000000000003f60 subq $0x28, %rsp | |
0000000000003f64 movups 0x30(%rip), %xmm0 | |
0000000000003f6b movups %xmm0, 0xb(%rsp) | |
0000000000003f70 movaps 0x19(%rip), %xmm0 | |
0000000000003f77 movaps %xmm0, (%rsp) | |
0000000000003f7b movq %rsp, %rdi | |
0000000000003f7e callq 0x3f8a | |
0000000000003f83 xorl %eax, %eax | |
0000000000003f85 addq $0x28, %rsp | |
0000000000003f89 retq | |
julia> ccall(("julia_hello", "libhello"), Int32, (), ) | |
Hello from compiled Julia! | |
0 | |
julia> # Call a symbol from that library | |
# (would have to `dlopen` the library first to use un-StaticCompiler'd) | |
function dlhello_linked() | |
@symbolcall julia_hello()::Int32 | |
end | |
dlhello_linked (generic function with 1 method) | |
julia> # Compile executable that links against libhello | |
compile_executable(dlhello_linked, (), "./", | |
# -lhello tells the compiler and linker to look for a shlib called "libhello" | |
# -L$(pwd()) tells them where to look | |
cflags=`-lhello -L$(pwd())` | |
) | |
"/Users/cbkeller/dlhello_linked" | |
shell> ./dlhello_linked | |
Hello from compiled Julia! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment