Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emersonmello/libprg
A minimal C library template using Modern CMake
https://github.com/emersonmello/libprg
c cmake cmake-examples library template
Last synced: about 1 month ago
JSON representation
A minimal C library template using Modern CMake
- Host: GitHub
- URL: https://github.com/emersonmello/libprg
- Owner: emersonmello
- License: mit
- Created: 2023-07-07T19:18:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-24T16:50:23.000Z (about 1 year ago)
- Last Synced: 2023-10-25T13:27:46.782Z (about 1 year ago)
- Topics: c, cmake, cmake-examples, library, template
- Language: CMake
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# A minimal C library template using Modern CMake
An example to demonstrate how to organize files for writing a library in the C language using Modern CMake.
## Project structure
```bash
.
|-- CMakeLists.txt
|-- LICENSE
|-- Readme.md
`-- libprg
|-- CMakeLists.txt
`-- src
|-- include
| `-- libprg
| `-- libprg.h
`-- libprg
|-- libprg.c
|-- sub.c
`-- sum.c
```## How to use this library in a C application using CMake
CMake [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) can download this library directly from his GitHub repository and you must indicate from which branch or tag. In this example I will use the `main` branch or the `0.0.1` tag.
Create a new project (C executable) using CMake and your `CMakeLists.txt` file should look like this:
```cmake
cmake_minimum_required(VERSION 3.21)
project(myapp C)set(CMAKE_C_STANDARD 17)
include(FetchContent)
FetchContent_Declare(
libprg
GIT_REPOSITORY https://github.com/emersonmello/libprg.git
GIT_TAG origin/main
# or GIT_TAG 0.0.1
# You can reference a local directory instead
# URL file:///${CMAKE_CURRENT_SOURCE_DIR}/../libprg
)
FetchContent_MakeAvailable(libprg)add_executable(myapp main.c)
# linking libprg
target_link_libraries(myapp PUBLIC libprg)
```And your `main.c` should look like this:
```c
#include
#includeint main(void) {
int a = 1, b = 2;result_t r = compute(a, b, SUM);
printf("%d + %d = %8.2f", a, b, r.value);return 0;
}
```# References
- https://cliutils.gitlab.io/modern-cmake/
- https://blog.feabhas.com/2021/08/cmake-part-3-source-file-organisation/
- https://iakko.medium.com/creating-a-c-shared-library-with-cmake-50c6d2f97ee8