Search code examples
c++cvisual-studiocgo

How to build a library with cgo using visual studio compiler?


It is a bit of a newbie question, but I can't seems to make it work.

I know I should use CC=, CXX= and i know the vs compiler name is cl though I'm not sure it is also true for the c++ compiler.

Now assuming the compiler name is cl for both I get the error: go build CC=cl cxx=cl --buildmode=c-shared -o ../lib/core.dll

I get: can't load package: package CC=cl: cannot find package "CC=cl" in any of: ...

If on the other hand I try to compile without the CC / CXX options then it is all working good.

SO to sum it up: 1. what is the c and c++ compiler name for vs? 2. why i get an error when trying to choose which compiler it will use?


Solution

  • After a few days of playing around it seems that the only working way to create a library that would work with visual studio is to:

    1. create a .dll (using cgo with go 1.10+)
    2. use the bat file or the method mention in HERE to create a stub lib to connect the dll to visual studio.

      • a side note it might be also possible to use loadlibrary func (I did not try that as i tried to avoid this method as it seems to be not a recommended approach).