Исправил shared сборку

This commit is contained in:
Aleksandr Orefkov 2025-11-15 13:07:52 +03:00
parent 4ddb1ae236
commit 8d8ec95faa
3 changed files with 19 additions and 33 deletions

View File

@ -5,7 +5,7 @@ include(FetchContent)
project(
simstr
VERSION 1.0.0
VERSION 1.2.4
DESCRIPTION "Yet another string library"
HOMEPAGE_URL "https://github.com/orefkov/simstr"
LANGUAGES CXX
@ -24,12 +24,6 @@ endif()
option(SIMSTR_BUILD_TESTS "Построить тесты" ON)
option(SIMSTR_BENCHMARKS "Построить замеры производительности" Off)
# При OFF - строится обычная статическая библиотека.
# При On - simstr собирается как статическая библиотека, которую потом встроят в shared библиотеку,
# и она будет свои функции объявлять как экспортируемые. Потребители же будут собираться с указанием,
# что функции - импортируемые
option(SIMSTR_IN_SHARED "Функции simstr будут экспортироваться или импортироваться" Off)
add_library(simstr_simstr
src/sstring.cpp
src/simple_unicode.cpp
@ -54,12 +48,20 @@ if(EMSCRIPTEN)
endif()
endif(EMSCRIPTEN)
if (NOT CMAKE_CXX_SIMULATE_ID)
if (${CMAKE_BUILD_TYPE} STREQUAL Debug)
if (NOT CMAKE_CXX_SIMULATE_ID)
set (CMAKE_CXX_SIMULATE_ID "None")
endif()
# Для MSVC подключаем natvis файл для красивой отладки
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC OR ${CMAKE_CXX_SIMULATE_ID} STREQUAL MSVC)
target_link_options(simstr_simstr PUBLIC "/natvis:${CMAKE_CURRENT_SOURCE_DIR}/for_debug/simstr.natvis")
endif()
# Для MSVC подключаем natvis файл для красивой отладки
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC OR ${CMAKE_CXX_SIMULATE_ID} STREQUAL MSVC)
add_custom_command(
TARGET simstr_simstr PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/for_debug/simstr.natvis
${CMAKE_CURRENT_BINARY_DIR}/simstr.natvis
)
target_link_options(simstr_simstr PUBLIC "/natvis:simstr.natvis")
endif()
endif()
set_target_properties(
@ -90,18 +92,12 @@ add_simdutf()
target_link_libraries(simstr_simstr PUBLIC simdutf::simdutf)
if(SIMSTR_IN_SHARED)
if(BUILD_SHARED_LIBS)
# Всем объявляем, что мы будем в shared библиотеке
add_compile_definitions(SIMSTR_IN_SHARED)
# Себе объявим, что мы должны экспортировать функции
target_compile_definitions(simstr_simstr PRIVATE SIMSTR_EXPORT)
# Пример построения shared библиотеки, которая включит в себя simstr.
# Как при этом создать экзешник, пользующийся этой dll - пример в tests/CMakeLists.txt
# Нельзя объявить библиотеку без исходников, добавим просто .h файл
add_library(simstr_dll SHARED include/simstr/sstring.h)
# simstr должна войти в simstr_dll целиком
target_link_libraries(simstr_dll PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,simstr::simstr>")
endif(SIMSTR_IN_SHARED)
endif(BUILD_SHARED_LIBS)
if(SIMSTR_BUILD_TESTS)
enable_testing()

View File

@ -1,7 +1,7 @@
# simstr - библиотека строковых объектов и функций
[![CMake on multiple platforms](https://github.com/orefkov/simstr/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/orefkov/simstr/actions/workflows/cmake-multi-platform.yml)
Версия 1.0.
Версия 1.2.4.
В этой библиотеке содержится реализация нескольких видов строковых объектов и различных алгоритмов для работы со строками.

View File

@ -4,17 +4,7 @@
add_executable(testStr test_str.cpp)
if (NOT SIMSTR_IN_SHARED)
target_link_libraries(testStr simstr::simstr GTest::gtest_main)
else()
# Пример создания экзешника, если simstr экспортируются из dll.
# Так как мы подключаем только simstr_dll, и входящие в неё либы не видны
# нужно вручную задать каталоги include и стандарт C++20
target_link_libraries(testStr simstr_dll GTest::gtest_main)
get_target_property(SIMSTR_INCLUDES simstr::simstr INCLUDE_DIRECTORIES)
target_include_directories(testStr PRIVATE ${SIMSTR_INCLUDES})
target_compile_features(testStr PUBLIC cxx_std_20)
endif()
target_link_libraries(testStr simstr::simstr GTest::gtest_main)
add_test(NAME testStr COMMAND testStr)