c++ccompilationlinkermpir

MPIR gcc compilation - cannot find -lmpir


I am trying to compile a simple C program using GCC with MPIR under MinGW on my Windows 7 machine. I installed MPIR successfully (I guess) with configure, make, make check and make install (did not use "sudo" - what is this?).

The program is called "mytest.cpp", sits in the top folder of MPIR, namely C:/MPIR/mpir-2.7.0/, where also "mpir.h" is sitting (is it "the" (correct one? are there several?) mpir.h?):

#include "mpir.h"
using namespace std;

int main ()
{
  mpz_t z; 
  mpz_init(z);   
  return 0;
}

I tried compiling via

gcc mytest.c -o mytest -lmpir -I/C:/MPIR/mpir-2.7.0/

with the hope that GCC would then be able to locate mpir.h, "-lmpir" because a helpful developer told me to; but then it says:

"C:/mingw/ [...] /bin/ld.exe: cannot find -lmpir"

where "[...]" stands for some directory up-and-down-climbs inside the "minGW" directory. However, I am with the shell currently in the C:/MPIR/mpir-2.7.0/ directory.

What is wrong? How to make GCC find the mpir files? Should the compile option "-I" be spelled differently? I also heard about some "-L" option but could not find that anywhere. Thanks.


Solution

  • Ok, I fixed it.
    Summarizing, critical points are:
    - the order of the gcc options matter: "-o mytest" needs to go to the end, and "-lname" before but after the "-Ldir";
    - the path should have ".libs" at the end because this is where the libraries are (even if they do not need to be named libmpir.a)
    - (at least in MinGW) the working format is c:/MPIR/mpir-2.7.0/.libs (thus absolute, also from /usr/local/ or other places)

    What worked was for example:

    $ gcc mytest.c -Lc:/MPIR/mpir-2.7.0/.libs -lmpir -o mytest
    $ gcc mytest.c -Lc:/MPIR/mpir-2.7.0/.libs -lmpir.dll -o mytest

    Best.