Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jc2018 opened this issue Sep 12, 2024 · 2 comments

Comments

@jc2018
Copy link

jc2018 commented Sep 12, 2024

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
Copy link
Owner

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
Copy link
Author

jc2018 commented Sep 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants