filipdutescu / modern-cpp-template

A template for modern C++ projects using CMake, Clang-Format, CI, unit testing and more, with support for downstream inclusion.
The Unlicense
1.69k stars 214 forks source link

[Confusion] about ${PROJECT_NAME}_BUILD_SHARED_LIBS #26

Closed Kiddinglife closed 3 years ago

Kiddinglife commented 3 years ago

Describe the confusion
I am confused about the below cmake snippets in StandardSettings.cmake:

# Export all symbols when building a shared library
if(${PROJECT_NAME}_BUILD_SHARED_LIBS)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
    set(CMAKE_CXX_VISIBILITY_PRESET hidden)
    set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
endif()

As cmake tool will build all the libraries as shared library when BUILD_SHARED_LIBS is ON, my understnading is that we should overwrite the cmake built-in cache variable of BUILD_SHARED_LIBS when ${PROJECT_NAME}_BUILD_SHARED_LIBS is ON. Something looks like this:

# Export all symbols when building a shared library
if(${PROJECT_NAME}_BUILD_SHARED_LIBS)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
    set(CMAKE_CXX_VISIBILITY_PRESET hidden)
    set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
    set(BUILD_SHARED_LIBS ON CACHE BOOL "build shared libraries except when explicitely set to static" FORCE)
endif()

Let me know what you think it.

Expected behavior Overwrite the cmake built-in cache variable of BUILD_SHARED_LIBS when ${PROJECT_NAME}_BUILD_SHARED_LIBS is ON

Desktop (please complete the following information):

filipdutescu commented 3 years ago

Honestly, I think I should remove the custom macro and just keep the default one, since there is no reason not to. Good catch!