1

When I want to create a static build for my code (I want to run this code on the server) I get a strange problem:

/usr/local/lib/libntl.a(lip.o): In function `redc(void*, void*, long, unsigned long, void*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:3515: undefined reference to `__gmpn_addmul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:3527: undefined reference to `__gmpn_sub_n'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_rem_struct_basic::eval(long*, void*, _ntl_tmp_vec*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5709: undefined reference to `__gmpn_mod_1'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_reduce_struct_montgomery::eval(void**, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:3600: undefined reference to `__gmpn_addmul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:3612: undefined reference to `__gmpn_sub_n'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_crt_struct_basic::eval(void**, long const*, _ntl_tmp_vec*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:4709: undefined reference to `__gmpn_addmul_1'
/usr/local/lib/libntl.a(lip.o): In function `gmod_simple(void*, void*, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2067: undefined reference to `__gmpn_tdiv_qr'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_rem_struct_fast::eval(long*, void*, _ntl_tmp_vec*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5749: undefined reference to `__gmpn_mod_1'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_rem_struct_medium::eval(long*, void*, _ntl_tmp_vec*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5793: undefined reference to `__gmpn_mod_1'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_grshift(void*, long, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1420: undefined reference to `__gmpn_rshift'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_glshift(void*, long, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1355: undefined reference to `__gmpn_lshift'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gadd(void*, void*, void**)':
/usr/include/gmp.h:2194: undefined reference to `__gmpn_sub_n'
/usr/include/gmp.h:2139: undefined reference to `__gmpn_add_n'
/usr/include/gmp.h:2194: undefined reference to `__gmpn_sub_n'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsub(void*, void*, void**)':
/usr/include/gmp.h:2139: undefined reference to `__gmpn_add_n'
/usr/include/gmp.h:2194: undefined reference to `__gmpn_sub_n'
/usr/include/gmp.h:2194: undefined reference to `__gmpn_sub_n'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsubpos(void*, void*, void**)':
/usr/include/gmp.h:2194: undefined reference to `__gmpn_sub_n'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gmul(void*, void*, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1725: undefined reference to `__gmpn_mul'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1727: undefined reference to `__gmpn_mul'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsmul(void*, long, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1792: undefined reference to `__gmpn_mul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1790: undefined reference to `__gmpn_lshift'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsdiv(void*, long, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1850: undefined reference to `__gmpn_divrem_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1848: undefined reference to `__gmpn_rshift'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsmod(void*, long)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1915: undefined reference to `__gmpn_mod_1'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gdiv(void*, void*, void**, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:1989: undefined reference to `__gmpn_tdiv_qr'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_crt_struct_fast::eval(void**, long const*, _ntl_tmp_vec*)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:4668: undefined reference to `__gmpn_addmul_1'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsqrt(void*, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2105: undefined reference to `__gmpn_sqrtrem'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gsqrts(long)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2131: undefined reference to `__gmpn_sqrtrem'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_ggcd(void*, void*, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2187: undefined reference to `__gmpn_gcd'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2185: undefined reference to `__gmpn_gcd'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gexteucl(void*, void**, void*, void**, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2536: undefined reference to `__gmpn_gcdext'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_ginv(void*, void*, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:2652: undefined reference to `__gmpn_gcdext'
/usr/local/lib/libntl.a(lip.o): In function `_ntl_gaorsmul_1(void*, long, long, void**)':
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5963: undefined reference to `__gmpn_addmul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5972: undefined reference to `__gmpn_mul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:5989: undefined reference to `__gmpn_submul_1'
/home/piotr/Downloads/ntl-9.6.2/src/g_lip_impl.h:6024: undefined reference to `__gmpn_mul_1'
collect2: error: ld returned 1 exit status
CMakeFiles/crypto.dir/build.make:225: polecenia dla obiektu 'crypto' nie powiodły się
make[2]: *** [crypto] Błąd 1
CMakeFiles/Makefile2:67: polecenia dla obiektu 'CMakeFiles/crypto.dir/all' nie powiodły się
make[1]: *** [CMakeFiles/crypto.dir/all] Błąd 2
Makefile:83: polecenia dla obiektu 'all' nie powiodły się
make: *** [all] Błąd 2

My cmake looks exactly like this:

cmake_minimum_required(VERSION 2.8.9)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
project(crypto)

set(PROJECT_NAME "crypto")

find_package(NTL REQUIRED)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -pthread")
else()
        message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

SET(CMAKE_EXE_LINKER_FLAGS "-static -lntl -lgmpxx -lgmp -lm")

add_executable(crypto main.cpp elliptic_curve.cpp point.cpp lenstra.cpp pollard_algorithm.cpp ThreadPool.cpp)
target_link_libraries(crypto ${NTL_LIBRARY})

I really don't have any idea what I'm doing wrong, I will be gratefull for any help.

UPDATE:

I have gmp and ntl installed:

⋊> ~/D/gmp-6.1.0 ls ~/sw/lib/libntl.a        
/home/piotr/sw/lib/libntl.a
⋊> ~/D/gmp-6.1.0 ls /usr/local/lib/libgmp.a     
/usr/local/lib/libgmp.a
⋊> ~/D/gmp-6.1.0 ls /usr/local/lib/libgmpxx.a   
/usr/local/lib/libgmpxx.a

When i remove lines:

SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")

from cmake, I can compile normally without any problems my code. But I need a static compilation to exec this code on server, but with my build setup at now this is impossible, I don't know why.

I have also proper FindNTL.cmake file which looks like this:

FIND_PATH(NTL_INCLUDE_DIR RR.h
  HINTS
  $ENV{NTLDIR}
  PATH_SUFFIXES NTL include/NTL include
  PATHS
  ~/sw #
)

FIND_LIBRARY(NTL_LIBRARY
  NAMES ntl
  HINTS
  $ENV{NTLDIR}
  PATH_SUFFIXES lib64 lib libs64 libs libs/Win32 libs/Win64
  PATHS
  ~/sw
)


# handle the QUIETLY and REQUIRED arguments and set NTL_FOUND to TRUE if
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NTL DEFAULT_MSG  NTL_LIBRARY NTL_INCLUDE_DIR)

MARK_AS_ADVANCED(NTL_LIBRARY NTL_INCLUDE_DIR)

make VERBOSE=1

[ 14%] Linking CXX executable crypto
/usr/bin/cmake -E cmake_link_script CMakeFiles/crypto.dir/link.txt --verbose=1
/usr/bin/c++    -std=c++11 -pthread  -static -lntl -lgmpxx -lgmp -lm CMakeFiles/crypto.dir/main.cpp.o CMakeFiles/crypto.dir/elliptic_curve.cpp.o CMakeFiles/crypto.dir/point.cpp.o CMakeFiles/crypto.dir/lenstra.cpp.o CMakeFiles/crypto.dir/pollard_algorithm.cpp.o CMakeFiles/crypto.dir/ThreadPool.cpp.o  -o crypto -rdynamic /usr/local/lib/libntl.a 
pkruk
  • 809
  • 1
  • 7
  • 24
  • It seems "libntl.a" has some other dependencies that you need to get – Sreekar Jan 12 '16 at 20:34
  • Looks like you probably need https://gmplib.org/ – xaxxon Jan 12 '16 at 20:39
  • @xaxxon I have gmp and ntl installed already. – pkruk Jan 12 '16 at 20:46
  • I Updated my question with more informations :) – pkruk Jan 12 '16 at 21:02
  • run `make VERBOSE=1` and show the output please. It might be a library ordering issue for your linker. make sure the thing that USES the symbol comes before the thing that RESOLVES (e.g. has the implementation) the symbol. – xaxxon Jan 12 '16 at 21:36
  • 1
    `-lntl` and other `-l` options should be passed to **linker** (CMAKE_EXE_FLAGS), but you set them for *compiler* (CMAKE_CXX_FLAGS). – Tsyvarev Jan 12 '16 at 21:42
  • @Tsyvarev I did this, nothing changed, the same error – pkruk Jan 12 '16 at 22:59
  • 1
    Run make `VERBOSE=1` as suggested by xaxxon. BTW, `CMAKE_FIND_LIBRARY_SUFFIXES` is unrelated to your problem, as you set it *after* `find_package()`. Same for `BUILD_SHARED_LIBRARIES` (you don't build libraries). Content of `FindNTL.cmake` has a little sence for your problem too, as you could simply check value of variables which are set in it (and it is worth to check them!). – Tsyvarev Jan 12 '16 at 23:33
  • @Tsyvarev thank you for comments, i fix the issues you mentioned, and I update the question with this fixes and with output from make VERBOSE=1, problems steal exist and I don't see where is a crucial issue in here.. – pkruk Jan 13 '16 at 11:47
  • 1
    According to make output, you link to `ntl` library *twice*: Once via `-lntl` and second via `/usr/local/lib/libntl.a`. Note, that second path is not `/home/piotr/sw/lib/libntl.a` which you probably expect. – Tsyvarev Jan 13 '16 at 12:11

1 Answers1

0

According to hints from @Tsyvarev I made some changes, and run make with verbose, and finally i get make command which looks like this:

g++ -std=c++11 -static -lgmpxx -lgmp -lm CMakeFiles/crypto.dir/main.cpp.o CMakeFiles/crypto.dir/elliptic_curve.cpp.o CMakeFiles/crypto.dir/point.cpp.o CMakeFiles/crypto.dir/lenstra.cpp.o CMakeFiles/crypto.dir/pollard_algorithm.cpp.o CMakeFiles/crypto.dir/ThreadPool.cpp.o  -o crypto -rdynamic /home/piotr/sw/lib/libntl.a 

But ended unsuccesfully with error:

/usr/bin/ld: /usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/crtbeginT.o: relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/crtbeginT.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

Error when i use clang++ instead of g++ is:

/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

So I guess I need to recompile libc library with -fPIE options

pkruk
  • 809
  • 1
  • 7
  • 24