#include <julia.h>
#include <iostream>
using namespace std;
struct TestType {
double a;
double b;
};
int main(int argc, char *argv[])
{
TestType A, *ret;
A.a=2.;
A.b=3.;
jl_init(NULL);
jl_load("TestArg.jl");
jl_value_t * mod = (jl_value_t*)jl_eval_string("TestArg");
jl_function_t * func = jl_get_function((jl_module_t*)mod,"TestFunc");
jl_call1(func, (jl_value_t *)&A);
jl_atexit_hook(0);
return 0;
}
# TestArg.jl
module TestArg
type TestType
a::Float64
b::Float64
end
function TestFunc(data::TestType)
println("in TestArg.TestFunc ")
end
end
#include <julia.h>#include <iostream>using namespace std;struct TestType {double a;double b;};int main(int argc, char *argv[]){TestType A, *ret;A.a=2.;A.b=3.;
jl_init("/Users/inorton/git/jl71/usr/lib/");
jl_load("TestArg.jl");jl_value_t * mod = (jl_value_t*)jl_eval_string("TestArg");
// construct a TestType instance
jl_value_t* jl_A = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "TestType"),jl_box_float64(A.a), jl_box_float64(A.b));
jl_function_t * func = jl_get_function((jl_module_t*)mod,"TestFunc");
jl_call1(func, jl_A);jl_atexit_hook(0);return 0;}
// construct a TestType instancejl_value_t* jl_A = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "TestType"),jl_box_float64(A.a), jl_box_float64(A.b));
Bart,Can you explain what you mean by "need to be rooted"? The jl_new_struct statement as Isaiah suggested works, why do we need the additional statements as you suggested?
On Fri, Sep 9, 2016 at 8:36 PM, K leo <cnbi...@gmail.com> wrote:Bart,Can you explain what you mean by "need to be rooted"? The jl_new_struct statement as Isaiah suggested works, why do we need the additional statements as you suggested?Because it doesn't, it'll segfault at any time if the GC feels like giving you a bad day.
http://julia.readthedocs.io/en/latest/manual/embedding/#memory-management
I tried to compile the code with the GC statements, but got a bunch of error regarding jl_tls_states:
julia-config.jl is hardly useful at all. I unpacked the Linux binary of Julia (0.5) in ~/Software/julia-9c76c3e89a, and I set JULIA_DIR accordingly. When I execute julia-config in the following way, I got all kinds of "undefined reference to" error messages.> $JULIA_DIR/share/julia/julia-config.jl --cflags --ldflags --ldlibs | xargs g++ test6.cpp/usr/bin/ld: warning: libLLVM-3.7.1.so, needed by /home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so, not found (try using -rpath or -rpath-link)/home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so: undefined reference to `llvm::AttributeSet::hasAttribute(unsigned int, llvm::Attribute::AttrKind) const'/home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so: undefined reference to `llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getLoopLatch() const'.........If I only execute julia-config for flags, and I use these flags to cook up g++ script, and the g++ script got me the same problems.> $JULIA_DIR/share/julia/julia-config.jl --cflags --ldflags --ldlibs-DJULIA_ENABLE_THREADING=1 -fPIC -DJULIA_INIT_DIR=\"/home/xxx/Software/julia-9c76c3e89a/lib\" -I/home/xxx/Software/julia-9c76c3e89a/include/julia-L/home/xxx/Software/julia-9c76c3e89a/lib-Wl,-rpath,/home/xxx/Software/julia-9c76c3e89a/lib -ljuliaThe output for --ldflags is clearly incorrect or incomplete for my installation on Ubuntu. The script that generally works for me is the following, where some extra things have to be set.> g++ -o test -fPIC -I$JULIA_DIR/include/julia test6.cpp -L$JULIA_DIR/lib/ -L$JULIA_DIR/lib/julia -lLLVM-3.7.1 -ljulia $JULIA_DIR/lib/julia/libstdc++.so.6
The concrete error you got is probably due to the missing -DJULIA_ENABLE_THREADING.
On Sat, Sep 10, 2016 at 8:53 AM, K leo <cnbi...@gmail.com> wrote:julia-config.jl is hardly useful at all. I unpacked the Linux binary of Julia (0.5) in ~/Software/julia-9c76c3e89a, and I set JULIA_DIR accordingly. When I execute julia-config in the following way, I got all kinds of "undefined reference to" error messages.> $JULIA_DIR/share/julia/julia-config.jl --cflags --ldflags --ldlibs | xargs g++ test6.cpp/usr/bin/ld: warning: libLLVM-3.7.1.so, needed by /home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so, not found (try using -rpath or -rpath-link)/home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so: undefined reference to `llvm::AttributeSet::hasAttribute(unsigned int, llvm::Attribute::AttrKind) const'/home/xxx/Software/julia-9c76c3e89a/lib/libjulia.so: undefined reference to `llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getLoopLatch() const'.........If I only execute julia-config for flags, and I use these flags to cook up g++ script, and the g++ script got me the same problems.> $JULIA_DIR/share/julia/julia-config.jl --cflags --ldflags --ldlibs-DJULIA_ENABLE_THREADING=1 -fPIC -DJULIA_INIT_DIR=\"/home/xxx/Software/julia-9c76c3e89a/lib\" -I/home/xxx/Software/julia-9c76c3e89a/include/julia-L/home/xxx/Software/julia-9c76c3e89a/lib-Wl,-rpath,/home/xxx/Software/julia-9c76c3e89a/lib -ljuliaThe output for --ldflags is clearly incorrect or incomplete for my installation on Ubuntu. The script that generally works for me is the following, where some extra things have to be set.> g++ -o test -fPIC -I$JULIA_DIR/include/julia test6.cpp -L$JULIA_DIR/lib/ -L$JULIA_DIR/lib/julia -lLLVM-3.7.1 -ljulia $JULIA_DIR/lib/julia/libstdc++.so.6The two compilation appears to be running against two independent installation and they are not comparable. Assuming the private libraries are found in lib/julia the julia-config.jl script should probably be updated to include it though.
Hi Bart,Do you think "jl_A" should also be rooted as it is later used as the argument?
Hi Bart,These are meant to call Julia code from C++. You mentioned "there may be easier ways using Cxx.jl or CxxWrap.jl". Are the two packages only for calling C/C++ from Julia, and not the otherway around? Am I missing something?