From eeda7f1a134bbabb8dfae1c6a4a74585906ae00e Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com> Date: Tue, 23 Feb 2021 09:38:24 -0800 Subject: [PATCH] Get static/dynamic CRT link options in sync (#1715) * Get static/dynamic CRT link options in sync * Update docs * PR feedback * en-us * Another 'en-us' in a link * MSVC_USE_STATIC_CRT * Update README.md Co-authored-by: Anton Kolesnyk --- CMakeLists.txt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 5 +++++ README.md | 13 +++++++++++++ 3 files changed, 64 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d284cfa8d..11e2c2bd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ option(BUILD_DOCUMENTATION "Create HTML based API documentation (requires Doxyge option(RUN_LONG_UNIT_TESTS "Tests that takes more than 5 minutes to complete. No effect if BUILD_TESTING is OFF" OFF) option(BUILD_STORAGE_SAMPLES "Build sample application for Azure Storage clients" OFF) option(BUILD_PERFORMANCE_TESTS "Build the performance test library" OFF) +option(MSVC_USE_STATIC_CRT "(MSVC only) Set to ON to link SDK with static CRT (/MT or /MTd switch)." OFF) include(AzureTransportAdapters) include(AzureVcpkg) @@ -29,6 +30,51 @@ project(azure-sdk LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED True) +if(MSVC_USE_STATIC_CRT AND MSVC) + # 1. More about static/shared CRT: + # https://docs.microsoft.com/cpp/build/reference/md-mt-ld-use-run-time-library?view=msvc-160 + # + # 2. MSVC_USE_STATIC_CRT build flag approach is used/inspired by libcurl + # (https://github.com/curl/curl/blob/master/CMakeLists.txt) and some other projects. + # + # 3. GTest would emit the following warning: + # warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library + # AddGoogleTest.cmake uses gtest_force_shared_crt + # (see https://github.com/google/googletest/blob/master/googletest/README.md), + # which respects linker settings that we set below, and our settings below are all in sync. + # + # 4. Sometimes, the following approach is recommended instead: + # +-----------------------------------------------------------------------------------+ + # | # Use the static runtime libraries when building statically | + # | # for consistency with vcpkg's arch-windows-static triplets: | + # | cmake_policy(SET CMP0091 NEW) | + # | # see https://cmake.org/cmake/help/v3.15/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html | + # | if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) | + # | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") | + # | endif() | + # +-----------------------------------------------------------------------------------+ + # However, it only works when cmake installed is 3.15+; + # we have to require a minimum of 3.13. + # + # 5. We "replace with empty string" (i.e. remove) first, then add, so that '/MT' + # will be present (and present once) even if '/MD' was not. + + string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT") + + string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") + + string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT") + + string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") + set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT") + + string(REGEX REPLACE "/MDd" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") +endif() + if(BUILD_TESTING) include(AddGoogleTest) enable_testing () diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fe7246b1f..edbfc7084 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -152,6 +152,11 @@ The following CMake options are available for adding/removing project features. Build Doxygen documentation OFF + +MSVC_USE_STATIC_CRT +On MSVC, link SDK with static CRT (use `/MT` or `/MTd` switch) +OFF + #### Testing the project diff --git a/README.md b/README.md index 79056f4e7..bf181bfb0 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,19 @@ For a complete list of available packages, please see the [latest available pack Last stable versions of packages that are production-ready. These libraries provide similar functionalities to the beta packages, as they allow you to use and consume existing resources and interact with them, for example: upload a storage blob. They might not implement the [guidelines](https://azure.github.io/azure-sdk/cpp_introduction.html) or have the same feature set. They do however offer wider coverage of services. +### VcPkg + +Released SDK library versions are available on [VcPkg](https://github.com/microsoft/vcpkg): +* `azure-core-cpp` +* `azure-identity-cpp` +* `azure-storage-blobs-cpp` +* `azure-storage-common-cpp` +* `azure-storage-files-datalake-cpp` +* `azure-storage-files-shares-cpp` + +> NOTE: In case of getting linker errors when consuming the SDK on Windows, make sure that [VcPkg trilet](https://vcpkg.readthedocs.io/en/latest/users/triplets/) being consumed matches the [CRT link flags](https://docs.microsoft.com/cpp/build/reference/md-mt-ld-use-run-time-library?view=msvc-160) being set for your app or library build. See also `MSVC_USE_STATIC_CRT` build flag. + + ## Need help - For reference documentation visit the [Azure SDK for C++ documentation](https://azure.github.io/azure-sdk-for-cpp).