以下是仅使用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
![enter image description here](https://istack.dev59.com/xLDwl.webp)
完成安装后,为了验证我们已正确安装:
$ 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 转 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
标签,第一个标签包含在
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
、
automake
和
libtool
这些包。
libgmp-devel
、libgmpxx4
、libgmp10
。 - matzeri