* Add initial documentations * Update documentation for Basic Client and add WebSocket section * feat: add a static site generator with multi-language support - Introduced a new Rust-based static site generator in the `docs-gen` directory. - Implemented core functionality for building sites from markdown files, including: - Configuration loading from `config.toml`. - Markdown rendering with frontmatter support. - Navigation generation based on page structure. - Static file copying and output directory management. - Added templates for base layout, pages, and portal. - Created a CSS file for styling and a JavaScript file for interactive features like language selection and theme toggling. - Updated documentation source with new configuration and example pages in English and Japanese. - Added a `justfile` target for building the documentation site. * Add language/theme toggle functionality - Created a new Japanese tour index page at docs/ja/tour/index.html - Implemented navigation links for various sections of the cpp-httplib tutorial - Added a language selector to switch between English and Japanese - Introduced theme toggle functionality to switch between light and dark modes - Added mobile sidebar toggle for better navigation on smaller screens
2.1 KiB
title, order
| title | order |
|---|---|
| Getting Started | 1 |
All you need to get started with cpp-httplib is httplib.h and a C++ compiler. Let's download the file and get a Hello World server running.
Getting httplib.h
You can download it directly from GitHub. Always use the latest version.
curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
Place the downloaded httplib.h in your project directory and you're good to go.
Setting Up Your Compiler
| OS | Development Environment | Setup |
|---|---|---|
| macOS | Apple Clang | Xcode Command Line Tools (xcode-select --install) |
| Ubuntu | clang++ or g++ | apt install clang or apt install g++ |
| Windows | MSVC | Visual Studio 2022 or later (install with C++ components) |
Hello World Server
Save the following code as server.cpp.
#include "httplib.h"
int main() {
httplib::Server svr;
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("Hello, World!", "text/plain");
});
svr.listen("0.0.0.0", 8080);
}
In just a few lines, you have a server that responds to HTTP requests.
Compiling and Running
The sample code in this tutorial is written in C++17 for cleaner, more concise code. cpp-httplib itself can compile with C++11 as well.
# macOS
clang++ -std=c++17 -o server server.cpp
# Linux
# `-pthread`: cpp-httplib uses threads internally
clang++ -std=c++17 -pthread -o server server.cpp
# Windows (Developer Command Prompt)
# `/EHsc`: Enable C++ exception handling
cl /EHsc /std:c++17 server.cpp
Once it compiles, run it.
# macOS / Linux
./server
# Windows
server.exe
Open http://localhost:8080 in your browser. If you see "Hello, World!", you're all set.
You can also verify with curl.
curl http://localhost:8080/
# Hello, World!
To stop the server, press Ctrl+C in your terminal.
Next Steps
Now you know the basics of running a server. Next, let's look at the client side. cpp-httplib also comes with HTTP client functionality.
Next: Basic Client