|
| 1 | +# Building libmseed with CMake |
| 2 | + |
| 3 | +This document describes how to build and install libmseed using CMake. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +### Building the library |
| 8 | + |
| 9 | +```bash |
| 10 | +# Configure the build |
| 11 | +cmake -B build -S . |
| 12 | + |
| 13 | +# Build the library |
| 14 | +cmake --build build |
| 15 | + |
| 16 | +# Install the library |
| 17 | +cmake --install build |
| 18 | +``` |
| 19 | + |
| 20 | +### Using in another CMake project |
| 21 | + |
| 22 | +After installing libmseed, you can use it in your CMake project: |
| 23 | + |
| 24 | +```cmake |
| 25 | +# Find the package |
| 26 | +find_package(libmseed REQUIRED) |
| 27 | +
|
| 28 | +# Link against your target |
| 29 | +add_executable(myapp main.c) |
| 30 | +target_link_libraries(myapp PRIVATE mseed::mseed) |
| 31 | +``` |
| 32 | + |
| 33 | +## Build Options |
| 34 | + |
| 35 | +The following options can be configured during the CMake configuration step: |
| 36 | + |
| 37 | +| Option | Default | Description | |
| 38 | +|--------|---------|-------------| |
| 39 | +| `BUILD_SHARED_LIBS` | `ON` | Build shared libraries | |
| 40 | +| `BUILD_STATIC_LIBS` | `ON` | Build static libraries | |
| 41 | +| `BUILD_EXAMPLES` | `OFF` | Build example programs | |
| 42 | +| `BUILD_TESTS` | `OFF` | Build test suite | |
| 43 | +| `LIBMSEED_URL` | `OFF` | Enable URL support via libcurl | |
| 44 | + |
| 45 | +### Examples |
| 46 | + |
| 47 | +Build only static library: |
| 48 | +```bash |
| 49 | +cmake -B build -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON |
| 50 | +``` |
| 51 | + |
| 52 | +Build with examples and tests: |
| 53 | +```bash |
| 54 | +cmake -B build -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON |
| 55 | +``` |
| 56 | + |
| 57 | +Enable URL support with libcurl: |
| 58 | +```bash |
| 59 | +cmake -B build -DLIBMSEED_URL=ON |
| 60 | +``` |
| 61 | + |
| 62 | +Custom installation prefix: |
| 63 | +```bash |
| 64 | +cmake -B build -DCMAKE_INSTALL_PREFIX=/opt/libmseed |
| 65 | +cmake --build build |
| 66 | +cmake --install build |
| 67 | +``` |
| 68 | + |
| 69 | +## Installation |
| 70 | + |
| 71 | +The install step will install: |
| 72 | +- Library files (static and/or shared) to `lib/` |
| 73 | +- Header file (`libmseed.h`) to `include/` |
| 74 | +- CMake config files to `lib/cmake/libmseed/` |
| 75 | +- pkg-config file to `lib/pkgconfig/` |
| 76 | +- Documentation files to `share/doc/libmseed/` |
| 77 | +- Example source code to `share/doc/libmseed/examples/` |
| 78 | + |
| 79 | +## Using libmseed in Your Project |
| 80 | + |
| 81 | +### Method 1: CMake find_package (Recommended) |
| 82 | + |
| 83 | +After installing libmseed, you can use it in your CMake project: |
| 84 | + |
| 85 | +```cmake |
| 86 | +cmake_minimum_required(VERSION 3.12) |
| 87 | +project(MyProject) |
| 88 | +
|
| 89 | +# Find libmseed |
| 90 | +find_package(libmseed REQUIRED) |
| 91 | +
|
| 92 | +# Create your executable |
| 93 | +add_executable(myapp main.c) |
| 94 | +
|
| 95 | +# Link against libmseed |
| 96 | +target_link_libraries(myapp PRIVATE mseed::mseed) |
| 97 | +``` |
| 98 | + |
| 99 | +If libmseed is installed in a non-standard location: |
| 100 | +```bash |
| 101 | +cmake -B build -DCMAKE_PREFIX_PATH=/opt/libmseed |
| 102 | +``` |
| 103 | + |
| 104 | +### Method 2: Using as a subdirectory |
| 105 | + |
| 106 | +You can also include libmseed directly in your project: |
| 107 | + |
| 108 | +```cmake |
| 109 | +cmake_minimum_required(VERSION 3.12) |
| 110 | +project(MyProject) |
| 111 | +
|
| 112 | +# Add libmseed as subdirectory |
| 113 | +add_subdirectory(external/libmseed) |
| 114 | +
|
| 115 | +# Create your executable |
| 116 | +add_executable(myapp main.c) |
| 117 | +
|
| 118 | +# Link against libmseed |
| 119 | +target_link_libraries(myapp PRIVATE mseed::mseed) |
| 120 | +``` |
| 121 | + |
| 122 | +### Method 3: pkg-config |
| 123 | + |
| 124 | +If you prefer pkg-config: |
| 125 | + |
| 126 | +```cmake |
| 127 | +find_package(PkgConfig REQUIRED) |
| 128 | +pkg_check_modules(LIBMSEED REQUIRED mseed) |
| 129 | +
|
| 130 | +add_executable(myapp main.c) |
| 131 | +target_link_libraries(myapp PRIVATE ${LIBMSEED_LIBRARIES}) |
| 132 | +target_include_directories(myapp PRIVATE ${LIBMSEED_INCLUDE_DIRS}) |
| 133 | +``` |
| 134 | + |
| 135 | +## Running Tests |
| 136 | + |
| 137 | +If you built with tests enabled: |
| 138 | + |
| 139 | +```bash |
| 140 | +cmake -B build -DBUILD_TESTS=ON |
| 141 | +cmake --build build |
| 142 | +cd build |
| 143 | +ctest |
| 144 | +``` |
| 145 | + |
| 146 | +## Cross-compilation |
| 147 | + |
| 148 | +CMake makes cross-compilation straightforward. Example for cross-compiling to ARM: |
| 149 | + |
| 150 | +```bash |
| 151 | +cmake -B build -DCMAKE_TOOLCHAIN_FILE=/path/to/arm-toolchain.cmake |
| 152 | +cmake --build build |
| 153 | +``` |
| 154 | + |
| 155 | +## Integration with FetchContent |
| 156 | + |
| 157 | +You can also use CMake's FetchContent to automatically download and build libmseed: |
| 158 | + |
| 159 | +```cmake |
| 160 | +include(FetchContent) |
| 161 | +
|
| 162 | +FetchContent_Declare( |
| 163 | + libmseed |
| 164 | + GIT_REPOSITORY https://github.com/EarthScope/libmseed.git |
| 165 | + GIT_TAG v3.1.11 |
| 166 | +) |
| 167 | +
|
| 168 | +FetchContent_MakeAvailable(libmseed) |
| 169 | +
|
| 170 | +add_executable(myapp main.c) |
| 171 | +target_link_libraries(myapp PRIVATE mseed::mseed) |
| 172 | +``` |
| 173 | + |
| 174 | +## Troubleshooting |
| 175 | + |
| 176 | +### Library not found |
| 177 | +If CMake cannot find libmseed, ensure it's installed and either: |
| 178 | +1. Set `CMAKE_PREFIX_PATH` to the installation directory |
| 179 | +2. Set `libmseed_DIR` to the directory containing `libmseedConfig.cmake` |
| 180 | + |
| 181 | +Example: |
| 182 | +```bash |
| 183 | +cmake -B build -Dlibmseed_DIR=/usr/local/lib/cmake/libmseed |
| 184 | +``` |
| 185 | + |
| 186 | +### URL support issues |
| 187 | +If enabling URL support fails, ensure libcurl is installed: |
| 188 | +```bash |
| 189 | +# Ubuntu/Debian |
| 190 | +sudo apt-get install libcurl4-openssl-dev |
| 191 | + |
| 192 | +# macOS |
| 193 | +brew install curl |
| 194 | + |
| 195 | +# Fedora/RHEL |
| 196 | +sudo dnf install libcurl-devel |
| 197 | +``` |
| 198 | + |
| 199 | +## Comparison with Makefile |
| 200 | + |
| 201 | +The CMake build system provides several advantages over the traditional Makefile: |
| 202 | + |
| 203 | +- Better cross-platform support (Windows, macOS, Linux) |
| 204 | +- Easier integration with other CMake projects |
| 205 | +- Automatic dependency detection |
| 206 | +- Support for multiple build systems (Make, Ninja, Visual Studio, Xcode) |
| 207 | +- Better support for IDEs |
| 208 | +- Modern package management (find_package, FetchContent) |
| 209 | + |
| 210 | +Both build systems are maintained and fully supported. |
0 commit comments