c++g++seal

link problem when build c++ code with SEAL lib


I'm writing a homomorphic encryption demo with Microsoft SEAL in c++.

I built and installed the Microsoft SEAL manually. I can see the include files under /usr/local/include/SEAL-4.1 and a lib file /usr/local/lib/libseal-4.1.a.

When I try to build my program, I run g++ -o main -I /usr/local/include/SEAL-4.1 -L /usr/local/lib -lseal-4.1 main.cpp, and it returns bounch of 'undefined reference to' error:

/usr/bin/ld: /tmp/ccZGo8L8.o: warning: relocation against `_ZN4seal4util16global_variables18global_memory_poolE' in read-only section `.text._ZN4seal16MemoryPoolHandle6GlobalEv[_ZN4seal16MemoryPoolHandle6GlobalEv]'
/usr/bin/ld: /tmp/ccZGo8L8.o: in function `generate_keypair[abi:cxx11]()':
main.cpp:(.text+0x11d): undefined reference to `seal::CoeffModulus::BFVDefault(unsigned long, seal::sec_level_type)'
/usr/bin/ld: main.cpp:(.text+0x395): undefined reference to `seal::KeyGenerator::KeyGenerator(seal::SEALContext const&)'
/usr/bin/ld: main.cpp:(.text+0x3a4): undefined reference to `seal::KeyGenerator::secret_key() const'
/usr/bin/ld: /tmp/ccZGo8L8.o: in function `encrypt(std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&, std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&)':
main.cpp:(.text+0xc4f): undefined reference to `seal::Encryptor::Encryptor(seal::SEALContext const&, seal::PublicKey const&)'
/usr/bin/ld: /tmp/ccZGo8L8.o: in function `evaluate(std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&, std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&, std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&)':
main.cpp:(.text+0x1898): undefined reference to `seal::Evaluator::Evaluator(seal::SEALContext const&)'
/usr/bin/ld: /tmp/ccZGo8L8.o: in function `decrypt(std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&, std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&, std::unique_ptr<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> > > >&)':
main.cpp:(.text+0x200e): undefined reference to `seal::Decryptor::Decryptor(seal::SEALContext const&, seal::SecretKey const&)'
/usr/bin/ld: main.cpp:(.text+0x2065): undefined reference to `seal::Decryptor::decrypt(seal::Ciphertext const&, seal::Plaintext&)'
/usr/bin/ld: /tmp/ccZGo8L8.o: in function `generate_keypair2[abi:cxx11]()':
main.cpp:(.text+0x25e1): undefined reference to `seal::CoeffModulus::BFVDefault(unsigned long, seal::sec_level_type)'
/usr/bin/ld: main.cpp:(.text+0x2706): undefined reference to `seal::BatchEncoder::BatchEncoder(seal::SEALContext const&)'
/usr/bin/ld: main.cpp:(.text+0x2948): undefined reference to `seal::KeyGenerator::KeyGenerator(seal::SEALContext const&)'
/usr/bin/ld: main.cpp:(.text+0x2957): undefined reference to `seal::KeyGenerator::secret_key() const'
...

I don't know what the problem is. I'm very new to c++ and SEAL. Is /usr/local/lib/libseal-4.1.a the wrong lib file? Or my build cmd wrong?


Solution

  • Traditionally, linkers process files from left to right, so if X depends on Y, X must be added before Y in the command. So place your source files before the libraries they depend on.

    g++ main.cpp -o main -I /usr/local/include/SEAL-4.1 -L /usr/local/lib -lseal-4.1