Hello again,
I am running Julia Version 0.4.0-dev+5841 (installed from source code)
and currently run Ubuntu 15.04.
I am trying to write a julia file that will call some c++ classes and
functions, where inside
these functions some other Julia functions will be used and I face some
errors.
Let me get things one by one.
This is my c++ files:
1) The cpp of the class (ArrayMaker.cpp):
#include "ArrayMaker.h"
#include <iostream>
using namespace std;
ArrayMaker::ArrayMaker() {
// TODO Auto-generated constructor stub
}
ArrayMaker::~ArrayMaker() {
// TODO Auto-generated destructor stub
}
double ArrayMaker::ArrayMak(int iNum, float fNum) {
jl_init_with_image("/home/kostav/julia/usr/lib/julia", "sys.so");
jl_eval_string("print(sqrt(2.0))");
cout << "Got arguments: " << iNum << ", and " << fNum << endl;
double sol;
jl_load("/home/kostav/.julia/v0.4/loleee/src/loleee.jl");
jl_value_t * mod = (jl_value_t*)jl_eval_string("loleee");
jl_function_t * func =
jl_get_function((jl_module_t*)mod,"funior");
jl_value_t * argument = jl_box_float64(2.0);
jl_value_t * argument2 = jl_box_float64(3.0);
jl_value_t * ret = jl_call2(func, argument, argument2);
sol = jl_unbox_float64(ret);
iNumber = iNum;
fNumber = fNum;
fArr = new float[iNumber];
jl_atexit_hook();
return sol;
}
float* ArrayMaker::fillArr() {
cout << "Filling the array" << endl;
for (int i=0; i < iNumber; i++) {
fArr[i] = fNumber;
fNumber *= 2;
}
return fArr;
}
2) The equivelant header file (ArrayMaker.h):
#ifndef ARRAYMAKER_H
#define ARRAYMAKER_H
#include <julia.h>
class ArrayMaker
{
public:
ArrayMaker();
virtual ~ArrayMaker();
double ArrayMak(int, float);
float* fillArr();
private:
int iNumber;
float fNumber;
float* fArr;
};
#endif
3) The main file (main.cpp):
#include "ArrayMaker.h"
#include <iostream>
using namespace std;
int main(int argc, char ** argv) {
ArrayMaker* FI_CSP=new ArrayMaker();
int a = 7;
float b = 4;
double z;
z = FI_CSP->ArrayMak(a,b);
cout<<endl<<"Test = "<< z;
return 1;
}
Now inside the function ArrayMak I call a julia function that just takes
two arguements and adds them and returns the value of the sum
this function is called by a module I made under the name lolee. Note that
when I compile and run this code with the equivelant makefile
it works properly.
Now next thing I do is to create the shared library that I need to pass to
the cxx, just so that you can have a look in every single line I write
this is the makefile I use to create the shared library:
# define the C compiler to use
CC = g++
# define any compile-time flags
CFLAGS = -fPIC -Wall -g -Wl,-rpath,/home/kostav/julia/usr/lib
LDFLAGS = -shared
# define any directories containing header files other than /usr/include
#
INCLUDES = -I /home/kostav/julia/src -I /home/kostav/julia/usr/include -I
/home/kostav/julia/src/support -I /home/kostav/julia/contrib
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
LFLAGS = -L/home/kostav/julia/usr/lib
# define any libraries to link into executable:
# if I want to link in libraries (libx.so or libx.a) I use the -llibname
# option, something like (this will link in libmylib.so and libm.so:
LIBS = -ljulia -lLLVM-3.7svn
# define the C source files
SRCS = main.cpp ArrayMaker.cpp
# define the C object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)
# define the shared library
MAIN = test.so
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
.PHONY: depend clean
all: $(MAIN)
@echo Simple compiler named mycc has been
compiled
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
$(LDFLAGS)
# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o
file)
# (see the gnu make manual section about automatic variables)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
$(RM) *.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
So after that I end up with the shared library test.so. Then I write the
following julia file where I try to do a call
of the c++ class that has inside the call to the julia function to check
that it works with the following julia file:
using Cxx
# Importing shared library and header file
const path_to_lib = "/home/kostav/Documents/Project/julia/cle"
#const path2 = "/home/kostav/julia/src/julia.h" #pwd()
addHeaderDir(path_to_lib, kind=C_System)
Libdl.dlopen(path_to_lib * "/test.so", Libdl.RTLD_GLOBAL)
#include("/home/kostav/julia/src/julia.h")
cxxinclude("ArrayMaker.h")
maker = @cxxnew ArrayMaker()
a = @cxx maker->ArrayMak(5, 2.0)
So when I run this file in julia I receive the following outcome:
In file included from :1:
/home/kostav/Documents/Project/julia/cle/ArrayMaker.h:4:10: fatal error:
'julia.h' file not found
#include <julia.h>
^
1.4142135623730951Got arguments: 5, and 2
And actually when julia crushes when it reaches to the point to run the
command:
" jl_value_t * ret = jl_call2(func, argument, argument2);"
Actually is doesn't crush, what I get in the terminal is just the cursos
moving as if a process is loading,
but it does it for 10 minutes now, so I assume sth is going wrong.
Any ideas what I should do different?
In general you think is valid what I am trying to do (use cxx to call
self-written c++ classes where inside I call
self-written julia functions)?
In general I do this kind of functionality a lot and that's why I am asking.
I am really not sure what to do on this point.
Any kind of help would be grateful.