无法将静态程序与ntl库链接

时间:2016-01-12 20:25:53

标签: c++ build cmake gmp ntl

当我想为我的代码创建静态构建时(我想在服务器上运行此代码)我遇到了一个奇怪的问题:

/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

我的cmake看起来完全像这样:

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})

我真的不知道我做错了什么,我会感激任何帮助。

更新:

我安装了gmp和ntl:

⋊> ~/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

当我删除行时:

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

来自cmake,我可以正常编译而没有任何问题我的代码。 但我需要一个静态编译来在服务器上执行此代码,但是现在我的构建设置是不可能的,我不知道为什么。

我还有正确的FindNTL.cmake文件,如下所示:

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)

使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 

1 个答案:

答案 0 :(得分:0)

根据来自@Tsyvarev的提示,我做了一些更改,并使用详细的make运行make,最后我得到make命令,如下所示:

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 

但不假思索地结束了错误:

/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

使用clang ++代替g ++时出错:

/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)

所以我想我需要用-fPIE选项重新编译libc库