GMP with MinGW on Windows

坚强是说给别人听的谎言 提交于 2019-12-11 04:33:39

问题


I managed to build the GMP library successfully on my Windows 7 (64-bit computer). The steps I followed were:

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

All of the tests pass successfully. Clearly, I'm at the point where it's possible to compile and run GMP programs. But, when I try to compile the following program:

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

with the command:

gcc -m32 -lgmp main.c

I get the following error:

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

Can anyone tell what I'm doing wrong? I've done my Googling, and after a few too many hours, any help you can offer would be appreciated. Thanks!


回答1:


The -l options are used to add libraries to your program. Note that order is important! You should always list libraries after all your objects.

Try gcc -m32 main.c -lgmp



来源:https://stackoverflow.com/questions/17037586/gmp-with-mingw-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!