0

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.

Futurist
  • 75
  • 1
  • 8

2 Answers2

1

Change

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

to

gcc mytest.c -o mytest -lmpir -IC:/MPIR/mpir-2.7.0/ -LC:/MPIR/mpir-2.7.0

You don't need a / in front of C: and the -L flag tells the linker where to find the library that you are linking to with -l flag.

Also, I would recommend using relative paths to your includes and libraries instead of absolute.

rost0031
  • 1,894
  • 12
  • 21
  • Thanks, I tried that; it still "cannot find -lmpir". Compiling with a "-v" option yields, among others: "... #include "..." search starts here: [newline] #include <...> search starts here: [newline] c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include [newline] ..." and similars; does that indicate that the compiler did not leave the MinGW directory? I did, however, add the MPIR path to the PATH environment variable, and am also working in the directory containing the #included file... ??? – Futurist Jun 03 '15 at 17:16
  • Try to manually find the libmpir.a in the mingw directory. That's where your -L flag needs to point to. – rost0031 Jun 03 '15 at 17:20
  • There is none. In the MPIR directory, however, there are various similarly named files, in particular libmpir.la, libmpir.lai, libmpir.dll.a and more, the first one in the top mpir-2.7.0 directory, the others in the .lib subdirectory. I did, however, perform "make check" and "make install" without any errors... how do I check if the installation was successful? thanks. – Futurist Jun 03 '15 at 17:38
  • Ok, that's your directory that you need to point to with the -L flag. So you should have -lmplr -LC:\MPLR-or-whatever\path\to\lib at the very end of your compile/link command. – rost0031 Jun 03 '15 at 17:40
  • Yes... that's what I do... I use exactly what you wrote, gcc mytest.c -o mytest -lmpir -IC:/MPIR/mpir-2.7.0/ -LC:/MPIR/mpir-2.7.0 . However, is there a file "libmpir.a" missing which ought to be generated in the build process? It still does not find any mpir library. – Futurist Jun 03 '15 at 18:00
  • Try `-lmplr.dll` since you only seem to have the libmplr.dll.a file available. This is a static library that is converted to a dll by the build process later and should be statically linkable. – rost0031 Jun 03 '15 at 18:07
  • libmpir.dll.a is available, not libmplr.dll.a . I tried this, but it neither helped. – Futurist Jun 03 '15 at 23:23
  • Sorry, I couldn't tell the difference between the spellings with capital `I` and lowercase `l`. I should have noticed the order of your flags as well. Glad you figured it out. – rost0031 Jun 03 '15 at 23:28
0

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.

Futurist
  • 75
  • 1
  • 8
  • Hm. After two weeks (not trying it), the above still did compile without error, but when executing the program via "mytest", it complained "The program could not be started since libmpir-16.dll is missing. Install the program again." But this file is actually sitting in the C:/MPIR/mpir-2.7.0/.libs directory (while eg. mpir.a or libmpir.a are not!)... or do I need to look for it somewhere else? I have added the MinGW/bin and the mpir-2.7.0/ path to the PATH environment variable... no improvement. Where else can they be and should they be? – Futurist Jun 17 '15 at 15:29