Compiling OpenSSL with MinGW

Qt 5 / QtWebkit can make use of OpenSSL, which MinGW distributions do not ship.

Pre-built packages

The Win32OpenSSL project [slproweb.com] provides pre-compiled libraries for both 32 and 64 bit. However, these depend on the Microsoft Visual C++ 2008 runtime being installed.

Compiling on your own

Requirements

How to build

  • Download latest OpenSSL from http://www.openssl.org/source (e.g. openssl-1.0.1c.tar.gz)
  • Start a MSYS command prompt (C:\msys\1.0\msys.bat)
  • Extract tar.gz using MSYS tar (and ignore the symlink warnings)
    • don’t use 7zip or other apps, since they fail to set up any symlinks! tar will also complain about symlinks, but compilation will still succeed.

  1. $ tar xvzf openssl -1.0.1c. tar. gz
  2. $ cd openssl -1.0.1c

Check that gcc is in PATH, otherwise add it, e.g.

  1. $ export PATH =/c /Mingw -builds /bin :$PATH

for MinGW (32 bit) do:

  1. $ . /Configure  --prefix =$PWD /dist no -idea no -mdc2 no -rc5 shared mingw

for MinGW-w64 do:

  1. $ . /Configure  --prefix =$PWD /dist no -idea no -mdc2 no -rc5 shared mingw64

Compile & install:

  1. $ make depend  && make  && make install

Errors

If you run into

  1. perl asm /sha1 -x86_64. pl mingw64  > sha1 -x86_64. s
  2. gcc  -I..  -I.. /..  -I.. /modes  -I.. /asn1  -I.. /evp  -I.. /.. /include   -D_WINDLL  -DOPENSSL_PIC  -DOPENSSL_TH
  3. READS  -D_MT  -DDSO_WIN32  -DL_ENDIAN  -O3  -Wall  -DWIN32_LEAN_AND_MEAN  -DUNICODE  -D_UNICODE  -DOPENSSL_IA
  4. 32_SSE2  -DOPENSSL_BN_ASM_MONT  -DOPENSSL_BN_ASM_MONT5  -DOPENSSL_BN_ASM_GF2m  -DSHA1_ASM  -DSHA256_ASM  -
  5. DSHA512_ASM  -DMD5_ASM  -DAES_ASM  -DVPAES_ASM  -DBSAES_ASM  -DWHIRLPOOL_ASM  -DGHASH_ASM  -c   -o sha1 -x86_
  6. 64. o sha1 -x86_64. s
  7. sha1 -x86_64. s : Assembler messages :
  8. sha1 -x86_64. s : 1824 : Warning : end of file not at end of a line ; newline inserted
  9. sha1 -x86_64. s : 2183 : Error : number of operands mismatch  for `rol '
  10. make[2]: *** [sha1-x86_64.o] Error 1
  11. make[2]: Leaving directory `/d/dev/tmp/openssl-1.0.1c/crypto/sha'
  12. make [ 1 ] :  ***  [subdirs ] Error  1
  13. make [ 1 ] : Leaving directory ` /d /dev /tmp /openssl -1.0.1c /crypto '
  14. make: *** [build_crypto] Error 1

check out http://openssl.6102.n7.nabble.com/Compile-error-with-MinGW-w64-td36657.html

  • Use MSYS perl
  • or change crypto/perlasm/x86_64-xlate.pl as mentioned in the e-mail thread.

Using it

Add the bin, include, lib folders to your compilation environment, e.g. for cmd.exe:

  1. C :\ > set PATH =%PATH %;C :\openssl -1.0.1c\dist\bin
  2. C :\ > set INCLUDE =%INCLUDE %;C :\openssl -1.0.1c\dist\include
  3. C :\ > set LIB =%LIB %;C :\openssl -1.0.1c\dist\lib

When you now run Qt’s configure.exe openssl should be detected, and Qt links against the libraries.


### 解决 C2338 错误以启用 Unicode 支持 C2338 是一种编译器错误,通常发生在程序试图使用某些特性时未能满足特定条件的情况下。当涉及到 Unicode 和 UTF-8 的支持时,可以通过调整项目的编译设置来解决问题。 #### 启用 `/utf-8` 编译选项 为了使项目能够正确处理 UTF-8 字符集,可以启用 `/utf-8` 编译选项。此选项的作用是将源字符集和执行字符集指定为 UTF-8 编码的字符集[^2]。这相当于在命令行中指定了以下参数: ```plaintext /source-charset:utf-8 /execution-charset:utf-8 /validate-charset ``` 这些标志确保了源文件中的字符串以及运行时的行为均基于 UTF-8 进行解析。 #### 修改项目属性配置 对于 Visual Studio 用户,在项目属性中完成如下操作即可应用上述编译选项: 1. 打开 **项目属性**对话框。 2. 转到 `Configuration Properties -> C/C++ -> Command Line` 部分。 3. 在附加选项字段中输入 `/utf-8` 并保存更改。 通过这种方式,Visual C++ 将按照 UTF-8 字符集进行代码编译[^3]。 #### 处理可能引发的其他问题 尽管启用了 `/utf-8` 选项,但如果应用程序仍然依赖于 ANSI 或 MBCS 函数接口,则可能会遇到兼容性问题。这是因为 Windows 默认情况下会假设所有非 Unicode 程序使用的字符编码为当前系统的活动代码页而非 UTF-8[^1]。因此,推荐的做法是在涉及系统 API 调用的地方尽可能采用宽字符版本(即 `_wcs*` 类型函数),并将数据从 UTF-8 映射至 UCS-2/UTF-16 格式后再传递给底层操作系统服务。 以下是实现这一转换的一个简单例子: ```cpp #include <locale> #include <codecvt> #include <string> std::wstring utf8_to_wide(const std::string& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; return converter.from_bytes(str); } int main() { const char* utf8_str = u8"你好"; auto wide_str = utf8_to_wide(utf8_str); MessageBoxW(NULL, wide_str.c_str(), L"Title", MB_OK); // 使用宽字节API显示消息框 } ``` 以上代码片段展示了如何利用标准库组件把一个 UTF-8 编码的标准字符串转成适合传入 Win32 宽字节 API 的形式。 #### 总结 要解决 C2338 错误并成功启用 Unicode 支持,关键是合理运用 `/utf-8` 编译开关,并妥善管理不同层次上的文字表示方法之间的映射关系。只有这样才能既享受现代文本处理的优势又避免潜在的文化冲突风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值