ToruNiina / toml11

TOML for Modern C++
https://toruniina.github.io/toml11/
MIT License
1.04k stars 160 forks source link

Error in CMakeLists.txt when Building on Windows with Visual Studio 2022-v140-Base Configuration #274

Open jc2018 opened 1 month ago

jc2018 commented 1 month ago

When I build my project on CentOS 7.9 (using gcc version 4.8.5), it builds successfully. However, when attempting to build the project on Windows with the following configuration:

{
    "name": "VS2022-v140-Base",
    "inherits": "base",
    "generator": "Visual Studio 17 2022",
    "architecture": "x64",
    "toolset": "v140"
}

I encountered an error in the CMakeLists.txt file. The specific error message is:

  CMake Error at winbuild/_deps/toml11-src/src/CMakeLists.txt:140 (target_compile_definitions):
  target_compile_definitions may only set INTERFACE properties on INTERFACE

I believe there are issues in the CMakeLists.txt file that need to be addressed for successful building on Windows with the specified configuration.

ToruNiina commented 1 month ago

thank you for sharing, i will look into this later, when i have time(these days im really busy, so it takes time i think).

but actually, msvc17 fails to compile with some features of toml11, so i recommend to use the latest one or msvc19.

jc2018 commented 1 month ago

As a solution, I packaged toml11-headeronly-4.2.0.zip and exported toml11-config.cmake as follows:

get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

add_library(toml11 INTERFACE)
target_include_directories(toml11 INTERFACE
            INTERFACE_INCLUDE_DIRECTORIES  "${SELF_DIR}/include"
    )

# required options to use toml11 with MSVC
if(MSVC)
    target_compile_options(toml11 INTERFACE "/utf-8")
    if(MSVC_VERSION LESS 1910)
        message(STATUS "MSVC < 1910. DEFINE_CONVERSION_NON_INTRUSIVE is disabled")
        target_compile_definitions(toml11 INTERFACE -DTOML11_WITHOUT_DEFINE_NON_INTRUSIVE)
    elseif(MSVC_VERSION LESS 1920) # MSVC 2017
        target_compile_options(toml11 INTERFACE "/experimental:preprocessor")
    else() # MSVC 2019
        target_compile_options(toml11 INTERFACE "/Zc:preprocessor")
    endif()
endif()

The platform compiles successfully. My way of calling it:

CPMAddPackage("http://url/artifacts/library/toml11/toml11-headeronly-4.2.0.zip")

As shown in my example, you should only need to make the necessary modifications as prompted ("target_compile_definitions may only set INTERFACE properties on INTERFACE"). Your library is excellent, thank you for your contribution. Regarding the issue of MSVC compilation failure, based on my experience, I recommend using VS2022 and installing toolchains for 2015, 2017, and 2019. You can test them one by one. I highly recommend using CMakePresets.json for platform-specific compilation settings. The initial JSON string is also part of my project's CMakePresets.json configuration. Once configured, you can achieve cross-platform packaging with a single command. For Linux, use cmake --workflow --preset Centos7Project, and for Windows, use cmake --workflow --preset VS2022-v140-Project.