Reputation: 377
I'm trying to compile the following C++ code
#include <julia.h>
int main(int argc, char *argv[])
{
/* required: setup the julia context */
jl_init(NULL);
/* run julia commands */
jl_eval_string("print(sqrt(2.0))");
/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook();
return 0;
}
I compile the code using
gcc -I/usr/include/julia -L/usr/lib/x86_64-linux-gnu/julia -ljulia juliatest.cpp -o test
I get the following error-
juliatest.cpp:19:20:: error: too few arguments to function ‘void jl_atexit_hook(int)’
jl_atexit_hook();
^
In file included from juliatest.cpp:1:0:
/usr/include/julia/julia.h:1188:16: note: declared here
DLLEXPORT void jl_atexit_hook(int status);
^
If I remove jl_atexit_hook();
from the code, I get the following errors-
juliatest.cpp:(.text+0x1a): undefined reference to `jl_init_with_image'
juliatest.cpp:(.text+0x24): undefined reference to `jl_eval_string'
What am I doing wrong?
Upvotes: 0
Views: 1387
Reputation: 628
The example you are trying to compile is a little bit outdated. As already mentioned you need to give an exitcode
to the jl_atexit_hook()
function. The linker message is about missing functions defined in libraries. To get rid of distribution details I downloaded and build the tarball. Now the example can be build using this makefile:
JULIA_DIR:=julia-0.4.2
JULIA_LIB:=$(JULIA_DIR)/usr/lib
JULIA_SRC:=$(JULIA_DIR)/src
JULIA_INC:=$(JULIA_DIR)/usr/include
CPPFLAGS:=-I$(JULIA_INC) -I$(JULIA_SRC) -I$(JULIA_SRC)/support
LDFLAGS:=-L$(JULIA_LIB)
LDLIBS=-ljulia
export LD_LIBRARY_PATH:=$(JULIA_LIB):$(JULIA_LIB)/julia
all: main
run: main
@./main
clean:
rm -f main
If you now type make run
you will get the next error message about a wrong path the system image is searched in. As Thomas noted here the function jl_init() is creating a context that may fail in this case. We shall give the name and the path of the system image to the init function using jl_init_with_image("julia-0.4.2/usr/lib/julia", "sys.so")
instead. This is an ugly hard coded path and can surely be replaced. But for getting started with this example and to get this problem known, it is enough. The corrected example is this:
#include <julia.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
/* required: setup the julia context */
jl_init_with_image("julia-0.4.2/usr/lib/julia", "sys.so");
/* run julia commands */
jl_eval_string("print(sqrt(2.0))");
/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook(0);
putchar('\n');
return 0;
}
Running make run
will now be a quite complicated way to calculate the square root of 2 :-)
Have fun.
Upvotes: 1