golang cgo windows mingw64 環(huán)境搭建

[TOC]

準(zhǔn)備

  • 網(wǎng)絡(luò)一定要ok
  • 提前安裝好 cmder 或者 cygwin,涉及大量命令操作,而windows本身命令行非常難用,不適合開發(fā)

安裝 Mingw-w64

MinGW全稱Minimalist GNU For Windows,是個精簡的Windows平臺C/C++、ADA及Fortran編譯器
相比Cygwin而言,體積要小很多,使用較為方便。
MinGW提供了一套完整的開源編譯工具集,以適合Windows平臺應(yīng)用開發(fā),且不依賴任何第三方C運(yùn)行時庫

MinGW包括:

  • 一套集成編譯器,包括C、C++、ADA語言和Fortran語言編譯器
  • 用于生成Windows二進(jìn)制文件的GNU工具的(編譯器、鏈接器和檔案管理器)
  • 用于Windows平臺安裝和部署MinGW和MSYS的命令行安裝器(mingw-get)
  • 用于命令行安裝器的GUI打包器(mingw-get-inst)

MingW 分 32位和64位版本:下載地址分別如下:
http://sourceforge.net/projects/mingw/
http://sourceforge.net/projects/mingw-w64/

gcc 主要有三種不同的線程庫的定義,分別是 Win32,OS/2,以及 POSIX
前兩種定義只適合于他們各自的平臺,而 POSIX wiki 定義的線程庫是適用于所有的計(jì)算平臺的,故肯定使用 threads-posix

C++ Exceptions有三種處理方式

  • DWARF 不傳遞錯誤,需要使用DWARF-2(或DWARF-3)調(diào)試信息,生成的包會很大
  • SJLJ 長跳轉(zhuǎn),即使拋出異常也正常的執(zhí)行,可以在沒有使用GCC編譯的代碼或者沒有調(diào)用堆棧展開信息的代碼中工作
  • SEH 結(jié)構(gòu)化異常處理,Windows使用自己的異常處理機(jī)制

下載 MinGW

這里選擇的是 Mingw-w64 win64 Personal Builds
8.1.0 posix seh x86_64-8.1.0-release-posix-seh-rt_v6-rev0

安裝 MinGW

直接下載解壓即可,解壓的目錄 %MINGW64_HOME% ,將 %MINGW64_HOME%\bin 配置在環(huán)境變量 Path 中
然后命令行輸入 gcc -v 打印

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

配置全局 make

進(jìn)入mingw64 的安裝目錄,新建文件 make.bat 內(nèi)容為

@echo off
C:\mingw64\bin\mingw32-make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

注意目錄按安裝目錄來

保存后測試配置

$ make -v
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

golang 在win下交叉 cgo 編譯注意

  • cygwin 本身也能安裝 GNU 的 make 或者 gcc 工具鏈,但是非常容易出現(xiàn)不能編譯的問題,故建議使用 mingw64 的 gcc
  • cygwin 本身只作為一個備用編譯方案,如果 mingw64 出現(xiàn)問題,可以切換到 cygwin 來執(zhí)行
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容