c++ program using GMP library - c++

I have installed GMP using the instruction on this website: http://www.cs.nyu.edu/exact/core/gmp/
Then I looked for an example program using the library:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
But if I compile this using the command: g++ test.cpp -o test.exe, it says gmpxx.h: no such file or directory.
How can I fix this? I am kind of new to this. And I am using MinGW.

Get the actual version here GNU GMP Library. Make sure you configure it to be installed in /usr/lib (pass --prefix=/usr to configure).
Here you have documentation: GNU GMP Manual.
You are not using the lib correctly. I don't know if you can directly access mpx values
with C++ functions but, here you have a working example of what you wanted to achieve:
#include<iostream>
#include<gmp.h>
using namespace std;
int main (int argc, char **argv) {
mpz_t a,b,c;
mpz_inits(a,b,c,NULL);
mpz_set_str(a, "1234", 10);
mpz_set_str(b,"-5678", 10); //Decimal base
mpz_add(c,a,b);
cout<<"\nThe exact result is:";
mpz_out_str(stdout, 10, c); //Stream, numerical base, var
cout<<endl;
mpz_abs(c, c);
cout<<"The absolute value result is:";
mpz_out_str(stdout, 10, c);
cout<<endl;
cin.get();
return 0;
}
Compile with:
g++ -lgmp file.cpp -o file

Here is the correct procedure for setting up the current (as of 7/2/13) GNU bignum libraries with Eclipse CDT, MinGW, and msys for C++. To get through this, you should have used Unix or Linux before, as well as Windows, and you should have a vague recollection of programming and compiling programs. This is the culmination of over a week of research and hardcore frustration, so if I messed something up note it politely or I will blow you up with the power of my mind!
I assume you have already downloaded and installed Eclipse and MinGW and have installed msys into MinGW. You must install MinGW before msys!
Download the tarball for the GMP libraries from gmplib.org to ${gmp_download}. I downloaded the gmp-5.1.2.tar.xz because I have never used lzip and didn't know if it was available in msys.
Open up an msys window (essentially a bash shell). cd ${gmp_buid} and tar -Jxvf ${gmp_download}/gmp-x.x.x.tar.xz
Those tar options are different from what you may find elsewhere on the web! -Jxvf is right for xz (and I think lzip), but for gzip you use -xzvf.
cd gmp-x.x.x and run ./config.guess. Write down the output. You will need it next.
Run ./configure --prefix=${gmp_build} --build= --enable-cxx --with-gnu-ld
Apparently if you don't explicitly tell GMP to build for your platform it builds everything, which is bad. The cxx option builds the C++ libraries and --with-gnu-ld allows it to work with ld. Pretty straightforward.
make
make install
EX: suppose you installed to C:/gmp. You should have gmp/include/gmp.h and gmpxx.h. You should also have gmp/lib/libgmp.a, libgmp.la, libgmpxx.a, libgmpxx.la. You should also have a share directory with stuff in it.
Set up eclipse:
Go to project --> properties
Under C/C++ build --> Environment edit the PATH variable and add ${gmp_build}/include;${gmp_build}/lib
Under C/C++ build --> settings --> tool settings --> GCC Assembler --> general add ${gmp_build}/include as an include path.
Same place but --> GCC C++ compiler --> Includes add ${gmp_build}/include as an include path.
Same place --> GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line. THE END OF THE LINE!
Same place --> GCC C compiler Add the same include paths and miscellaneous options as before.
Same place --> MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER! Now add ${gmp_build}/lib to "LIbrary Search Path (-L)"
Under C/C++ General --> Paths & Symbols --> Incudes Tab check that you have ${gmp_build}/include in your include directories for Assembly, C, and C++. If they aren't there you may have messed up an earlier step. They should be auto populated by Eclipse.
Same place --> Libraries Tab check that you have gmp and gmpxx IN THAT ORDER. It should already be populated.
Same Place --> Library Paths Tab Check for ${gmp_build}/lib which should already be there.
Hit "Apply" and make sure you rebuild the index or the changes won't take. Hit OK to close out.
Run this short program to verify your setup:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main ()
{
mpz_t p;
mpz_init_set_ui (p,3);
return 0;
}
Your compile commands should look similar to this:
g++ "-IC:\gmp\include" -O0 -g3 -Wall -c -fmessage-length=0 -lgmp -lgmpxx -o main.o "..\main.cpp" g++ "-LC:\gmp\lib" -o GMPDebug.exe main.o -lgmp -lgmpxx
Notes:
The order of the options is important. I don't know all of the whys, but if the second command line (which links the program) has the -lgmp -lgmpxx flags before the -o option, the linking will fail miserably.
The -l flag is a tricky one. It actually says "Go look in -L for liblibrary.a". In this case "Go look in C:\gmp\lib for libgmp.a and libgmpxx.a".
I have heard of bugs involving cout and the 64 bit version of eclipse, so I am using the 32 bit version, where I am seeing the same bug. :-)

Since there are very small examples in gmp library docs, I'm including exponentiation example for reference:
Program calculates 2 ^ 20000
#include <iostream>
#include <gmp.h>
using namespace std;
int main(void) {
mpz_t result, base;
mpz_inits(result,base,NULL);
mpz_set_str(base, "2", 10);
mpz_pow_ui(result, base, 20000);
mpz_out_str(stdout, 10, result);
return 0;
}
Compile:g++ -o gmp_pow_test gmp_pow_test.cpp -lgmp
Run :./gmp_pow_test
Install gmp library on Ubuntu with following: sudo apt-get install libgmp-dev libgmpxx4ldbl

You need to tell the compiler what libraries you want to use.
g++ -lgmp -lgmpxx file.cpp -o file

It is probably too late to be useful, but...
First, your program works just fine. As others pointed out, you need to (a) ensure that GMP library is installed (including its gmpxx extension, and all the relevant files), and (b) that you're telling the compiler where to find both the include files, and the libraries to link with.
In my case include files are in /opt/local/include, and the libraries are in /opt/local/lib (where Macports placed them :).
Here's the code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum of " << a << " and " << b << " is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
// cin >> a;
return 0;
}
Here's the compile/link command:
clang++ -o gmpxx-tst -I/opt/local/include gmpxx-tst.cpp -L/opt/local/lib -lgmpxx -lgmp
Here's what invocation of gmpxx-tst produces:
$ ./gmpxx-tst
sum of 1234 and -5678 is -4444
absolute value is 4444
$

You'll need to tell the compiler where the header file is.
g++ test.cpp -I/path/to/directory/that/contains/the/header -o test.exe

I have tried so many solutions and all of them has some problems
Here is the best way to install GMP and eclipse
Follow this link
http://www.multigesture.net/articles/how-to-install-mingw-msys-and-eclipse-on-windows/
You need to make sure of the following that hasn't been mentioned there:
When installing MinGW choose a path that contains no space like "c:\MinGW"
Once installed, From start open MinGW installation manger
choose all the basics
-then under MinGW, choose all GMP libraries to be installed
Apply changes
After that you will install JDK, then Add "C:\Program Files\Java\jdk1.8.0_121\bin" to PATH system variable
After installing Eclipse go to:
GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line
MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER
gooooo ahead.

Related

gmp.h missing when trying to use boost library

I am trying to use the boost library with QT on windows. I've successfully build the library and also managed to include it in my project. However, on including gmp (#include "boost/multiprecision/gmp.hpp") and creating an object (boost::multiprecision::mpz_int myint;) I get the following error:
C:\Users\Laurenz\Documents\libraries\boost_1_66_0\include\boost\multiprecision\gmp.hpp:31: error: gmp.h: No such file or directory
And indeed, I haven't been able to find any such file in the boost directory. What did I do wrong?
Install the dependency and link to it. (See What is an undefined reference/unresolved external symbol error and how do I fix it?)
Alternatively, consider not using GMP, using cpp_int.hpp instead.
Since you already installed the GMP library, here's the last step:
Live On Coliru
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
boost::multiprecision::mpz_int i("1238192389824723487823749827349879872342834792374897923479");
std::cout << pow(i, 3) << "\n";
}
Note the -lgmp flag at the end of the compile/link command:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo -lgmp
Running it:
./demo
1898298004808110659499396020993351679788129852647955073547637871096272981567489303363372689896302906549189545322451852317205769760555889831589125591739044248515246136031239

Including external libraries in CodeRunner 2 app?

I've always used Xcode to compile OpenCV based code in c++. The procedure in Xcode was quite simple, I just had to mention the paths and add the necessary lib files to the project. Theres this app called CodeRunner 2 for macOS. Theres no proper documentation on how to include external libraries to compile code in this app. Is it possible to link OpenCV headers and compile them in CodeRunner ? If yes, could someone post the steps?
You can run OpenCV in CodeRunner by setting up a new language. Go to Preferences -> Languages, right-click C++, and select Duplicate. Name the new language "C++ OpenCV". On the right side of the preferences window, click Settings then the Edit Script button. Look for this line (or something similar):
xcrun clang++ -x c++ -lc++ -o "$out" "${files[#]}" "${#:1}"
Add the clang++ command line parameters for OpenCV after "$out". Here's my version:
xcrun clang++ -x c++ -lc++ -o "$out" -I/usr/local/opt/opencv3/include -L/usr/local/opt/opencv3/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_calib3d "${files[#]}" "${#:1}"
Modify the -I and -L parameters to match your OpenCV install path. On this machine I used Homebrew to install OpenCV so it was installed in /usr/local/opt. On other machines I've compiled from source so OpenCV is installed in /usr/local/lib.
Modify the -l parameters to include the libraries you typically use.
After saving the compile script, go back to Preferences -> Languages and select the Templates button. You can set up a template for OpenCV programs. Here's mine:
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
int main(int argc, char *argv[]) {
cv::Mat image;
// read an image
if (argc < 2)
image = cv::imread("img.jpg");
else
image = cv::imread(argv[1]);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
// create image window named "asdfasdf"
cv::namedWindow("asdfasdf");
// show the image on window
cv::imshow("asdfasdf", image);
// wait for key
cv::waitKey(0);
return 0;
}
The previous reply by SSteve is great and also helps me sort out linking Boost library in CodeRunner.
Because the solution in the previous reply is specific to OpenCV library, a carelessly adding to the clang++ command line for external libraries in general might just generate massive building errors, which was the case when I tried to link Boost library.
Here, I want to clarify the unclear bit in SSteve's reply so everyone knows how and where to modify the command line before compiling their code with external library in Mac OS system.
I will use my case to explain, but in some point I will inform you of the tricky bits in CodeRunner setting or general command line typing.
I use macport to install the Boost library by
sudo port install boost
header file is located at /opt/local/include
library is located at /opt/local/lib/
If you cannot find the specific sub-library in Boost, open your terminal and type
cd /opt/local/lib/
find . -iname "*boost*"
and you should see all sub-libraries of Boost ( static library ends with .a and dynamic library ends with .dylib ) as below.
Before you start to modify the original command line ( supporting c++ 14 version ) such as
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
you need to know the directory of header file is after -I and the directory of Boost library is after -L, like
-I /opt/local/include/
-L /opt/local/lib/
In order to use a compiled static or dynamic sub-library in Boost ( see figure above ), you have to include it specifically after -L /opt/local/lib/. However, simply copying the library name without file extension either .a or .dylib would never let CodeRunner find the library you expect to run !!!
The detail is explained here and I just quota the important bit below
clang -dynamiclib -o libtest.dylib file1.o file2.o -L/some/library/path -lname_of_library_without_lib_prefix
To run such an example code in Boost Quickstart Document
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
the way to include <boost/regex.hpp> now is by
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" -I /opt/local/include/ -L /opt/local/lib -lboost_regex-mt "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
By using this command line, you should be able to compile the example code with Boost library.
Just remember to replace the prefix -lib with -l and exclude the file extension in the command line.
At last, there are some alternative solution to include the external library by using Xcode, which is in here

Cant find Shared Object on runtime. KRPC

Im trying to use a library named krpc. It is installed with cmake according to the instructions. https://krpc.github.io/krpc/cpp/client.html#using-the-library
This is the test program:
#include <iostream>
#include <krpc.hpp>
#include <krpc/services/krpc.hpp>
int main() {
krpc::Client conn = krpc::connect();
krpc::services::KRPC krpc(&conn);
std::cout << "Connected to kRPC server version " << krpc.get_status().version() << std::endl;
}
Compiled with:
g++ main.cpp -std=c++11 -lkrpc -lprotobuf
ldd returns: (other libraries is found)
ldd a.out
libkrpc-0.3.7.so => not found
The library is installed from ./krpc-cpp-0.3.7
which contains libkrpc.so.0.3.7.
How do I properly install the library? I tried changing the name of the .so. And making softlinks to /ust/lib. And running ldconfig anew afterwards.
Had to copy the .so to /usr/lib, then run ldconfig.
-Thanks to #Xin Huang

Difficulty linking Boost 1.60 on OS X after installing via Homebrew

I have a fresh install of OS X 10.11.4 that I immediately installed the Xcode toolchain on, then Homebrew, then Boost 1.60. In order to test that everything had gone well, I wrote the following code on my Desktop.
#include <iostream>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path new_directory("hello");
boost::filesystem::create_directory(new_directory);
}
I then attempted to compile it as I usually have done with the following command.
$ clang++ test.cpp -o test -lboost_system -lboost_filesystem
I received the following error.
test.cpp:3:10: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
This is how I have always compiled projects that link Boost in the past. I'm assuming that I have probably forgotten a step along the way that allows clang to search a specific path to dynamically link the libraries. What should I change in order for this compilation command to work?
For me, boost has been compiled and installed into a subdirectory of my home directory, so you'll need to modify the paths as appropriate for your homebrew installation:
flags="-std=c++1z -I/${HOME}/local/include -L${HOME}/local/lib -lboost_filesystem -lboost_system"
c++ ${flags} -o jared jared.cpp
First get the location of boost by doing the following:
brew info boost
From the image above, you can see that my location is
/usr/local/Cellar/boost/1.66.0
Then, to compile, use the following:
c++ -I /usr/local/Cellar/boost/1.66.0 main.cpp -o boost

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.
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.
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.