WIN10 VS2019 编译Cyrus SASL

环境

  1. 下载安装Visual Studio 2019
    安装时在【工作负载】必须勾选【使用C++的桌面开发】
  2. 下载cyrus-sasl源码
    Githubclone或者下载zip包,我本来是需要2.1.26,但是从从https://www.cyrusimap.org/releases/下载对应版本的源码包编译都有问题,因此直接从Github下载2.1分支的包,解压到C:\ ,并重命名C:\cyrus-sasl-2.1
  3. Python3.7环境(可选)
    我编译sasl是因为在windows使用python的sasl模块时有问题,因此通过python的sasl模块来调试生成的libdll是否可用(当然也可以直接通过sasl自带的clientserver或者其他调用软件进行调试)。从官网下载安装python3.7即可。

编译

  1. 打开编译控制台
    编译32位的,因此在开始菜单的Visual Studio 2019目录下选择【x86 Native Tools Command Prompt for VS 2019】;编译64位的就选择【x64 Native Tools Command Prompt for VS 2019】。下面先编译32位的,再编译64位的
    在这里插入图片描述
  2. 编译32位
    直接进入源码的lib目录
C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl STATIC=no
...
windlopen.obj : error LNK2001: 无法解析的外部符号 _anonymous_server_plug_init
...

查看是否执行成功,如果编译报以上错误,是静态链接插件的库不存在,可以选择需要修改win32/include/config.h注释所有静态插件,那么插件都通过动态链接库的形式提供;也可以选择将插件的文件从plugins拷贝到lib目录,并修改所有插件文件中的***_server_plug_init***_client_plug_init方法,在方法前面增加LIBSASL_API作为导出函数,并在lib\NTMakefile中添加对应的模块,具体查看https://jira.mongodb.org/browse/CXX-645#。我需要使用动态加载插件,因此直接修改config.h

// #define STATIC_ANONYMOUS 1
// #define STATIC_DIGESTMD5 1
// #define STATIC_NTLM 1
// #define STATIC_PLAIN 1
// #define STATIC_SCRAM 1

重新编译和安装,并拷贝lib\libsasl.lib命名为lib\sasl2.lib

C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl STATIC=no
C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl STATIC=no install
C:\cyrus-sasl-2.1\lib>copy /Y C:\sasl\lib\libsasl.lib C:\sasl\lib\sasl2.lib

接下来进入include目录安装头文件

C:\cyrus-sasl-2.1\include>nmake /f ntmakefile prefix=c:\sasl STATIC=no install

安装完成在prefix指定的目录C:\sasl会有如下

C:\SASL
├─bin
│      libsasl.dll
│      libsasl.pdb
├─include
│  └─sasl
│          *.h
└─lib
        libsasl.lib
        sasl2.lib
  1. 编译64位
    打开x64 Native Tools Command Prompt for VS 2019的控制台,直接进入源码的lib目录,先清理之前32位的编译,同时修改prefixC:\sasl64
C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl64 STATIC=no clean
C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl64 STATIC=no
C:\cyrus-sasl-2.1\lib>nmake /f ntmakefile prefix=C:\sasl64 STATIC=no install
C:\cyrus-sasl-2.1\lib>copy /Y C:\sasl64\lib\libsasl.lib C:\sasl64\lib\sasl2.lib
C:\cyrus-sasl-2.1\lib>cd ..\include
C:\cyrus-sasl-2.1\include>nmake /f ntmakefile prefix=C:\sasl64 STATIC=no install

两个版本顺利安装完成

调试python的sasl

安装好python3.7和pip之后,直接安装sasl,32位的Python需要使用C:\sasl\,64位的Python需要使用C:\sasl64\

C:\>pip install sasl --global-option=build_ext --global-option=-IC:\\sasl64\\include --global-option=-LC:\\sasl64\\lib
...
    ...\sasl\saslwrapper.h(26): fatal error C1083: 无法打开包括文件: “unistd.h”: No such file or directory
...

unistd.h是Linux下的头文件,增加一个头文件放在INCLUDE路径所在的目录,我把它放在C:\sasl\include\C:\sasl64\include\

#ifndef _UNISTD_H
#define _UNISTD_H    1

#define R_OK    4
#define W_OK    2
#define F_OK    0 

#define access _access
#define dup2 _dup2
#define execve _execve
#define ftruncate _chsize
#define unlink _unlink
#define fileno _fileno
#define getcwd _getcwd
#define chdir _chdir
#define isatty _isatty
#define lseek _lseek

#define size_t unsigned int

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

char *getpass(const char *prompt)
{    
    return "no pass";
}

#endif /* unistd.h  */

重新编译,查看是否会报以下错误

C:\>pip install sasl --global-option=build_ext --global-option=-IC:\\sasl64\\include --global-option=-LC:\\sasl64\\lib
...
 LINK : fatal error LNK1181: 无法打开输入文件“sasl2.lib”
...

报以上错误是因为默认编译sasl生成的库是libsasl.lib,需要拷贝重命名为sasl2.dll

C:\>copy C:\sasl64\lib\libsasl.lib C:\sasl64\lib\sasl2.lib

如果报以下错误,那么可能是python的版本和sasl的版本不一致(32位还是64位必须一致)

C:\>pip install sasl --global-option=build_ext --global-option=-IC:\\sasl64\\include --global-option=-LC:\\sasl64\\lib
...
saslwrapper.obj : error LNK2001: 无法解析的外部符号 sasl_decode
...

检查确定sasl编译的时候使用了正确的版本。
一切顺利编译完成,还需要最后一步就是拷贝libsasl.dll到安装目录

C:\>FOR /F "usebackq delims=" %A IN (`python -c "from importlib import util;import os;print(os.path.dirname(util.find_spec('sasl').origin))"`) DO copy C:\sasl64\bin\libsasl.dll %A 

拷贝完成之后就可以在python中使用sasl

>>> import sasl
>>> client = sasl.Client()

注意python在单独使用sasl库的时候必须在init()方法之前初始化socket,否则会报初始化失败,具体原因查看[解决方案] Windows pyhive Could not start SASL分析

'Livia', '415 534-9219', '5720 McAuley St.', 'Oakland', 'CA', '946cyrus-sasl 是一款用于实现身份验证、授权和加密通信的开源软件。安09'); INSERT INTO authors (au_id, au_lname, au_fname, phone, address, city, state, zip) VALUES ('807装 cyrus-sasl 可以提供更加安全的通信保护。以下是 cyrus-sasl 的编译安-91-6654', 'Panteley', 'Sylvia', '301 946-8853', '1956 Arlington Pl装步骤: 1. 下载 cyrus-sasl 的源代码包,解压缩到任意目录下。 2..', 'Rockville', 'MD', '20853'); INSERT INTO authors (au_id, au_lname, au_fname, phone, address 进入 cyrus-sasl 源代码目录,执行以下命令: ``` ./configure --prefix=/usr/local/cyrus-s, city, state, zip) VALUES ('846-92-7186', 'Hunter', 'Sheryl', '415 836-712asl ``` 其中,--prefix 参数指定了安装目录,可以根据实际情况进行修改。 3.8', '3410 Blonde St.', 'Palo Alto', 'CA', '94301'); INSERT INTO authors (au_id, au_l 执行 make 命令进行编译。 ``` make ``` 4. 执行 make install 命令进行安装。 ``` make installname, au_fname, phone, address, city, state, zip) VALUES ('893-72-1158', 'McBadden', ``` 5. 安装完成后,可以在指定的安装目录下找到 cyrus-sasl 的相关文件 'Heather', '707 448-4982', '301 Putnam', 'Vacaville', 'CA', '95688'); 。例如,在本例中,可以在 /usr/local/cyrus-sasl 目录下找到 cyrus-sasl 的二进INSERT INTO authors (au_id, au_lname, au_fname, phone, address, city, state, zip) VALUES ('899-46制文件、库文件和头文件等。 以上就是 cyrus-sasl 的编译安装步骤。需要注意的是-2035', 'Ringer', 'Anne', '801 826-0752', '67 Seventh Av.', 'Salt Lake City',,安装 cyrus-sasl 前需要先安装相关依赖库和工具,例如 gcc、make、openssl 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值