1

我设法在我的 Windows 7(64 位计算机)上成功构建了 GMP 库。我遵循的步骤是:

./configure --enable_cxx --disable-static --enable-shared --prefix="/c/MinGW"
make
make install
make check

所有的测试都成功通过。显然,我已经可以编译和运行 GMP 程序了。但是,当我尝试编译以下程序时:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gmp.h>

char *progname;

void print_usage_and_exit ()
{
    fprintf (stderr, "usage: %s -q nnn\n", progname);
    fprintf (stderr, "usage: %s nnn ...\n", progname);
    exit (-1);
}

int main (int argc, char **argv)
{
    mpz_t n;
    int i;

    progname = argv[0];

    if (argc < 2)
        print_usage_and_exit ();

    mpz_init (n);

    if (argc == 3 && strcmp (argv[1], "-q") == 0)
    {
        if (mpz_set_str (n, argv[2], 0) != 0)
            print_usage_and_exit ();
        exit (mpz_probab_prime_p (n, 25) == 0);
    }

    for (i = 1; i < argc; i++)
    {
        int result;
        if (mpz_set_str (n, argv[i], 0) != 0)
            print_usage_and_exit ();
        result = mpz_probab_prime_p (n, 25);
        mpz_out_str (stdout, 10, n);
        if (result == 0)
            puts (" is composite");
        else if (result == 1)
            puts (" is a probable prime");
        else /* result == 2 */
            puts (" is a prime");
    }
    exit (0);
}

使用命令:

gcc -m32 -lgmp main.c

我收到以下错误:

C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x7f): undefined reference to `_imp____gmpz_init'
C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0xc3): undefined reference to `_imp____gmpz_set_str'
C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0xe2): undefined reference to `_imp____gmpz_probab_prime_p'
C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x12b): undefined reference to `_imp____gmpz_set_str'
C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x14a): undefined reference to `_imp____gmpz_probab_prime_p'
C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x170): undefined reference to `_imp____gmpz_out_str'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

谁能告诉我做错了什么?我已经完成了谷歌搜索,在几个小时后,您可以提供的任何帮助将不胜感激。谢谢!

4

1 回答 1

5

-l 选项用于将库添加到您的程序中。注意顺序很重要!您应该始终在所有对象之后列出库。

尝试gcc -m32 main.c -lgmp

于 2013-06-11T06:26:08.057 回答