zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.71k stars 2.36k forks source link

CMAKE optimization build flags on MSVC #4210

Open phlptp opened 3 years ago

phlptp commented 3 years ago

Please use this template for reporting suspected bugs or requests for help.

Issue description

On MSVC this warning is generated

Warning LNK4075 ignoring '/INCREMENTAL' due to '/LTCG' specification    libzmq  

This does not affect compilation but not having the warning would be good.

This issue is that in CMAKE on multi-configuration builds specifically on MSVC this bit of code does not skip for the debug configuration which is why the warning is generated.

  # 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()

so we get conflicting flags in the debug configuration

the most appropriate fix would likely be

  # Optimization flags. http://msdn.microsoft.com/en-us/magazine/cc301698.aspx
  if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
    set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} /LTCG")

   set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
    set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
    set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
    set(CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL "${CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL} /LTCG")
  endif()

when tested on my system with visual studio 2019 this resolves the warning

Environment

What's the actual result? (include assertion message & call stack if applicable)

Warning LNK4075 ignoring '/INCREMENTAL' due to '/LTCG' specification    libzmq  

What's the expected result?

No warning

phlptp commented 3 years ago

I do not have permission from my organization to submit PR's to the libzmq github, otherwise I would make a pr with the fix.

ghost commented 2 years ago

CMake will override some of your settings and add /INCREMENTAL into the CMakeCache.txt file. The user is expected to override some of these compiler and linker flags either with cmake options at command prompt or in the CMakeCache.txt.

You can look within CMakeCache.txt to see this happening.