Quick start for P4 API for C/C++ on Windows

This quick start guide covers how to:

  • Install P4 API for C/C++.

  • Create a Visual Studio project.

  • Verify that P4 API for C/C++ is installed correctly.

  • Connect to P4 Server.

  • Check out a file to work.

  • Submit the file to the P4 Server.

Before you begin

Ensure you have the following:

  • Access to a working version of P4 Server.

  • The server and port values for P4 Server.

  • The username for your P4 user.

  • The name of the workspace that contains the files you want to check out, edit, and commit.

  • This quick start uses Visual Studio Community 2026 (or newer) with the Desktop development with C++ workload installed.

  • OpenSSL 3.x.

  • (Optional) A working version of P4V, to check if a file has been added to a changelist.

Install P4 API for C/C++

  1. Download the P4 API for C/C++ ZIP file from the Perforce website.

    To determine which version to download, open PowerShell and run:

    • $env:PROCESSOR_ARCHITECTURE

      For the following outputs, download the corresponding version of P4 API for C/C++:

      • AMD64 - Windows (x64)

      • ARM64 - Windows (ARM64)

      • x86 - Windows (x64)*

      * P4 API for C/C++ is provided for modern 64-bit Windows platforms. If your device returns x86, verify whether you are running a 32-bit operating system or a 32-bit shell on a 64-bit operating system.

      If you are using Visual Studio Community and compiling with cl.exe, download Windows (x64).

      Only download Minimalist GNU for Windows (x64) if you are compiling with the MinGW/GCC toolchain.

    • openssl version

      Download a P4 API for C/C++ that matches the major OpenSSL version installed on your device. For example, OpenSSL 3.6.3 is compatible with a P4 API for C/C++ package that contains openssl3.

  2. Extract the package to a location such as: c:\p4api

  3. To verify that the SDK was extracted successfully:

    1. Go to the folder c:\p4api\include\p4

    2. Check the file clientapi.h is present.

You are now ready to create a Visual Studio project and verify that P4 API for C/C++ is installed correctly.

Configure P4 API for C/C++ in Visual Studio

This involves creating a project and then adding the paths to the various libraries and files.

Create the Project

  1. Open Visual Studio.

  2. Click Create a new project.

  3. Select Console App (C++) and click Next.

  4. Name the project. For example, p4test.

  5. Click Create.

Set the Build Configuration

  1. In Visual Studio, locate the build configuration drop-down in the toolbar.

  2. Set Configuration to Release.

  3. Set Platform to x64.

The P4 API for C/C++ libraries are built using Release settings.

Building a Debug configuration can result in runtime library mismatch errors.

Configure the project paths

Visual Studio needs to know where to find the libraries for P4 API for C/C++. To configure the paths:

  1. In Visual Studio, open your project and open the Solution Explorer (Ctrl + Alt+ L).

  2. Open Properties (Alt+ Enter).

  3. At the top of the Properties window, set the following:

    1. Configuration: All Configurations

    2. Platform: x64

      If you are using ARM64 Windows, use ARM64 libraries and select the ARM64 platform in Visual Studio.

  4. Update the include path:

    1. Click C/C++ > General.

    2. Find the Additional Include Directories field.

    3. Click Edit. (It is in a drop-down on the right of the field)

    4. In the edit window, enter the following file path: C:\p4api\include\p4

    5. Click Apply.

  5. Update the library paths:

    1. Click Linker > General.

    2. Find the Additional Library Directories field.

    3. Click Edit. (It is in a drop-down on the right of the field)

    4. In the edit window, enter each directory on a separate line:

      C:\p4api\lib
      C:\Program Files\OpenSSL-Win64\lib\VC\x64\MD

      Note: The OpenSSL directory might differ depending on the OpenSSL version and installation location. This procedure assumes that the OpenSSL libraries (libssl.lib and libcrypto.lib) are installed.

    5. Click Apply.

  6. Add the library dependencies:

    1. Click Linker > Input.

    2. Find the Additional Dependencies field.

    3. Click Edit. (It is in a drop-down on the right of the field)

    1. This field might already contain the following values:

      $(CoreLibraryDependencies)
      %(AdditionalDependencies)

      Do not delete the existing values. Append the following file names to the existing value:

    1. In the edit window, enter each file on a separate line:

      libp4api.lib
      ws2_32.lib
      iphlpapi.lib
      bcrypt.lib
      crypt32.lib
      libssl.lib
      libcrypto.lib

      An example of an updated Additional Dependencies is as follows:

      $(CoreLibraryDependencies)
      %(AdditionalDependencies)
      libp4api.lib
      ws2_32.lib
      iphlpapi.lib
      bcrypt.lib
      crypt32.lib
      libssl.lib
      libcrypto.lib
    1. Click Apply.

  7. Click OK.

Test the file paths

To ensure the file paths are configured correctly:

  1. In Visual Studio, open the source file created for your project. For example, p4test.cpp.

  2. Replace the existing code with the following:

    #include "clientapi.h"
    
    int main()
    {
        ClientApi client;
        return 0;
    }
  3. Build the solution. (Either click Build or (Ctrl + Shift+ B).

  4. Expected result: Build: 1 succeeded, 0 failed

If the build succeeds, Visual Studio can locate the P4 API for C/C++ header files, libraries, and required OpenSSL dependencies.

Connect to P4 Server

Use the following program to connect to P4 Server and display the first five files returned by the server. Make a note of the changelist number returned by the program. You will need it when submitting the file.

Before building the program, update the following values to match your environment:

  • client.SetPort() with your P4 Server address and port.

  • client.SetUser() with your P4 user.

For example:

client.SetPort("1666");
client.SetUser("P4UserName");

Create the program

  1. In Visual Studio, create a new source file named: connect_to_p4.cpp

  2. Replace the contents of the file with the following code:

    Copy
    #include "clientapi.h"
    #include <iostream>

    class MyClientUser : public ClientUser
    {
    public:
        int count = 0;

        void OutputInfo(char level, const char *data) override
        {
            if (count < 5)
            {
                std::cout << data << std::endl;
                count++;
            }
        }
    };

    int main()
    {
        Error e;
        ClientApi client;
        MyClientUser ui;

        client.SetPort("1666");
        client.SetUser("P4UserName");

        client.Init(&e);

        if (e.Test())
        {
            StrBuf msg;
            e.Fmt(&msg);

            std::cout << "Connection failed:" << std::endl;
            std::cout << msg.Text() << std::endl;

            return 1;
        }

        std::cout << "Connected successfully." << std::endl;
        std::cout << std::endl;
        std::cout << "First 5 files:" << std::endl;
        std::cout << std::endl;

        char *args[] =
        {
            (char *)"//..."
        };

        client.SetArgv(1, args);
        client.Run("files", &ui);

        client.Final(&e);

        return 0;
    }
  3. Build the solution. (Use Start Without Debugging and ensure the build configuration is: Release and x64).

  4. Expected result: The program connects to P4 Server and displays the first five files in the terminal.

Check out a file

The following program will:

  • Connect to P4 Server using your credentials.

  • Create a pending changelist with a simple description.

  • Open a file for edit in that changelist.

  • Display a summary of the actions performed.

  • Display the changelist number.

  • Disconnect from P4 Server.

Update the following values in the program:

  • SetPort() - The P4 Server address and port in the format server:port.

  • SetUser() - Your P4 user name.

  • SetClient() - The workspace to use.

  • DEPOT_FILE - The depot file to open for edit. For example: //DemoDepot/main/README.md

Create the program

  1. In Visual Studio, create a new source file named: check_out_file.cpp

  2. Replace the contents of the file with the following code:

    Copy
    #include "clientapi.h"
    #include <iostream>
    #include <string>

    static const char* DEPOT_FILE =
    "//DemoDepot/main/README.md";

    class ChangeClientUser : public ClientUser
    {
    public:
        StrBuf form;
        int changelistNumber = 0;

        ChangeClientUser()
        {
            form =
                "Change: new\n"
                "\n"
                "Description:\n"
                "\tCreated from P4API Quick Start\n";
        }

        void InputData(StrBuf* strbuf, Error* e) override
        {
            strbuf->Set(form);
        }

        void OutputInfo(char level, const char* data) override
        {
            std::cout << data << std::endl;

            std::string msg(data);

            size_t changePos = msg.find("Change ");
            size_t createdPos = msg.find(" created.");

            if (changePos == 0 && createdPos != std::string::npos)
            {
                changelistNumber =
                    std::stoi(msg.substr(7, createdPos - 7));
            }
        }

        void HandleError(Error* err) override
        {
            StrBuf msg;
            err->Fmt(&msg);
            std::cout << msg.Text() << std::endl;
        }
    };

    class OutputClientUser : public ClientUser
    {
    public:
        void OutputInfo(char level, const char* data) override
        {
            std::cout << data << std::endl;
        }

        void HandleError(Error* err) override
        {
            StrBuf msg;
            err->Fmt(&msg);
            std::cout << msg.Text() << std::endl;
        }
    };

    int main()
    {
        Error e;
        ClientApi client;

        ChangeClientUser changeUi;
        OutputClientUser outputUi;

        client.SetPort("1666");
        client.SetUser("P4UserName");
        client.SetClient("main");

        client.Init(&e);

        if (e.Test())
        {
            StrBuf msg;
            e.Fmt(&msg);

            std::cout << "Connection failed:" << std::endl;
            std::cout << msg.Text() << std::endl;

            return 1;
        }

        std::cout << "Connected successfully." << std::endl;
        std::cout << std::endl;

        //
        // Create changelist
        //
        char* changeArgs[] =
        {
            (char*)"-i"
        };

        client.SetArgv(1, changeArgs);

        std::cout << "Creating changelist..." << std::endl;

        client.Run("change", &changeUi);

        int cl = changeUi.changelistNumber;

        std::cout << std::endl;
        std::cout << "Changelist number: " << cl << std::endl;
        std::cout << std::endl;

        //
        // Open file for edit in that changelist
        //
        std::string clString = std::to_string(cl);

        char* editArgs[] =
        {
            (char*)"-c",
            (char*)clString.c_str(),
            (char*)DEPOT_FILE
        };

        client.SetArgv(3, editArgs);

        std::cout << "Opening file for edit..." << std::endl;

        client.Run("edit", &outputUi);

        //
        // Summary
        //
        std::cout << std::endl;
        std::cout << "Summary" << std::endl;
        std::cout << "-------" << std::endl;
        std::cout << "Created changelist: " << cl << std::endl;
        std::cout << "Opened file: " << DEPOT_FILE << std::endl;
        std::cout << std::endl;
        std::cout << "Make a note of this changelist number." << std::endl;
        std::cout << "You will need it when submitting the file."
            << std::endl;

        client.Final(&e);

        std::cout << std::endl;
        std::cout << "Disconnected from P4 Server." << std::endl;

        return 0;
    }
  3. Build the solution. (Use Start Without Debugging and ensure the build configuration is: Release and x64).

  4. Expected result:

    • A success message is output to the terminal and returns the changelist number (make a note of this for the next step).

    • The file is added to the newly created pending changelist.

  5. To verify the file is checked out, open P4V and check the changelist.

Submit a file

After editing the file, submit the changelist to make your changes available to other users.

The following program will:

  • Connect to P4 Server using your credentials.

  • Prompt for a changelist number.

  • Submit the specified changelist.

  • Display the results returned by P4 Server.

  • Disconnect from P4 Server.

Update the following values in the program:

  • SetPort() - The P4 Server address and port in the format server:port.

  • SetUser() - Your P4 user name.

  • SetClient() - The workspace to use.

Create the program

  1. In Visual Studio, create a new source file named: submit_file.cpp

  2. Replace the contents of the file with the following code:

    Copy
    #include "clientapi.h"
    #include <iostream>
    #include <string>

    class SubmitClientUser : public ClientUser
    {
    public:
        void OutputInfo(char level, const char* data) override
        {
            std::cout << data << std::endl;
        }

        void HandleError(Error* err) override
        {
            StrBuf msg;
            err->Fmt(&msg);
            std::cout << msg.Text() << std::endl;
        }
    };

    int main()
    {
        Error e;
        ClientApi client;
        SubmitClientUser ui;

        std::string changelistNumber;

        std::cout << "Enter the changelist number to submit: ";
        std::getline(std::cin, changelistNumber);

        client.SetPort("1666");
        client.SetUser("P4UserName");
        client.SetClient("main");

        client.Init(&e);

        if (e.Test())
        {
            StrBuf msg;
            e.Fmt(&msg);

            std::cout << "Connection failed:" << std::endl;
            std::cout << msg.Text() << std::endl;

            return 1;
        }

        std::cout << std::endl;
        std::cout << "Connected successfully." << std::endl;
        std::cout << std::endl;

        char* submitArgs[] =
        {
            (char*)"-c",
            (char*)changelistNumber.c_str()
        };

        client.SetArgv(2, submitArgs);

        std::cout << "Submitting changelist..." << std::endl;
        std::cout << std::endl;

        client.Run("submit", &ui);

        client.Final(&e);

        std::cout << std::endl;
        std::cout << "Disconnected from P4 Server." << std::endl;

        return 0;
    }
  3. Build the solution. (Use Start Without Debugging and ensure the build configuration is: Release and x64).

  4. Expected result:

    • The program connects to P4 Server

    • It asks for the changelist number of the changelist you want to submit.

    • Submits the changelist and disconnects from P4 Server.

    • A success message will output this information.

  5. To verify the file has been submitted, open P4V, check the changelist is gone and the file has been submitted.

Congratulations

You have successfully used the P4 API for C/C++ to:

  • Connect to P4 Server.

  • Retrieve repository data.

  • Create a pending changelist.

  • Open a file for edit.

  • Submit a changelist.

You have completed a complete P4 workflow using the P4 API for C/C++ and can now begin building your own applications that interact with P4 Server.

Next Steps