0

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.

  • The instructions say you have to link to the library generated in the lib folder: [https://github.com/homenc/HElib/blob/master/INSTALL.md#standard-method](https://github.com/homenc/HElib/blob/master/INSTALL.md#standard-method) – drescherjm Apr 20 '22 at 10:48
  • Thank you for the answer. I tried also compiling with -I/home/lulu/helib_install/helib_pack/lib -L/home/lulu/helib_install/helib_pack/lib -L/home/lulu/helib_install/helib_pack/lib -lhelib -I/home/lulu/helib_install/helib_pack/lib -L/home/lulu/helib_install/helib_pack/lib -lhelib But none of them seems to work. The latter, in fact, produces many errors similar to the one I posted in the question. It seems like I'm able to link the library (in fact I don't receive the "no such file or directory error"), but the compiler can't find the classes he needs. – luluSaponetta Apr 20 '22 at 11:03
  • Please edit your question and add the exact text of the new command you tried. – drescherjm Apr 20 '22 at 11:08
  • I think you need `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 -lntl` because the linker error is about `_ntl_gfree` – drescherjm Apr 20 '22 at 11:19
  • I found this question for help with that: [https://stackoverflow.com/questions/12139285/compiler-errors-using-ntl-library](https://stackoverflow.com/questions/12139285/compiler-errors-using-ntl-library) – drescherjm Apr 20 '22 at 11:20
  • Got it! I'm posting the solution as an edit in the question. Thank you guys I really appreciate your help, you saved my day! – luluSaponetta Apr 20 '22 at 13:06
  • I voted to reopen to be able to answer this specific problem with a proper answer for this library instead of putting the answer in the question or closing as a duplicate for the more general resolve unresolved externals problem. – drescherjm Apr 20 '22 at 13:10

0 Answers0