0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-26 23:01:04 +08:00

Problem: (Windows) /GL /LTCG breaks builds when the compiler/linker of static library differs from compiler/linker of executable

Solution: Enable /GL and /LTCG if and only if building as a Release (i.e. non-DEBUG) DLL.

Resolves zeromq/libzmq#4287
This commit is contained in:
Michael Ngarimu 2021-10-27 22:07:50 -07:00
parent 36d6b5d923
commit 7c625afcc0

View File

@ -891,13 +891,6 @@ if(MSVC)
string(REGEX REPLACE "/Z.[^:]" "/Z7 " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") string(REGEX REPLACE "/Z.[^:]" "/Z7 " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REGEX REPLACE "/Z.[^:]" "/Z7 " CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") string(REGEX REPLACE "/Z.[^:]" "/Z7 " CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
# Optimization flags. http://msdn.microsoft.com/en-us/magazine/cc301698.aspx
if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /LTCG")
endif()
endif() endif()
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -1319,6 +1312,27 @@ if(MSVC)
VS_GLOBAL_RunCodeAnalysis true) VS_GLOBAL_RunCodeAnalysis true)
endfunction() endfunction()
if(BUILD_SHARED) if(BUILD_SHARED)
# Whole Program Optimization flags. http://msdn.microsoft.com/en-us/magazine/cc301698.aspx
#
# "Finally, there's the subject of libraries. It's possible to create .LIB
# files with code in its IL form. The linker will happily work with these
# .LIB files. Be aware that these libraries will be tied to a specific
# version of the compiler and linker. If you distribute these libraries,
# you'll need to update them if Microsoft changes the format of IL in a
# future release."
#
# /GL and /LTCG can cause problems when libraries built with different
# versions of compiler are later linked into an executable while /LTCG is active.
# https://social.msdn.microsoft.com/Forums/vstudio/en-US/5c102025-c254-4f02-9a51-c775c6cc9f4b/problem-with-ltcg-when-building-a-static-library-in-vs2005?forum=vcgeneral
#
# For this reason, enable only when building a "Release" (e.g. non-DEBUG) DLL.
if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /LTCG")
endif()
add_library(libzmq SHARED ${sources} ${public_headers} ${html-docs} ${readme-docs} add_library(libzmq SHARED ${sources} ${public_headers} ${html-docs} ${readme-docs}
${CMAKE_CURRENT_BINARY_DIR}/NSIS.template.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc) ${CMAKE_CURRENT_BINARY_DIR}/NSIS.template.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
if(ENABLE_ANALYSIS) if(ENABLE_ANALYSIS)