Thread: MingW and GNU MP?

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    MingW and GNU MP?

    I've been trying to get GMP and MingW to play ball for the last couple of hours, but it's been a struggle.

    I finally got GMP installed under Windows XP, but now i've hit another problem.

    I'm trying to compile something simple like this:

    Code:
    #include <gmp.h>
    
    int main()
    {
         mpz_t sum;
         return 0;
    }
    Basically a program that will try out one of GMPs data types, but the program only works if i put the entire path between the '<'s, like <C:\GMP\include\gmp.h>.

    How do i avoid this, the gmp documentation doesn't do this, and i don't wan't to either?

    Btw, the documentation tells me to do some linking, (-lgmpxx and -lgmp), but when i try this, nothing really happens, i suspect this to be the problem. What do i need to do to get it right?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    nothing really happens
    The segment of code you posted doesn't do anything. "nothing really happens" would be the correct behavior.

    As for having to use the full path, gmp.h isn't in your include path. You can pass -IC:\GMP\include *dash - uppercase 'eye' - path" to the compiler, and then use <gmp.h> in your program.

    Personally, I have a directory structure like:
    Code:
    + addons
      + include
      + lib
      + bin
    
      + libgmp
      + SDL
      + any_other_3rd_party_library
    I then copy any headers to include, any libraries (*.a files) to lib, and any dlls/useful tools to bin. Then I have the environment variable CPATH and CPLUS_INCLUDE_PATH set to the include directory. This keeps things organized, for one, and allows me to upgrade gcc without losing/having to move the libraries. (Although C++ libraries need to be rebuilt - at least, wxWidgets did.)

    See this page for environment variables affecting gcc/g++.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Cactus_Hugger View Post
    The segment of code you posted doesn't do anything. "nothing really happens" would be the correct behavior.
    That's not what i meant, when i try to compile the program with the -lgmp flag, it gives me the same errors as it would if i hadn't included the -lgmp (Undeclared reference, blablabla).

    Thanks for the "-I" tip though, i tried passing "-IC:\GMPB\include" to the compiler, but it just comes up with some "undefined reference" when i try to run some functions from the GMP library.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Probably should have added this in the first post.
    "undefined reference" occurs when you link, so the -I flag is either working, or it wasn't needed. To link, you need to specify where the library files are, so the compiler can find them. The flag is -Ldirectory. (Just like -I, but with -L) You'll need use the directory to the library files (*.a files), instead of the include folder.

    Your compiler should also say something like "couldn't find library -lgmp", or something along those lines, when you pass -lgmp. (Since it's not there, it ought to be complaining...)

    (There's also an environment variable for libraries, but use the flags if you're not comfortable with environment variables.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Cactus_Hugger View Post
    Probably should have added this in the first post.
    "undefined reference" occurs when you link, so the -I flag is either working, or it wasn't needed. To link, you need to specify where the library files are, so the compiler can find them. The flag is -Ldirectory. (Just like -I, but with -L) You'll need use the directory to the library files (*.a files), instead of the include folder.

    Your compiler should also say something like "couldn't find library -lgmp", or something along those lines, when you pass -lgmp. (Since it's not there, it ought to be complaining...)

    (There's also an environment variable for libraries, but use the flags if you're not comfortable with environment variables.)
    Awesome, it's working now I did "g++ main.cpp -IC:\GMPB\include -LC:\GMPB\lib -lgmp" and it compiled right away.

    Last problem:
    It still wont work if i just do "g++ main.cpp", that gives me a ton of errors. Please don't tell me i have to show it the way to the header and to the library and do "-lgmp" each time i want to compile?

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    At very best, you'll have to type "g++ main.cpp -lgmp"

    g++ can't (unfortunately) magically determine the location to the header files. (And it's not a good idea to use absolute locations... who says I even have a C: ?) You can supply the location in a couple of ways:
    1) Using the -I / -L flags as you already have. If you use a makefile, you can simply put CFLAGS = -IC:\etc..., and add the -L flag to wherever you link. (If you're compiling everything on the command line, I would highly suggest looking into either makefiles or an IDE - either helps simplify large projects with many files.)

    2) You can copy the headers and *.a files into their respective directories wherever you have g++, alongside the standard headers/libraries.

    3) You can setup the environment variables as mentioned above. I do this in my autoexec.bat file:
    Code:
    SET CPATH=N:\MinGW\addons\include
    SET LIBRARY_PATH=N:\MinGW\addons\lib
    Substitute your paths as needed, of course. You can also run those SET commands in just a command prompt (cmd or command) and they'll work, but only for that prompt until that prompt is closed, which is a great way to make sure they work first. (Of course, they work in batch files too, if you carry gcc on a usb drive like I do. )
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I find it easier to just do a make install to MinGW's own include and lib directories.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Actually, I find it easier to just do a make install to MinGW's own include and lib directories.
    That's fine as long as you don't mind updating all your installed tools when you get the next newer version of the compiler.

    It also tends make "interesting" job of updating your next tools library version when there is a change in the name of a header-file, and you include the old one, as you don't know which to remove... :-(

    Any project larger than one C-file requires some sort of "build-tool" anyways - either a makefile or a batch-script to build, so why not use this to specify your paths to include/library files...

    --
    Mats

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's fine as long as you don't mind updating all your installed tools when you get the next newer version of the compiler.
    For GMP I think you pretty much should re-compile if you upgrade compiler, considering that they keep on warning of the dangers of GMP being miscompiled.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Cactus_Hugger View Post
    At very best, you'll have to type "g++ main.cpp -lgmp"

    g++ can't (unfortunately) magically determine the location to the header files. (And it's not a good idea to use absolute locations... who says I even have a C: ?) You can supply the location in a couple of ways:
    1) Using the -I / -L flags as you already have. If you use a makefile, you can simply put CFLAGS = -IC:\etc..., and add the -L flag to wherever you link. (If you're compiling everything on the command line, I would highly suggest looking into either makefiles or an IDE - either helps simplify large projects with many files.)

    2) You can copy the headers and *.a files into their respective directories wherever you have g++, alongside the standard headers/libraries.

    3) You can setup the environment variables as mentioned above. I do this in my autoexec.bat file:
    Code:
    SET CPATH=N:\MinGW\addons\include
    SET LIBRARY_PATH=N:\MinGW\addons\lib
    Substitute your paths as needed, of course. You can also run those SET commands in just a command prompt (cmd or command) and they'll work, but only for that prompt until that prompt is closed, which is a great way to make sure they work first. (Of course, they work in batch files too, if you carry gcc on a usb drive like I do. )
    Thanks alot, you've been a great help!

  11. #11
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Sorry for reviving this old thread but i've hit into more problems and i don't want to fill up the boards with threads on the same subject.

    As i said, i finally got it working with the C part of GMP, but really, what i wanted was the class interface of gmp so i can do stuff like:

    Code:
    mpz_class a, b;
    a = 89;
    b = a * 3;
    After reading the documentation i realized that i had only gotten the C part of the library so i'm forced to use functions for everything. The doc for GMP reads:

    The C++ support consists of a library 'libgmpxx.la' and header filer 'gmpxx.h'
    Well, i've only got the C files for GMP, and the header file, but i'm lacking the 'libgmpxx.la' file, even though i downloaded the newest version from the GMP project site (Ver 4.2.1), and the FAQ clearly states that the C++ wrapper files are included in the gmp files on the site.

    Where is the file im lacking?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To get gmpxx, GMP needs to be compiled with --enable-cxx. Unfortunately this is disabled by default.

    There are good reasons for not enabling --enable-cxx by default.
    It is a pity that --enable-cxx does not default to `yes' when an adequate C++ compiler is available. This means several other "bug reports" for those of us relying on the C++ interface. What are the reasons behind the current default? The C++ interface should somehow be out of its experimental phase. Or not? The language itself and the GMP C++ code has probably matured by now, but the compilers still cause major headaches. If we made --enable-cxx the default, the result would be that GMP didn't build for most users. We might be able to design tests that reject many of the compilers/include files/libraries that cause headaches. But that'd be a lot of work, and since C++ compiler writers are very quick to break new things, wouldn't be successful unless we made very frequent gmp releases. :-)
    Or at least there were when that was written.

    Anyway, this means that you'll need to find a special binary distribution or compile GMP yourself. After extensive googling I couldn't find any pre-compiled Windows binaries with gmpxx enabled, but maybe you'll have better luck. Or maybe you could try compiling GMP. It probably wouldn't be that difficult, but I wouldn't know as I've never tried it.

    For reference, here's some information about gmpxx. http://gmplib.org/manual/C_002b_002b...lass-Interface
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by dwks View Post
    To get gmpxx, GMP needs to be compiled with --enable-cxx. Unfortunately this is disabled by default.

    There are good reasons for not enabling --enable-cxx by default.

    Or at least there were when that was written.

    Anyway, this means that you'll need to find a special binary distribution or compile GMP yourself. After extensive googling I couldn't find any pre-compiled Windows binaries with gmpxx enabled, but maybe you'll have better luck. Or maybe you could try compiling GMP. It probably wouldn't be that difficult, but I wouldn't know as I've never tried it.

    For reference, here's some information about gmpxx. http://gmplib.org/manual/C_002b_002b...lass-Interface
    Well, i already did compile it once using Msys so why shouldn't i be able to compile it again, it's not really hard at all, it just takes forever. But the "enable-cxx" flag, should i put that after "./configure" or after "make install"?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Neo1 View Post
    Well, i already did compile it once using Msys so why shouldn't i be able to compile it again, it's not really hard at all, it just takes forever. But the "enable-cxx" flag, should i put that after "./configure" or after "make install"?
    Code:
    ./configure --enable-cxx
    (At least I presume so, as that's how other tools, such as gcc take parameters of such type).

    --
    Mats

  15. #15
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    This is quite a struggle! I've recompiled the GMP files with the --enable-cxx flags, and i have now got the correct headers and library files, but it still won't work, very frustrating!

    First of all: Cactus_hugger mentioned copying the library files and headers to my compiler directory to make compiling easier, i put the "gmp.h" in there, but there is also a subdirectory called "C++". This contains all the c++ headers, like string and iostream, should i put the "gmpxx.h" file in there?

    Also, if i try to compile a simple program using the c++ gmp classes, g++ gives me an error in the header file gmpxx.h - "Calling fdopen: No such file or directory". This is the program i am trying to compile:
    Code:
    #include <gmp.h>
    #include <gmpxx.h>
    
    int main()
    {
    	mpz_class a, b, c;
    	
    	a = 1423354;
    	return 0;
    }
    This is getting to me, i've shown the compiler the way to both library files and header files, and i've added "-lgmp -lgmpxx" just as the GMP documentation states, so why on earth isn't it working?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GNU textinfo
    By Mario F. in forum Tech Board
    Replies: 0
    Last Post: 12-04-2006, 08:20 PM
  2. Can anyone using mingw compile this?
    By deoren in forum Linux Programming
    Replies: 4
    Last Post: 03-18-2003, 11:18 AM
  3. Gnu Gcc
    By JasonLikesJava in forum Windows Programming
    Replies: 2
    Last Post: 03-07-2002, 12:34 PM
  4. Gnu Gcc with Windows
    By JasonLikesJava in forum C++ Programming
    Replies: 0
    Last Post: 03-06-2002, 12:40 PM