feat: make dynmicly installeble

This commit is contained in:
2026-01-05 21:29:10 +01:00
parent 4245d152fd
commit 1e2150a6a1
2 changed files with 57 additions and 15 deletions

View File

@@ -1,29 +1,65 @@
cmake_minimum_required(VERSION 4.0)
cmake_minimum_required(VERSION 3.16)
project(tstd C)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_library(tstd STATIC library.c
include/tstd/string.h
string.c
list.c
include/tstd/list.h
helper.h)
# Sources (headers are installed, not compiled)
set(TSTD_SOURCES
library.c
string.c
list.c
)
# Shared library: libtstd.so
add_library(tstd SHARED ${TSTD_SOURCES})
target_include_directories(tstd
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set_target_properties(tstd PROPERTIES
OUTPUT_NAME tstd
VERSION 1.0.0
SOVERSION 1
POSITION_INDEPENDENT_CODE ON
)
# Static library: libtstd.a (named tstd_static target, outputs libtstd.a)
add_library(tstd_static STATIC ${TSTD_SOURCES})
target_include_directories(tstd_static
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set_target_properties(tstd_static PROPERTIES
OUTPUT_NAME tstd
POSITION_INDEPENDENT_CODE ON
)
# Tests/executables can choose which one to link
add_executable(test_tstd_string test/test_string.c)
add_executable(test_tstd_list test/test_list.c)
target_link_libraries(test_tstd_string tstd)
target_link_libraries(test_tstd_list tstd)
target_include_directories(tstd
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# Link dynamically by default:
target_link_libraries(test_tstd_string PRIVATE tstd)
target_link_libraries(test_tstd_list PRIVATE tstd)
# If you want static instead, swap to tstd_static.
enable_testing()
add_test(NAME tstd_tests_string COMMAND test_tstd_string)
add_test(NAME tstd_tests_list COMMAND test_tstd_list)
# Install libs + headers
install(TARGETS tstd tstd_static
EXPORT tstdTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION include
)

6
install_dynamic.sh Executable file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
sudo cmake --install build
sudo ldconfig