[Golang] How to use CGO on Windows

Source: Internet
Author: User
Tags imagemagick
This is a created article in which the information may have evolved or changed.

Golang's CGO is called GCC compiled C code, the GCC toolchain is very handy on Linux, but not on Windows. The general Vc,golang used on Windows is not supported. So what if there is such a demand? With MinGW.

Packages on Linux generally cannot be compiled directly on Windows, except with the help of Cygwin or msys. (and then there's a whole bunch of dependencies to solve). On the project to CGO call is ImageMagick, directly can find VC compiled library, save a lot of trouble.

It is important to note that MinGW has 32-and 64-bit points (mingw-w64), so be sure not to install the wrong one. (32-bit GCC default compilation parameters are compatible with 386CPU decades ago, so you can give up on the 32-bit bar ... It's hard to get rid of that old historical burden.)

The next problem is that the VC library format is. lib and gcc. A is different, the old version of MinGW needs to use the Reimp tool to convert Lib to a, the new version is directly compatible with the LIB format, Reimp is also removed. Considering the latter link parameter is difficult to write, directly and simply rough changed the name, Core_rl_magickcore_.lib, Libcore.a, Core_rl_magickwand_.lib-libwand.a.

The rest is almost like Linux, set environment variable C_include_path is the INCLUDE path, set environment variable Cgo_ldflags -L "{lib路径}" -lcore -lwand , and finally remember to add ImageMagick SDK defined in the compilation parameters No_ Pkgconfig to block calls to Pkgconfig, go install-tags no_pkgconfig, complete.

Because go has compiled cache, that is, $gopath/pkg, as long as the CGO part of the code does not change, it is not necessary to add this part of the parameter. So if you want to use CGO as a thin package like the ImageMagick SDK, it's very necessary to turn C functions into go functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.