链接 GMP 库时出错
Posted
技术标签:
【中文标题】链接 GMP 库时出错【英文标题】:error linking GMP library 【发布时间】:2015-03-27 18:35:32 【问题描述】:我有以下 CMakeList.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "$CMAKE_SOURCE_DIR")
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
用下面的简单源文件main.cpp
#include <iostream>
#include <gmp.h>
#include <cstdio>
using namespace std;
int main()
mpz_t a,b;
mpz_init_set_str(a,"45",10);
mpz_init_set_str(b,"12",10);
mpz_add(a,a,b);
cout <<mpz_get_str(NULL,10,a)<<endl;
我在网上找到了一个 FindGMP.cmake 文件:
set(GMP_PREFIX "" CACHE PATH "path ")
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
PATHS $GMP_PREFIX/include /usr/include /usr/local/include )
find_library(GMP_LIBRARY NAMES gmp libgmp
PATHS $GMP_PREFIX/lib /usr/lib /usr/local/lib)
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
get_filename_component(GMP_LIBRARY_DIR $GMP_LIBRARY PATH)
set(GMP_FOUND TRUE)
endif()
if(GMP_FOUND)
if(NOT GMP_FIND_QUIETLY)
MESSAGE(STATUS "Found GMP: $GMP_LIBRARY")
endif()
elseif(GMP_FOUND)
if(GMP_FIND_REQUIRED)
message(FATAL_ERROR "Could not find GMP")
endif()
endif(
当我运行 cmake 时,我有以下输出:
moyle@localhost:~/Desktop/gmptri$ cmake .
-- Found GMP: /usr/local/lib/libgmp.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/moyle/Desktop/gmptri
然后我运行 make 并收到以下错误:
(precise)moyle@localhost:~/Desktop/gmptri$ make VERBOSE=1
/usr/bin/cmake -H/home/moyle/Desktop/gmptri -B/home/moyle/Desktop/gmptri --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/moyle/Desktop/gmptri/CMakeFiles /home/moyle/Desktop/gmptri/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/depend
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
cd /home/moyle/Desktop/gmptri && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri/CMakeFiles/Turrial.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/build
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
Linking CXX executable Turrial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Turrial.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Turrial.dir/main.cpp.o -o Turrial -rdynamic
CMakeFiles/Turrial.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x30): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x47): undefined reference to `__gmpz_add'
collect2: ld returned 1 exit status
make[2]: *** [Turrial] Error 1
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make[1]: *** [CMakeFiles/Turrial.dir/all] Error 2
make[1]: Leaving directory `/home/moyle/Desktop/gmptri'
make: *** [all] Error 2
谁能帮帮我?
【问题讨论】:
您可以在详细模式下运行构建以查看该阶段正在执行的命令吗? @EtanReisner 我编辑了这个问题。 【参考方案1】:您忘记将库与可执行文件链接。
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
target_link_libraries(Turrial gmp libgmp)
【讨论】:
如果使用 GMP 的 C++ 接口 (#include <gmpxx.h>
),我不得不去target_link_libraries(Turrial gmp gmpxx)
@HolKann 我也有类似的问题。 cmake
正确找到了 gmp
库,但是当我运行 make
时,它返回错误:ld: library not found for -lgmp
。我使用相同的FindGMP.cmake
文件和下面的指令。以上是关于链接 GMP 库时出错的主要内容,如果未能解决你的问题,请参考以下文章