bat脚本根据mingw版本判断是否可编译32/64位程序

MinGW编译器是否可以编译32/64位程序,取决于MinGW的版本。
一般我们下载的mingw-w64版本名是类似下面这样的名字:

i686-5.2.0-posix-dwarf-rt_v4-rev1
x86_64-5.2.0-posix-seh-rt_v4-rev1
x86_64-5.2.0-posix-sjlj-rt_v4-rev1

开始的i686,x86_64好理解,代表适用的处理器架构,i686代表是32位处理器,x86_64则代表64位处理器,
posix则代表线程模式(threading model),windows下还有另一种线程模式win32,这里不展开说明。
i686前缀的版本肯定可以编译32位程序,但是否能编译64位程序则取决于编译器版本的所用的异常实现模型–dwarf,seh,sjlj。

关于异常实现模型的概念还是看本文末尾的参考资料一节中列出的英文原文说得全面,下面是dwarf,seh,sjlj三种模型的简要介绍。

SJLJ (setjmp/longjmp): – available for 32 bit and 64 bit – not “zero-cost”: even if an exception isn’t thrown, it incurs a minor performance penalty (~15% in exception heavy code) – allows exceptions to traverse through e.g. windows callbacks

DWARF (DW2, dwarf-2) – available for 32 bit only – no permanent runtime overhead – needs whole call stack to be dwarf-enabled, which means exceptions cannot be thrown over e.g. Windows system DLLs.

SEH (zero overhead exception) – will be available for 64-bit GCC 4.8.

通过这个简介可以知道
sjlj适合32/64位程序(但是它会带来轻微的性能损失,在异常重的代码中有15%),
dwarf则只能用于32位程序,
seh只能用于64位程序
所以根据这个就可以知道前面列出的三个mingw-w6版本是否能编译32/64位程序了。

gcc有命令行参数--version可以返回编译器的版本信息,如下。

J:\jpegwrapper>gcc --version
gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 5.2.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

所以,通过上面的版本信息,允许在执行脚本时判断编译的编译能力:

下面的bat脚本片段展示如何利用--version参数来判断编译是否能编译32/64位代码:

where gcc
rem 判断是否安装了gcc,没有就退出
if errorlevel 1 (
    echo MinGW/gcc NOT FOUND.
    exit -1
)
echo MinGW/gcc found.
rem 通过查找版本信息中是否有sjlj或seh字符串的判断是否能编译64位程序
gcc --version |findstr "sjlj seh"
if errorlevel 1 (
    echo unsupported x86_64 build
    )else call:gcc_x86_64 

rem 通过查找版本信息中是否有sjlj或dwarf字符串的判断是否能编译32位程序
gcc --version |findstr "sjlj dwarf"
if errorlevel 1 (
    echo unsupported x86 build  
    )else call:gcc_x86
goto :end

参考资料

《What is difference between sjlj vs dwarf vs seh?》

《Exception handling: SJLJ, DWARF, and SEH》

猜你喜欢

转载自blog.csdn.net/10km/article/details/80035064
今日推荐