Integrating C libraries into the Go language on Windows
Today, we will see how we can integrate C libraries into the Go language when using the Windows operating system.
Many Go packages use C libraries, which is why sometimes you’ll get errors for not having CGO enabled.
But what is Cgo?
Cgo enables the creation of Go packages that call C code(https://pkg.go.dev/cmd/cgo)
This is a standard library that allows the creation of Go packages that call C code.
ERROR # runtime/cgo cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH% exit status 1
To use C packages in Go, you’ll need to have a C compiler installed. In my case, I installed Cygwin, but it didn’t work correctly, and MSYS2 also didn’t work, so I ended up using TDM-GCC-64, which you can download from https://jmeubank.github.io/tdm-gcc/
Once you download it and install it, you can simply click the CREATE button
![](https://blastcoding.com/wp-content/uploads/2024/07/tdm64-gcc-10.3.0-2_fLybPOyLFZ-1.png)
Finish the installation, and this should be enough.
Next, you’ll need to open the console for TDM-GCC, which should appear in the Windows start menu. If not, search for ‘MinGW’ in the bottom bar of your Windows; the name it will have is ‘MinGW command prompt’.
Open this new console and check if GOROOT is correct, GOPATH is correct, and CGO_ENABLED has the value 1.
You can find more information about these values here: https://blastcoding.com/en/frequently-used-predefined-environment-variables-in-go/
From the console, try to run the following program to see if you can use C.
Puedes crearte un main con el siguiente codigo para probar si Cgo esta funcionandopackage main /* #include*/ import "C" import "fmt" func main() { fmt.Println("Hello from Go") C.puts(C.CString("Hello from C")) // Wait for user input before exiting var input string fmt.Println("Press Enter to exit...") fmt.Scanln(&input) }
Problems with Antivirus
When running this program, you may encounter the ‘Access denied’ issue. This could be due to your antivirus software. If this happens, check the ‘Hault’ or ‘Quarantine’ just in case. Not all antivirus programs will react to the creation of a program in Go
![](https://blastcoding.com/wp-content/uploads/2024/07/Avira.Spotlight.UI_.Application_5KAh6X3ddj-1.png)
As you can see in my antivirus, when I run ‘go run main.go’, Go creates the .exe file, which the antivirus interprets as a potentially dangerous file for my PC, although it is not. This is because ‘main’ is autogenerated by Go.
We can also see ‘wailsbindings.exe’; this file is created by Wails, a GUI for Go, in that case, it will give us a problem
wails build Wails CLI v2.8.2 # Build Options Platform(s) | windows/amd64 Compiler | C:\Program Files\Go\bin\go.exe Skip Bindings | false Build Mode | production Devtools | false Frontend Directory | C:\Users\luisg\go\src\XXXXXX\frontend Obfuscated | false Skip Frontend | false Compress | false Package | true Clean Bin Dir | false LDFlags | Tags | [] Race Detector | false # Building target: windows/amd64 • Generating bindings: ERROR fork/exec C:\Users\luisg\AppData\Local\Temp\wailsbindings.exe: Access is denied. ERROR fork/exec C:\Users\luisg\AppData\Local\Temp\wailsbindings.exe: Access is denied.
But what actually happens is that the antivirus quarantines this file, so when we run ‘wails dev’ or ‘wails build’, the file will not be found.
You can restore this file if you wish; this will exclude the file from real-time virus scanning.
![](https://blastcoding.com/wp-content/uploads/2024/07/explorer_b6ee855wNM-1.png)
Also, in your antivirus, you can prevent it from checking entire folders in real-time if you wish.
![](https://blastcoding.com/wp-content/uploads/2024/07/gBBCCaPDyb-1.png)
Unfortunately, when we run ‘go build main’, it creates a ‘main.exe’ file in a different folder within Temp. Anyway, we could exclude Temp from real-time scanning.