c++ - 如何在 Windows 上安装 GMP Mp? (C++)

标签 c++ makefile cygwin autotools gmp

我遵循了我可能找到的每一个指南,但老实说,我什至不知道某些安装“步骤”的含义。

我尝试安装 Cygwin(和 MYSY)并运行指南告诉我的命令,但终端要么不执行任何操作,要么给出错误:“没有这样的文件或目录”。 (我把文件夹目录改成了我的文件所在的地方)

此外,我不完全确定我是否正确安装了所有内容,因为我应该在安装过程中检查一些附加组件,对吗?我按照指南做了,但我仍然可能错过了一些东西......

有人可以逐步向我解释如何安装它,明确说明所有必须完成的事情,(我正在运行 Windows 7)考虑到这是我第一次做这样的事情,我不知道是什么./configure , make 和所有其他命令甚至意味着...

最佳答案

以下是仅使用 cygwin 工具的简单步骤。

要编译 C++,我们需要 g++ 编译器;要找到要安装的正确包,cygwin 工具是 cygcheck(默认安装),使用 -p 开关,它会查询位于 https://cygwin.com/packages/ 的数据库。 :

$ cygcheck -p bin/g++
Found 3 matches for bin/g++
gcc-g++-7.3.0-1 - gcc-g++: GNU Compiler Collection (C++)
gcc-g++-7.3.0-2 - gcc-g++: GNU Compiler Collection (C++)
gcc-g++-7.3.0-3 - gcc-g++: GNU Compiler Collection (C++)

所以我们需要gcc-g++包。
要安装它,我们运行 cygwin 安装程序,选择 Full View ,搜索 gcc-g 以过滤数千个包,然后单击 skipgcc-g++

enter image description here

完成安装后,验证我们是否已正确安装:

$ cygcheck -c gcc-g++
Cygwin Package Information
Package              Version        Status
gcc-g++              7.3.0-3        OK

安装 gcc-g++ 也会拉取 C 编译器包 gcc-core 的安装。
要编译一个 gmp 程序,我们需要合适的头文件和共享库;通常包含在“*-devel”包中:

$ cygcheck -p include/gmpxx.h
Found 9 matches for include/gmpxx.h
libgmp-devel-6.1.0-3p1 - libgmp-devel: Library for arbitrary precision arithmetic (development) (installed binaries and support files)
libgmp-devel-6.1.1-1 - libgmp-devel: Library for arbitrary precision arithmetic (development) (installed binaries and support files)
libgmp-devel-6.1.2-1 - libgmp-devel: Library for arbitrary precision arithmetic (development)
mingw64-i686-gmp-6.0.0a-2 - mingw64-i686-gmp: Multiple Precision arithmetic library for Win32 toolchain (installed binaries and support files)
...

所有带有mingw64的包都是交叉编译的,我们可以忽略,所以是libgmp-devel。验证是否已正确安装

$ cygcheck -c libgmp-devel
Cygwin Package Information
Package              Version        Status
libgmp-devel         6.1.2-1        OK

包内容是头文件和导入库

$ cygcheck -l libgmp-devel
/usr/include/gmp.h
/usr/include/gmpxx.h
/usr/lib/libgmp.dll.a
/usr/lib/libgmpxx.dll.a

现在我们可以编写一个示例,我是从 https://gmplib.org/manual/C_002b_002b-Interface-General.html 并写入名为 mpz_add.cpp 的文件中 您可以使用任何您想要的编辑器。重要的是文件如下 Unix 行终止标准 LF 而不是 Windows CR+LF(如果不是,请参阅下面的注释)

$ file mpz_add.cpp
mpz_add.cpp: C++ source, ASCII text

$ cat mpz_add.cpp
#include <gmpxx.h>
#include <iostream>
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";

  return 0;
}

编译我们的例子并测试它:

$ g++ mpz_add.cpp -lgmpxx -lgmp -o  mpz_add
$ ./mpz_add
sum is -4444
absolute value is 4444

我们还可以在程序 mpz_add 中验证链接了哪些库,我添加了一些额外的注释:

$ cygcheck ./mpz_add
D:\cyg_pub\tmp\gmp\mpz_add.exe
  D:\cygwin64\bin\cygwin1.dll              <= cygwin library
    C:\WINDOWS\system32\KERNEL32.dll       <= windows system library
      C:\WINDOWS\system32\ntdll.dll         ...
      C:\WINDOWS\system32\KERNELBASE.dll    ...
  D:\cygwin64\bin\cyggmp-10.dll            <= GMP C library
  D:\cygwin64\bin\cyggmpxx-4.dll           <= GMP C++ library  
    D:\cygwin64\bin\cyggcc_s-seh-1.dll     <= C library
    D:\cygwin64\bin\cygstdc++-6.dll        <= C++ library

如果文件有错误的行终止,最好的工具是d2u (Dos to Unix)

$ cygcheck -p bin/d2u
Found 6 matches for bin/d2u
...
dos2unix-7.4.0-1 - dos2unix: Line Break Conversion

$ file mpz_add.cpp
mpz_add.cpp: C++ source, ASCII text, with CRLF line terminators
$ d2u mpz_add.cpp
dos2unix: converting file mpz_add.cpp to Unix format...
$ file mpz_add.cpp
mpz_add.cpp: C++ source, ASCII text

由于您还添加了标签 makefileautotools,第一个在包 make 中:

$ cygcheck -p bin/make.exe
Found 6 matches for bin/make.exe
..
make-4.2.1-2 - make: The GNU version of the 'make' utility 

第二个更复杂,你需要包 autoconf automakelibtool

关于c++ - 如何在 Windows 上安装 GMP Mp? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52686916/

相关文章:

c - makefile 中无法识别规则?

git - 如何修复获取/提交时的 "address space is already occupied"错误?

linux - 如何使用 linux find 命令清理所有 Maven 项目?

c++ - pthread调度问题

c++ - 在函数周围传递 C 风格的字符串

c - Makefile: '_start'的多重定义

linux - gcc '-c'和gcc '-o'是什么意思?

c++ - 使用 boost::proto 构建 s-expression

c++ - CMAKE 和 MKOCTFILE

c++ - mkdir 和 ofstream 的问题