Square root finder (uses mpir, written in c++) is failing with multiple lnk2019 compile-time linker errors when compiled in x64

256 Views Asked by At

Lately, I've been doing high-precision number crunching in c++. I've been creating a program to calculate the square root of two using the gmp (gnu multiple precision) fork mpir (multiple precision integers and rationals). When I compile the program (1 file, approx 100 lines) in Win32 mode, it compiles and runs fine, but when I started encountering overflow in x86 mode, I tried switching to x64 in hopes of higher precision calculations. When I tried compiling, it gave me 8 very similar linker errors, such as "unresolved external symbol __gmpf_get_str encountered in function wmain". I searched and searched on places like msdn and stack overflow, but despite the hours I spent, there was no such luck. Here's the code:

// SquareRootFast.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<sstream>
#include<malloc.h>
#include<cstdlib>
#include<mpir.h>
#include<fstream>
#include<iostream>
#include"mpir.cpp"
using namespace std;
char* __cdecl getstr(mpf_t mpf, size_t digits)
{
    char* u1 = (char*)malloc(sizeof(char) * 5 * digits);
    mp_exp_t* u2 = (mp_exp_t*)malloc(sizeof(long));
    char* alpha = __gmpf_get_str(u1, u2, 10, digits, mpf);
    return alpha;
}
int _tmain(int argc, _TCHAR* argv[])
{
    unsigned long precspec = 1024 * 1024 * 256;
    mpf_set_default_prec(precspec);
    mpf_t sqrt;
    mpf_init(sqrt);
    mpf_set_d(sqrt, 1);
    mpf_t one;
    mpf_init(one);
    mpf_set_d(one, 1);
    mpf_t two;
    mpf_init(two);
    mpf_set_d(two, 2);
    long long int count = 0;
    while(count < 28)
    {
        mpf_t parta;
        mpf_init_set_ui(parta, 0);
        mpf_t partb;
        mpf_init_set_ui(partb, 0);
        mpf_div(parta, sqrt, two);
        printf("Part A complete\n");
        mpf_div(partb, one, sqrt);
        printf("Part B complete\n");
        mpf_add(sqrt, parta, partb);
        printf(getstr(sqrt, 1000));
        printf("\n");
        count++;
        printf("%d\n", count);
    }
    ofstream writer;
    writer.open("C:\\Users\\Ben-4\\sqrt2(3).txt");
    writer << getstr(sqrt, 1000000000);
    writer.close();
}
1

There are 1 best solutions below

0
On

You must add the path to mpir.lib. Go in Projet properties > Linker > Input > Additional Dependencies, then add the full path to the mpir.lib file (must be enclosed in "")

Beware to select the x64 or 32 version depending on your build config.