Go under Windows can export static library and dynamic library, static library is. A file is not for VS use, also can not be converted to vs use static library. Dynamic libraries can be used for VS, but Windows requires header files, lib files, and DLL files. The next step is to generate a lib file from the DLL file.

Using the step

  1. Go build generates DLL files and.h files:
go build -ldflags "-s -w" -buildmode=c-shared -o xxx.dll
Copy the code

If you want to generate 32-bit DLL, cross-compilation does not support CGO, and Windows 32-bit system may not support DLL compilation, you need to use GCC static library into dynamic library (install 32 bit MINGw compiler), the command is as follows

go build -ldflags "-s -w" -buildmode=c-archive -o xxx.a
ar x xxx.a
gcc -shared -o xxx.dll *.o -lwinmm -lntdll -lWs2_32
Copy the code
  1. Write the def file with the name of the function to be exported:
EXPORTS DownloadRes GetCurrentDownloadSize GetDownloadSpeed GetDownloadState GetTotalDownloadSize InitClient StartClient  StopClientCopy the code

You can also use the dumpbin tool provided by the VS installation directory to generate def files. The dumpbin tool can generate def files in the VS installation directory of the VC\bin\dumpbin.exe command dumpbin.exe /exports xxx.dll > xxx.def. But you need to change it to def template format. Exe in the VS directory. Run the lib.exe /machine:X64 /def:xxx.def command to generate the lib file (32-bit: /machine:X86). So far you have headers, lib files, and DLL files.