> ## 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 Client 程式庫。請依照步驟安裝套件，並試用基本工作的範例程式碼。

本快速入門可協助您使用適用於 C++ 的 Client 程式庫進行第一次 PlayFab API 呼叫。

本快速入門是使用 Ubuntu 18.04 LTS 撰寫的。

[API 參考文件](/zh-TW/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

* 將下列資料夾新增至您的 include 目錄：
  * 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-TW/services/playfab/sdks/c/quickstart-linux.md)
- [適用於 Windows 的 C++ 快速入門](/zh-TW/services/playfab/sdks/playfab-cpp/quickstart-windows.md)
- [適用於 GDK 的 C/C++ 快速入門](/zh-TW/services/playfab/sdks/playfab-cpp/quickstart-gdk.md)
- [適用於 Linux 的 PlayFab Services](/zh-TW/services/playfab/sdks/platforms/linux.md)
- [Unity 快速入門](/zh-TW/services/playfab/sdks/unity3d/quickstart.md)
