以下是仅使用 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 视图,搜索 gcc-g 以过滤数千个包,然后单击 gcc-g++ 行中的 skip
完成安装后,验证我们是否已正确安装:
$ 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
由于您还添加了标签makefile 和autotools,第一个在包中:
$ 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
第二个更复杂,你需要包autoconfautomake和libtool,