Installing
libwebsockets (LWS) directly from GitHub uses standard CMake.Step-by-Step Installation (Linux / macOS)
1. Install System Dependencies
LWS requires
cmake, gcc/g++ (or clang), and SSL headers (OpenSSL/WolfSSL).- Ubuntu / Debian:Bash
sudo apt update sudo apt install -y git cmake build-essential libssl-dev zlib1g-dev - Fedora / RHEL / CentOS:Bash
sudo dnf install -y git cmake gcc gcc-c++ openssl-devel zlib-devel - macOS (Homebrew):Bash
brew install git cmake openssl zlib
2. Clone the Official Repository
Clone the repository and check out a stable release tag (recommended over
main for production reliability):Bash
# Clone the repository
git clone https://github.com/warmcat/libwebsockets.git
cd libwebsockets
# (Optional) Checkout a stable release tag
git checkout v4.3-stable
3. Build and Install via CMake
LWS requires an out-of-source build directory:
Bash
# Create and enter build directory
mkdir build && cd build
# Configure build files
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DLWS_WITH_SSL=ON \
-DLWS_WITH_HTTP2=ON
# Compile using all CPU cores
make -j$(nproc)
# Install system-wide
sudo make install
# Refresh shared library link cache (Linux)
sudo ldconfig
Key CMake Options (Customizing Your Build)
You can pass extra flags to
cmake .. depending on your requirements:| Flag | Default | Description |
-DLWS_WITH_SSL=ON | ON | Enables OpenSSL/TLS support. |
-DLWS_WITH_HTTP2=ON | ON | Enables HTTP/2 protocol support. |
-DLWS_WITH_MINIMAL_EXAMPLES=ON | OFF | Builds sample client/server code in the build folder. |
-DLWS_MAX_SMP=8 | 1 / Auto | Sets maximum SMP thread count for multi-threaded service loops. |
-DCMAKE_INSTALL_PREFIX=/usr | /usr/local | Changes the default installation target directory. |
Verifying the Installation
To verify your installation, create a minimal C test file (
test.c):C
#include <libwebsockets.h>
#include <stdio.h>
int main() {
printf("LWS Version: %s\n", lws_get_library_version());
return 0;
}
Compile and run it:
Bash
gcc test.c -o test -lwebsockets
./test
No comments:
Post a Comment
If you have any doubts, please let me know.