Skip to content

1. Install PACE and build your first program

中文 · Learning path

In this chapter you set up a project of your own that installs PACE as an xmake package, write a small HTTP server and check it with curl. The following chapters add programs to the same project.

Install xmake and a compiler

PACE builds with xmake 2.9.8 or newer and a C++20 compiler: GCC, Clang or MSVC. CI builds it on Linux, macOS, Windows and 64-bit FreeBSD; platform support lists the exact configurations.

  • Linux and macOS: install xmake as described on its site, and a compiler from your package manager or the Xcode command-line tools.
  • Windows: install xmake and Visual Studio with the C++ workload, then work in a shell that has the Visual Studio C++ environment.
  • FreeBSD: install xmake-io from upstream. The FreeBSD package named xmake is a different tool. The CI workflow shows a pinned source install.

Check the installation with xmake --version. You also need git, which xmake uses to fetch the package description, and curl to try the server below.

Create the project

Make a directory with this layout:

hello/
  xmake.lua
  main.cpp

Write xmake.lua:

set_project("hello")
set_languages("cxx20")
add_rules("mode.debug", "mode.release")
add_repositories("pace https://github.com/TabNahida/PACE.git main")
add_requires("pace")

target("hello")
    set_kind("binary")
    add_files("main.cpp")
    add_packages("pace")

add_repositories adds the PACE repository, which is also an xmake package repository. add_requires("pace") asks for the package with its default modules: the runtime, TCP and UDP, HTTP/1.1 and WebSocket. add_packages("pace") gives the target the include path, the feature definitions, the libraries in link order and the platform libraries. Do not copy PACE's .cpp files into your target or write a link line by hand.

Write the first program

Write main.cpp:

#include <pace/http1.hpp>
#include <iostream>

int main()
{
    try
    {
        pace::http::app app;
        app.get("/health", [] { return pace::http::text("ok\n"); });
        app.post("/echo", [](pace::http::request request) { return pace::http::text(std::move(request.body)); });
        std::cout << "Listening on http://127.0.0.1:8080\n";
        app.run(8080);
    }
    catch (const std::exception& error)
    {
        std::cerr << error.what() << '\n';
        return 1;
    }
}

http::app holds the routes. get and post register a handler for a method and an exact path. http::text makes a plain-text response that owns its body; the /echo handler returns the body it received. app.run(8080) listens on 127.0.0.1:8080 and serves on this thread until the process ends. There is no coroutine in this program; chapter 3 shows where coroutines come in.

Build and run

From hello:

xmake f -m release -y
xmake -y
xmake run hello

xmake f configures the project. The first time, xmake downloads PACE and builds it as a package; later projects with the same configs reuse the installed copy. In a second terminal:

curl -i http://127.0.0.1:8080/health
curl --data-binary hello http://127.0.0.1:8080/echo

Expect status 200 with ok, then hello. Stop the server with Ctrl-C. Keep this project: the next chapter sends requests to this server from a program of your own.

If a PACE header cannot be found, check the add_packages line. If a header such as <pace/tls.hpp> compiles but its types are missing, enable that module's config (next section) and configure again. If curl cannot connect, the server is not running or another program already uses port 8080; the server then reports the bind error.

Enable the modules you need

Each module is a config of the pace package. Enable the ones your program uses; the modules they build on come with them.

Your program uses Config
Tasks, timers, runtime always included
TCP and UDP always included
HTTP/1 clients and servers, http::app, static files and proxy http1 (on by default)
WebSocket websocket (on by default)
TLS tls = true
DNS dns = true
QUIC quic = true
HTTP/2 http2 = true (add tls = true for HTTPS)
HTTP/3 http3 = true (enables QUIC)
add_requires("pace", {configs = {tls = true, dns = true}})

Change the configs and configure again before building; xmake installs a matching build of the package. Include <pace/pace.hpp> for every enabled module, or the umbrella header of one module such as <pace/http1.hpp>. A config that enables a module also adds it to HTTP/1 where it applies: with dns = true the URL clients accept hostnames, and with tls = true they accept https://. Optional modules bring their third-party libraries (OpenSSL, nghttp2, ngtcp2, nghttp3), which xmake fetches the first time. On Windows, QUIC and HTTP/3 need quic_backend = "native". {http1 = false, websocket = false} gives a package with only the runtime, TCP/UDP and the shared HTTP types, without any optional dependency.

Build options lists every config.

Build the companion programs

Most chapters name a companion program in PACE's examples/ directory. It is a complete, self-checking version of the chapter's exercise. Build them in a checkout of PACE, separate from your project:

git clone https://github.com/TabNahida/PACE.git
cd PACE
xmake f -m release --examples=y -y
xmake -y
xmake run tutorial_tasks

tutorial_tasks prints Hello, PACE and exits. xmake test runs PACE's correctness tests. Chapters about optional modules give the extra options to add to xmake f; a source build's options have the same names as the package configs (--tls=y for tls = true).

C++ named module (import pace)

PACE can also be used through C++20 named modules instead of headers. Modules are off by default; enable the modules config and module scanning for the target:

add_requires("pace", {configs = {modules = true}})

target("hello_module")
    set_kind("binary")
    add_files("module_main.cpp")
    add_packages("pace")
    set_policy("build.c++.modules", true)

import pace; brings in every module enabled in the build, like #include <pace/pace.hpp>. Each library module also has its own named module, pace.<module>: pace.runtime, pace.net, pace.http, pace.http1, pace.gateway, pace.tls, pace.dns, pace.quic, pace.http2, pace.http3 and pace.websocket. Each exports only its own names, so a program that uses TCP imports pace.net and pace.runtime. This program needs only the runtime:

#include <coroutine>
#include <iostream>
import pace.runtime;

pace::task<int> answer()
{
    co_await pace::yield();
    co_return 42;
}

int main()
{
    std::cout << pace::run(answer) << '\n';
}

Replace import pace.runtime; with import pace; to import everything. Include the standard headers a source file uses before the import; it needs no PACE header. Modules do not export macros, and xmake still passes the feature definitions and link dependencies through add_packages("pace").

The modules are an import interface over the same static libraries: a translation unit that includes PACE headers and one that imports PACE modules use the same types and link together. Build the module with Clang and a matching clang-scan-deps on PATH, or with GCC 16 or newer; GCC before 16 is rejected with an error. MSVC and Apple Clang module builds are not validated. Rebuild after changing the compiler, the C++ standard or a module option, and do not share built module interfaces between toolchains. Build options lists the validated toolchains.

Next: 2. Make HTTP requests