Hi Yuyichao,
thanks for your comments.
If I may post my code here so that you can advise me better on what I should change.
For simplicity reasons, I reduced the size of the arrays to 3.
Here comes my module:
module MyModule
struct the_numbers
the_integers::NTuple{3,Cint}
the_integers_2::NTuple{3, Cint}
the_doubles::NTuple{3,Cdouble}
end
struct test_struct_1
a::Cint
my_nums::the_numbers
end
Base.@ccallable function print_struct(my_struct::test_struct_1)::Cdouble
println(my_struct.a)
println(my_struct.my_nums.the_doubles[1])
return my_struct.my_nums.the_doubles[1] + my_struct.my_nums.the_doubles[2];
end
if abspath(PROGRAM_FILE) == @__FILE__
my_numbers = the_numbers(ntuple(i -> i*1 ,3),ntuple(i -> (i+4)*2,3),ntuple(i -> (i + 7.7)*3,3))
my_n_struct = test_struct_1(3, my_numbers)
res = print_struct(my_n_struct)
println("Res is: ", res)
end
end
Then I create the precompile by doing:
julia --startup-file=no --trace-compile=MyModule_precompile.jl MyModule.jl
Now it is time to create the custom system image. First I create the file:
Base.init_depot_path()
Base.init_load_path()
@eval Module() begin
Base.include(@__MODULE__, "MyModule.jl")
for (pkgid, mod) in Base.loaded_modules
if !(pkgid.name in ("Main", "Core", "Base"))
eval(@__MODULE__, :(const $(Symbol(mod)) = $mod))
end
end
for statement in readlines("MyModule_precompile.jl")
try
Base.include_string(@__MODULE__, statement)
catch
# See julia issue #28808
Core.println("failed to compile statement: ", statement)
end
end
end # module
empty!(LOAD_PATH)
empty!(DEPOT_PATH)
and then:
julia --startup-file=no --cpu-target="generic" --output-o sys.o -J"lib/julia/sys.so" create_sysimage.jl
and
gcc -shared -o libsys.so -Wl,--whole-archive sys.o -Wl,--no-whole-archive -L"lib/" -ljulia
then I wrote a small C++ code:
#include <iostream>
#include <string>
#include <julia.h>
JULIA_DEFINE_FAST_TLS()
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int the_integers[3];
int the_integers_2[3];
double the_doubles[3];
}the_numbers;
typedef struct
{
int a;
the_numbers my_nums;
}test_struct_1;
extern double print_struct(test_struct_1 my_struct);
int main()
{
// jl_init();
jl_options.image_file = JULIAC_PROGRAM_LIBNAME;
julia_init(JL_IMAGE_JULIA_HOME);
test_struct_1 my_struct;
my_struct.a = 4;
my_struct.my_nums.the_integers[0] = 1;
my_struct.my_nums.the_integers[1] = 2;
my_struct.my_nums.the_integers[2] = 3;
my_struct.my_nums.the_integers_2[0] = 4;
my_struct.my_nums.the_integers_2[1] = 5;
my_struct.my_nums.the_integers_2[2] = 6;
my_struct.my_nums.the_doubles[0] = 7.7;
my_struct.my_nums.the_doubles[1] = 8.8;
my_struct.my_nums.the_doubles[2] = 9.9;
double res = print_struct(my_struct);
std::cout << "From C++: " << res << std::endl;
jl_atexit_hook(0);
return 0;
}
#ifdef __cplusplus
}
#endif
and compile as:
g++ -DJULIAC_PROGRAM_LIBNAME=\"libsys.so\" \
-o call_MyModule call_MyModule.cpp \
-L'./' -lsys -O2 -fPIE -I'include/julia' \
-L'lib' -ljulia lib/julia/libstdc++.so.6