From 5d28dd5520c6dd83fd485fb60594bcc55086acd9 Mon Sep 17 00:00:00 2001 From: Narek Galstyan Date: Sun, 4 Feb 2024 04:30:17 +0000 Subject: [PATCH 1/2] Fix lantern compilation with clang compilers --- CMakeLists.txt | 40 +++++++--------------------------------- 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d16170aa3..eab12c05e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,40 +174,14 @@ set_target_properties( # THIRD PARTY LIBRARIES -# needed to make sure cmake does not add libstdc++ to the linker command when an -# external cpp library is added more at` +# link in c++ runtime so our extension can be binary-distributed to environments with no libstdc++ +# N.B. the flag is meaningful for clang++ and g++, but not to clang and gcc, so we need to make sure CXX is set +# as the linker language +target_link_options(lantern PRIVATE -static-libstdc++) +# interesting discussion on all the hoops one has to go through to statically link stdc++ when +# using C as the link language. We used to do that, but it seems there is no reason to # https://cmake-developers.cmake.narkive.com/JnbrDyGT/setting-linker-language-still-adds-lstdc -if(NOT APPLE) - # clang handles static libstdc++ differently than gcc - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - find_library(STATIC_LIBSTDCPP NAMES libstdc++.a PATHS ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES}) - - if(STATIC_LIBSTDCPP) - set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES};${STATIC_LIBSTDCPP}") - endif() - else() - # apples does not understand -static-libstdc++ used in usearch to bundle libstdc++ with the - # created archive. - # so, on apple we dynamically link to the c++ runtime - # todo:: find a way to statically link the c++ runtime on mac - set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "") - set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "") - endif() -endif() - -# Note: -static-libstdc++ is only meaningful for dynamic builds. -# If we ever switch back to statically linking usearch or other third party libs, -# we need to explicitly link against libstdc++.a -if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_link_options(lantern PRIVATE -static-libstdc++) -elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - target_link_options(lantern PRIVATE -static) -endif () -# not sure why, but the above is not enough to force pulling cpp runtime into lantern.so -# the following is also necessary -target_link_libraries(lantern PRIVATE "libstdc++.a") - -set_target_properties(lantern PROPERTIES LINKER_LANGUAGE C) +set_target_properties(lantern PROPERTIES LINKER_LANGUAGE CXX) target_compile_definitions(lantern PRIVATE LANTERN_USE_USEARCH) target_compile_definitions(lantern PRIVATE "USEARCH_USE_SIMSIMD=$") From cc0e532eb52a0628952be3de562356182640879e Mon Sep 17 00:00:00 2001 From: Narek Galstyan Date: Sun, 4 Feb 2024 04:47:28 +0000 Subject: [PATCH 2/2] is linking libstdc++.a still needed? --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index eab12c05e..c34529821 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,8 @@ target_link_options(lantern PRIVATE -static-libstdc++) # using C as the link language. We used to do that, but it seems there is no reason to # https://cmake-developers.cmake.narkive.com/JnbrDyGT/setting-linker-language-still-adds-lstdc set_target_properties(lantern PROPERTIES LINKER_LANGUAGE CXX) +# todo:: why is still needed? +target_link_libraries(lantern PRIVATE "libstdc++.a") target_compile_definitions(lantern PRIVATE LANTERN_USE_USEARCH) target_compile_definitions(lantern PRIVATE "USEARCH_USE_SIMSIMD=$")