Set up P4 API for Go on Windows
Before you install P4 API for Go on Windows, ensure the following:
-
You know which version of Windows you are using (for example, x86 (32-bit), x64 (64-bit), and ARM64)
-
Admin rights (for installations)
To install P4 API for Go on Windows:
Step 1: Get Go 1.24
-
Go to the Go website and download a version of Go 1.24 that is compatible with your Windows device. For example, for Windows 64-bit, get
go1.24.x.Windows-amd64.msi. -
Install Go.
-
(Optional) To verify the installation using the command line, run:
go version.Expected output:
go version go1.24.x Windows/amd64
Step 2: Install MSYS2 (MinGW toolchain)
-
Go to the MSYS2 website and download a version that matches your Windows system. For example, for Windows 64-bit, get
msys2-x86_64-20260322.exe. -
Install MSYS2.
-
Open MSYS2 MinGW64.
-
In the MinGW64 terminal, install GCC by running:
pacman -S mingw-w64-x86_64-gcc -
(Optional) To verify the installation, in MinGW64 run:
gcc --versionIf successful, GCC version information is displayed.
Step 3: Make Go available in MINGW64
-
In the MinGW64 terminal, run:
export PATH="/c/Program Files/Go/bin:$PATH" -
(Optional) To verify Go is available in MINGW64, run:
go versionIf successful, Go version information is displayed.
Step 4: Download P4 API for C/C++
-
Go to the Perforce repository.
-
Navigate to a current release. For example,
r25.2. -
Download a P4 API for C/C++ build that:
-
Works with MinGW (GCC)
-
Matches OpenSSL 3 (required for compatibility)
-
Is 64-bit
-
Uses POSIX + SEH (correct for modern MINGW64)
For example, download
p4api-openssl3_gcc8_posix_seh.zip. -
-
Extract the contents to a known location. For example,
C:\p4api. -
Verify that the following files exist:
-
C:\p4api\include\p4\clientapi.h -
C:\p4api\lib
-
Step 5: Install OpenSSL (MinGW version)
-
In the MinGW64 terminal, run:
pacman -S mingw-w64-x86_64-openssl -
(Optional) To confirm that OpenSSL was installed correctly, check that the required libraries are present:
-
Run:
ls /mingw64/lib | grep ssl -
Expected output for
ssl:libssl.a-
libssl.dll.a
-
Run:
ls /mingw64/lib | grep crypto -
Expected output for
crypto-
libcrypto.a -
libcrypto.dll.a
-
If these files are not present, OpenSSL is not installed correctly.
-
Step 6: Configure CGO, set paths and linker flags
-
In the MinGW64 terminal, run the following commands:
-
Enable CGO:
go env -w CGO_ENABLED=1 -
Set include path:
go env -w CGO_CPPFLAGS="-IC:/p4api/include -DOS_NT -g" -
Set linker flags:
go env -w CGO_LDFLAGS="-LC:/p4api/lib -L/mingw64/lib -lp4api -lssl -lcrypto -lcrypt32 -lws2_32 -lole32 -lshell32 -luser32 -ladvapi32 -pthread -v"
-
-
(Optional) To verify the configuration is in place, run:
go env CGO_LDFLAGSCheck the output matches the configuration entered above.
Step 7: Create a test project
-
Create a folder in a known location. For example,
c:\p4test. -
In the new folder, create a file named
main.goand enter the following code.Copypackage main
import (
"fmt"
p4go "github.com/perforce/p4go"
)
func main() {
p4conn := p4go.New()
_, err := p4conn.Connect()
if err != nil {
fmt.Println("Connection failed:", err)
return
}
fmt.Println("Connected!")
p4conn.Disconnect()
}
Step 8: Build the project
-
In the MinGW64 terminal, to navigate to the test folder, run:
cd /c/p4test -
Initialize the module:
go mod init p4test -
Download the dependencies:
go mod tidy -
(Optional) Check the test folder to confirm that the following files are present.
-
main.go
-
go.mod
-
go.sum
If they are, the download and setup completed successfully.
-
-
Build the project:
go build -xThe build completes with no errors, you will see the this file:
p4test.exe
Step 9: Run the program
-
In the MinGW64 terminal, run the program:
./p4test.exeIf successfully installed, you will see either:
-
Connected! -
Connection failed: <message>
Both indicate that P4 API for Go is installed and working correctly.
-
Next steps
View the documentation forP4 API for Go structs and interfaces to see what other commands are available when using P4 API for Go.