Netbeans(Windows 64位)中带有C++的GMP错误

Error with GMP with C++ in Netbeans (Windows 64-Bit)

本文关键字:C++ GMP 错误 Windows 64位 Netbeans      更新时间:2023-10-16

我正在为学校做一个项目,在这个项目中,我正在进行涉及素数的各种不同计算。这些数字往往很大,因此我去寻找一个任意精度的库。我决定去GMP,因为我之前在Game Maker中使用过它(一个相对未知的程序),因为有人为它制作了一个dll。

现在,我已经按照安装手册进行了GMP的编写。我很难做到这一点,因为我完全不熟悉UNIX和cygwin。现在,我已经尝试在Netbeans中包含gmpxx.h作为测试程序,但事情出了问题。我的代码如下:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <gmpxx.h>
#include <stdarg.h>
using namespace std;
int main()
{
    mpz_t a;
    mpz_init(a);
    //cout << mpz_probab_prime_p(a,20);
    mpz_clear(a);
}

对于mpz_init和mpz_clear,我都得到了相同的错误:

根据未定义的符号'__gmpz_[init/clear]'截断重新定位以适应:R_X86_64_PC32

我只是猜测,但问题可能是以下任何一种:

  • 编译错误
  • 错误代码
  • 包含/链接不正确

它很可能是后者,尽管我已经尝试过为头文件等添加目录。我该如何修复此错误?

提前感谢

编辑:既然这是我的第一篇帖子,你能指出我需要澄清什么才能回答这个问题吗?

第2版:这是Netbeans:中的编译日志

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe
make[2]: Entering directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++ -Lpath/to/gmp/lib -lgmpxx -lgmp    -o dist/Debug/Cygwin_4.x-Windows/firsttestgmp build/Debug/Cygwin_4.x-Windows/main.o 
build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22: undefined reference to `__gmpz_init'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__gmpz_init'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:24: undefined reference to `__gmpz_clear'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:24:(.text+0x21): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__gmpz_clear'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe] Error 1
make[2]: Leaving directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)

第3版:正如@rubenwd所指出的,真正的错误是:

build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22: undefined reference to `__gmpz_init'

重要的是,链接库位于包含对它们的引用的对象文件之后。所以你的编译命令需要是:

g++ -o dist/Debug/Cygwin_4.x-Windows/firsttestgmp build/Debug/Cygwin_4.x-Windows/main.o -lgmpxx -lgmp

如果您在非标准位置安装了GMP(事实并非如此),则需要使用-L选项添加库目录的路径。

谢谢大家!我通过以下操作解决了问题:属性>构建>链接器>库>添加库然后添加gmp.a和gmpxx.a,它们是我在编译的gmp目录中找到的。