Frameworks

Deploy a C++ app with CMake

Deploy a C or C++ app built with CMake on LILY from a GitHub repository, with the CLI or the console.

This guide deploys a small HTTP server written in C++ with no dependencies. LILY detects C and C++ apps by their CMakeLists.txt. It finds the port in your code.

Before you start

  • A LILY account with GitHub connected, and the lly CLI installed and signed in. See the Quickstart.
  • A C++ compiler and CMake, to build the app locally.

1. Create the app

Create a directory with these two files:

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(cmake_demo CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(server src/main.cpp)
src/main.cpp
// A minimal HTTP server with POSIX sockets. No dependencies.
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cstdio>
#include <string>

int main() {
    const int port = 8080;
    int server = socket(AF_INET, SOCK_STREAM, 0);
    if (server < 0) { perror("socket"); return 1; }

    int yes = 1;
    setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

    sockaddr_in addr{};
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);
    if (bind(server, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) { perror("bind"); return 1; }
    if (listen(server, 16) < 0) { perror("listen"); return 1; }
    std::printf("Listening on :%d\n", port);
    std::fflush(stdout);

    const std::string body = "Hello from C++ on LILY\n";
    const std::string response =
        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: " +
        std::to_string(body.size()) + "\r\nConnection: close\r\n\r\n" + body;

    for (;;) {
        int client = accept(server, nullptr, nullptr);
        if (client < 0) continue;
        char buffer[2048];
        (void)read(client, buffer, sizeof(buffer));
        (void)write(client, response.data(), response.size());
        close(client);
    }
}

Build and run it locally:

cmake -B build && cmake --build build
./build/server
curl localhost:8080
Hello from C++ on LILY

2. Push it to GitHub

LILY builds from GitHub, not from your local files.

git init -b main
git add -A && git commit -m "First commit"
gh repo create cmake-demo --private --source=. --push

Any way of creating the GitHub repository works. LILY needs your GitHub account connected to read a private repository.

3. Deploy

Run this in the project directory:

lly deploy

No flags are needed.

lly detects the app, builds it and follows the build until it is live. The output, shortened:

  Repository  acme/cmake-demo @ main (origin)
  Project     cmake-demo (new)
  ✓ Source      1 root detected

  Detected roots
       Root  Framework  Status  Hint
    *  .     cpp        ok      CMakeLists.txt

  ✓ Analyzed
    copperfox      deployed   3m10s

  Apps
    App        Phase     URL                             Since  Note
    copperfox  deployed  https://copperfox.lilylabs.app  in 0s

Start a new project, pick the repository and follow the steps in Deploy an app. LILY lists the app with the framework cpp.

What LILY detected

SettingValueFrom
FrameworkcppCMakeLists.txt
Port8080/httphtons(port) in src/main.cpp
Public accessYesAn app with a detected port is public
Environment variablesNoneThe app reads none

Check that it worked

curl https://copperfox.lilylabs.app
Hello from C++ on LILY

Use the address from the summary in place of copperfox.lilylabs.app.

Known limits

None found with this app. It deployed and answered without changes.

Use an agent

Deploy this CMake project to LILY with the lly CLI.
Check that the server listens on a fixed port and that the branch is pushed to GitHub.
Run `lly deploy -y --json` from the project root. When it is live, request the URL and show me the response.
If it exits with code 7, show me the build log with `lly attach <slug> --build`.

Next steps

On this page