I have to use HElib for my thesis, so I installed it and tried the examples from HElib/examples/tutorial, in order to start encoding some data. I installed HElib following the INSTALL.md document and ran the examples as said in examples/README.md. I'm able to build the examples, get their executables and run them.
Then I tried writing a little program and compile it, but I obtain some errors. If I just include the header of HElib the program compiles and runs correctly, as in:
#include <helib/helib.h>
using namespace std;
using namespace helib;
int main(int argc, char* argv[])
{
cout << "hello world" << "\n";
return 0;
}
But when I try to use some HElib classes the program does not compile with "undefined reference" errors. The code I'm trying to compile is simple and taken from the HElib examples:
#include <helib/helib.h>
using namespace std;
using namespace helib;
int main(int argc, char* argv[])
{
Context context = ContextBuilder<CKKS>().m(16 * 1024).bits(119).precision(20).c(2).build();
cout << "distance=" << "\n";
return 0;
}
I'm trying compiling it with:
g++-11 encrypt_mnist.cpp -o encrypt_mnist -I/home/lulu/helib_install/helib_pack/include
and the error I get in this case is:
/usr/bin/ld: /tmp/cccLr3H1.o: in function `main':
encrypt_mnist.cpp:(.text+0x9e): undefined reference to `helib::ContextBuilder<helib::CKKS>::build() const'
/usr/bin/ld: /tmp/cccLr3H1.o: in function `NTL::ZZ::Deleter::apply(_ntl_gbigint_body*)':
encrypt_mnist.cpp:(.text._ZN3NTL2ZZ7Deleter5applyEP17_ntl_gbigint_body[_ZN3NTL2ZZ7Deleter5applyEP17_ntl_gbigint_body]+0x18): undefined reference to `_ntl_gfree(_ntl_gbigint_body*)'
collect2: error: ld returned 1 exit status
I tried also compiling it with
g++-11 encrypt_mnist.cpp -o encrypt_mnist -I/home/lulu/helib_install/helib_pack/lib
g++-11 encrypt_mnist.cpp -o encrypt_mnist -L/home/lulu/helib_install/helib_pack/lib
g++-11 encrypt_mnist.cpp -o encrypt_mnist -L/home/lulu/helib_install/helib_pack/lib -lhelib
g++-11 encrypt_mnist.cpp -o encrypt_mnist -I/home/lulu/helib_install/helib_pack/lib -L/home/lulu/helib_install/helib_pack/lib -lhelib
but none of the above options seem to work. The first three for example give me a "no such file or directory" error with respect to helib.h, so obviously they are not correct. The latter instead produces a lot of errors like the one I posted above (undefined reference with respect to the classes in HElib).
My knowledge of C++ is very low, but no one seems to be able to help me.
Thanks in advance for the help.
EDIT (SOLUTION)
The right command was:
g++ -g -O2 -std=c++17 -pthread -march=native encrypt_mnist.cpp -o encrypt_mnist -I/home/lulu/helib_install/helib_pack/include -L/home/lulu/helib_install/helib_pack/lib -lhelib -lntl -lgmp -lm
I added all the flags I needed thanks to this question; then I linked the helib.h file in the include directory with the -I option, and also the helib.a file in the lib directory with the -L option. The version of C++ needed for this turned out to be 17, so I added -std=c++17. Finally I linked all the necessary libraries with -lhelib, -lntl, -lgmp, -lm.