Loading/Using a dynamic library in a Xeus-cling notebook

Hi all,

I wasn’t able to find a more suitable forum/topic to discuss my problem. Please re-direct if this isn’t the right place to do so.

I am trying to use a C++ framework for a course (do demonstrations & display code stubs & print charts statically & dynamically), and chose Xeus-Cling as it integrates well into the Jupyter environment. I am trying to understand how dynamic libraries are loaded and used within the environment, but I am getting errors that I don’t know how to resolve. I started with this code stub that implements a trivial square root function.

"##mylib.h
#ifndef MYLIB_H
#define MYLIB_H
double calcSqrt(double);
#endif

##calc.c
#include <math.h>
#include “mylib.h”
double calcSqrt(double d)
{
return sqrt(d);
}

#include <stdio.h>
#include “mylib.h”

/##main.c
int main()
{
double d = 100;
printf(“sqrt(%3.0f)=%2.0f\n”,d,calcSqrt(d));
return 0;
}

I was able to compile the ‘calc.c’ as a dynamic library, and load it with main to get the result. The commands below did not receive any error.

gcc -c calc.c
gcc -c main.c
gcc -shared -fPIC -o libcalc.so calc.o
gcc main.o ./libcalc.so -lm
./a.out

However, when I ported it over to the Jupyter environment, I got an error.

#pragma cling add_library_path("<path_of_project>")
#pragma cling load(“libcalc”)

#include “mylib.h”

calcSqrt(100)

This is the error I got. I can’t figure out why since an objdump indicates that function is in the library (.so) file.

"IncrementalExecutor::executeFunction: symbol ‘_Z8calcSqrtd’ unresolved while linking [cling interface function]! You are probably missing the definition of calcSqrt(double) Maybe you need to load the corresponding shared library?

Interpreter Error:"

Any help will be greatly appreciated. Thanks in advance!

Ed

The problem has been resolved. Simply make use of g++ rather than gcc.

g++ -c calc.c
g++ -c main.c
g++ -shared -fPIC -o libcalc.so calc.o
g++ main.o ./libcalc.so -lm
./a.out