eclipse-paho / paho.mqtt.cpp

Other
1.03k stars 439 forks source link

MSVC Runtime Library Selection #349

Open lmapii opened 3 years ago

lmapii commented 3 years ago

Hi,

I am working on the integration of paho.mqtt.cpp and thus also paho.mqtt.c in a CMAKE project where the libraries are not installed locally but built within the workspace of the project (config time). The resulting installation directories are simply added to the CMAKE_PREFIX_PATH within the root CMakeLists.txt file.

list(APPEND CMAKE_PREFIX_PATH
    "${paho_c_binary_dir}"
    "${paho_cpp_binary_dir}"
)

This is working nicely under Linux, but under Windows I'm having troubles with the runtime selection: I am building both libraries using an execute_process call which invokes CMAKE within the corresponding repository (e.g., this repository). However, such a call does not seem to pass on the /MD or /MT` selection that can be performed, e.g., using the below instruction, resulting in the paho libs always being built for the dynamically linked runtime lib.

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

I am also using Google's protobuffer, where a compile-time flag is available to select the runtime. The same concept could be applied to the CMakeLists.txt of this repository (and a matching update for the paho.mqtt.c lib), e.g., using the following:

option(PAHO_MSVC_STATIC_RUNTIME "Link static runtime libraries" TRUE)
if(PAHO_BUILD_STATIC)
    if(MSVC AND PAHO_MSVC_STATIC_RUNTIME)
        message(STATUS "Linking static runtime libraries")
        foreach(flag_var
            CMAKE_CXX_FLAGS
            CMAKE_CXX_FLAGS_DEBUG
            CMAKE_CXX_FLAGS_RELEASE
            CMAKE_CXX_FLAGS_MINSIZEREL
            CMAKE_CXX_FLAGS_RELWITHDEBINFO)
            if(${flag_var} MATCHES "/MD")
                string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
            endif(${flag_var} MATCHES "/MD")
        endforeach(flag_var)
    endif()
endif()

Then any project could also select the runtime lib using a flag. This is working nicely with patched CMakeLists.txt. I just wanted to ask if this is of any interest then I'd open a PR.

gbronner commented 9 months ago

I would love this.