> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 适用于 Linux 的 C++ 快速入门

> 在 Ubuntu 18.04 LTS 上设置原生 C++ 项目，并使用跨平台 PlayFab XPlatCppSdk 库进行首次 PlayFab API 调用。

# 快速入门：适用于 Linux 的 C++

开始使用适用于 C++ 的 PlayFab 客户端库。按照步骤安装包并试用基本任务的示例代码。

本快速入门可帮助您使用 C++ 客户端库进行首次 PlayFab API 调用。

本快速入门是使用 Ubuntu 18.04 LTS 编写的。

[API 参考文档](/services/playfab/api-references) | [库源代码](https://github.com/PlayFab/XPlatCppSdk)

## 要求

* 一个 [PlayFab 开发者帐户](https://developer.playfab.com)。

## Linux C++ 项目设置

1. 安装以下软件（在 Ubuntu 上使用 sudo apt-get install \_\_\_\_）：
   * g++
   * gdb
   * make
   * cmake
   * libjsoncpp-dev
   * libcurl4-openssl-dev
   * git-all
2. 将 [PlayFab 跨平台 (CPP) SDK](https://github.com/PlayFab/XPlatCppSdk) 克隆到您的项目文件夹中。

要验证您的安装是否正确：

创建一个 main.cpp 并插入下面显示的 "Hello World" 代码。

```cpp theme={null}
// main.cpp: entry point for the console application

#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}
```

如果您使用的是 IDE，请确保：

* 将以下文件夹添加到您的源目录：
  * XPlatCppSdk/cppsdk/source/playfab

* 将以下文件夹添加到您的包含目录：
  * XPlatCppSdk/cppsdk
  * XPlatCppSdk/cppsdk/include

* 链接以下库：
  * jsoncpp
  * curl
  * pthread

否则，创建一个名为 CMakeLists.txt 的文件，并复制下面显示的构建命令。

```cmake theme={null}
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)

project(PlayFab_Test)

file(GLOB PF_SOURCE XPlatCppSdk/cppsdk/source/playfab/*.cpp)
add_executable(PlayFab_Test ${PF_SOURCE} main.cpp)

include_directories(XPlatCppSdk/cppsdk)
include_directories(XPlatCppSdk/cppsdk/include)
target_link_libraries(PlayFab_Test -ljsoncpp -lcurl -lpthread)
```

* 从 IDE 内构建并运行项目，或打开终端提示符并运行：
  * `cmake .`
  * `make`
  * `./PlayFab_Test`

## 设置您的首次 API 调用

本指南提供了进行首次 PlayFab API 调用所需的最少步骤，没有任何 GUI 或屏幕反馈。确认通过控制台打印语句进行。

首先，将 `main.cpp` 的内容替换为下面显示的内容。

```cpp theme={null}
// main.cpp: entry point for the console application

#include "playfab/PlayFabClientDataModels.h"
#include "playfab/PlayFabClientApi.h"
#include "playfab/PlayFabSettings.h"
#include <unistd.h>

using namespace PlayFab;
using namespace ClientModels;

bool finished = false;

void OnLoginSuccess(const LoginResult& result, void* customData)
{
    printf("Congratulations, you made your first successful API call!\n");
    finished = true;
}

void OnLoginFail(const PlayFabError& error, void* customData)
{
    printf("Something went wrong with your first API call.\n");
    printf("Here's some debug information:\n");
    printf(error.GenerateReport().c_str());
    printf("\n");
    finished = true;
}

int main()
{
    PlayFabSettings::staticSettings->titleId = ("144");

    LoginWithCustomIDRequest request;
    request.CreateAccount = true;
    request.CustomId = "GettingStartedGuide";

    PlayFabClientAPI::LoginWithCustomID(request, OnLoginSuccess, OnLoginFail);

    while (PlayFabClientAPI::Update() != 0)
        sleep(1);

    printf("Press enter to exit\n");
    getchar();
    return 0;
}
```

## 完成并执行

1. 在 IDE 中构建并运行项目，或打开终端并运行：
   * `./PlayFab_Test`

2. 加载时，将显示以下文本：
   * "Congratulations, you made your first successful API call!"

3. 开始进行其他 API 调用，并构建您的游戏。


## Related topics

- [Linux 快速入门](/zh-CN/services/playfab/multiplayer/networking/linux-specific-requirements.md)
- [适用于 Windows 的 C++ 快速入门](/zh-CN/services/playfab/sdks/playfab-cpp/quickstart-windows.md)
- [适用于 Linux 的 PlayFab Services](/zh-CN/services/playfab/sdks/platforms/linux.md)
- [适用于 GDK 的 C/C++ 快速入门](/zh-CN/services/playfab/sdks/playfab-cpp/quickstart-gdk.md)
- [适用于 Corona 的 Lua 快速入门](/zh-CN/services/playfab/sdks/lua/quickstart-corona.md)
