[GH-ISSUE #177] Don't hardcode msvc runtime library in cmake file #143

Closed
opened 2026-05-05 11:46:31 -06:00 by gitea-mirror · 10 comments
Owner

Originally created by @dirkvdb on GitHub (May 15, 2018).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/177

Originally assigned to: @jmcnamara on GitHub.

When compiling with MSVC the runtime settings are hard coded in the CMakeLists.txt

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd /O0 /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT /Ox /Zi /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT /Zi /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb")

This is bad practice, these settings should be defined in a toolchain file, never hardcoded in the cmake file.

When i want to compile the library using a different runtime for inclusion in my project the only option I have is patching the CMakeLists.txt

Originally created by @dirkvdb on GitHub (May 15, 2018). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/177 Originally assigned to: @jmcnamara on GitHub. When compiling with MSVC the runtime settings are hard coded in the CMakeLists.txt ``` set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd /O0 /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT /Ox /Zi /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb") set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT /Zi /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb") set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT /Fd${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb") ``` This is bad practice, these settings should be defined in a toolchain file, never hardcoded in the cmake file. When i want to compile the library using a different runtime for inclusion in my project the only option I have is patching the CMakeLists.txt
gitea-mirror 2026-05-05 11:46:31 -06:00
Author
Owner

@jmcnamara commented on GitHub (May 15, 2018):

@Alexhuszagh can you have a look at this as well.

<!-- gh-comment-id:389241846 --> @jmcnamara commented on GitHub (May 15, 2018): @Alexhuszagh can you have a look at this as well.
Author
Owner

@jmcnamara commented on GitHub (May 16, 2018):

@dirkvdb If you know how to fix this and can submit a PR then that would be good.

<!-- gh-comment-id:389433974 --> @jmcnamara commented on GitHub (May 16, 2018): @dirkvdb If you know how to fix this and can submit a PR then that would be good.
Author
Owner

@dirkvdb commented on GitHub (May 16, 2018):

I created a pull request, all it does is remove the hardcoded runtime flags.

<!-- gh-comment-id:389440748 --> @dirkvdb commented on GitHub (May 16, 2018): I created a pull request, all it does is remove the hardcoded runtime flags.
Author
Owner

@jmcnamara commented on GitHub (May 16, 2018):

For reference: PR #178

<!-- gh-comment-id:389530578 --> @jmcnamara commented on GitHub (May 16, 2018): For reference: PR #178
Author
Owner

@jmcnamara commented on GitHub (May 16, 2018):

@RalfKubis Any thoughts on this?

<!-- gh-comment-id:389531858 --> @jmcnamara commented on GitHub (May 16, 2018): @RalfKubis Any thoughts on this?
Author
Owner

@Alexhuszagh commented on GitHub (May 17, 2018):

@dirkvdb It is bad practice, but greatly simplifies life for simple cases.
@jmcnamara They're right, and I would likely accept the PR.

I can document toolchain configurations (although manually providing them is likely beyond the scope of this project, although I would gladly do so) inside the CMakeLists as a follow-up PR.

<!-- gh-comment-id:389913004 --> @Alexhuszagh commented on GitHub (May 17, 2018): @dirkvdb It is bad practice, but greatly simplifies life for simple cases. @jmcnamara They're right, and I would likely accept the PR. I can document toolchain configurations (although manually providing them is likely beyond the scope of this project, although I would gladly do so) inside the CMakeLists as a follow-up PR.
Author
Owner

@dirkvdb commented on GitHub (May 17, 2018):

@Alexhuszagh

  • I find it strange that you apply the runtime setting only for static library builds. Building a static library does not imply that you use the static runtime. It is perfectly feasible to build static libraries against the dynamic runtime.
  • The way the setting is now applied is not really clean, you add the runtime compiler option to the already present compiler options, resulting in both the dynamic and static runtime flag to be present in the compiler flags, you are lucky msvc takes the latter one
    It would be better to do:
    string(REPLACE "/MD" "/MT" ${CMAKE_C_FLAGS} "${${CMAKE_C_FLAGS}}")
  • If you want I can change the pull request to add an option like FORCE_STATIC_MSVC_RUNTIME, that way you can get the current behaviour without messing with toolchains.
<!-- gh-comment-id:389953274 --> @dirkvdb commented on GitHub (May 17, 2018): @Alexhuszagh - I find it strange that you apply the runtime setting only for static library builds. Building a static library does not imply that you use the static runtime. It is perfectly feasible to build static libraries against the dynamic runtime. - The way the setting is now applied is not really clean, you add the runtime compiler option to the already present compiler options, resulting in both the dynamic and static runtime flag to be present in the compiler flags, you are lucky msvc takes the latter one It would be better to do: `string(REPLACE "/MD" "/MT" ${CMAKE_C_FLAGS} "${${CMAKE_C_FLAGS}}")` - If you want I can change the pull request to add an option like `FORCE_STATIC_MSVC_RUNTIME`, that way you can get the current behaviour without messing with toolchains.
Author
Owner

@Alexhuszagh commented on GitHub (May 17, 2018):

@dirkvdb I entirely agree, also, this is not my code in question. We may want to ask @jimzshi what he thinks, since he authored the PR.

I think we should wait for feedback from him, but I agree this is peculiar, and also not best practice. Options sound like a great way, but this does honestly sound like something for a toolchain.

For reference:
https://github.com/jmcnamara/libxlsxwriter/pull/103

9a78c323a9 (diff-af3b638bc2a3e6c650974192a53c7291)

<!-- gh-comment-id:389987443 --> @Alexhuszagh commented on GitHub (May 17, 2018): @dirkvdb I entirely agree, also, this is not my code in question. We may want to ask @jimzshi what he thinks, since he authored the PR. I think we should wait for feedback from him, but I agree this is peculiar, and also not best practice. Options sound like a great way, but this does honestly sound like something for a toolchain. For reference: https://github.com/jmcnamara/libxlsxwriter/pull/103 https://github.com/jmcnamara/libxlsxwriter/commit/9a78c323a925c75d8feffb7c3f6a7773e79d2628#diff-af3b638bc2a3e6c650974192a53c7291
Author
Owner

@jimzshi commented on GitHub (May 18, 2018):

@Alexhuszagh @dirkvdb

i have no objections removing runtime lib from cmakefile.

imho both approach has its up/down sides in different scenarios. at this stage, i reckon @dirkvdb proposal is better since it provides more flexibility to the lib compilation.

i also think it would be even better if @dirkvdb can at meanwhile provide cmake options that lib users can choose runtime lib as they wish. many thanks in advance.

cheers.

<!-- gh-comment-id:390144580 --> @jimzshi commented on GitHub (May 18, 2018): @Alexhuszagh @dirkvdb i have no objections removing runtime lib from cmakefile. imho both approach has its up/down sides in different scenarios. at this stage, i reckon @dirkvdb proposal is better since it provides more flexibility to the lib compilation. i also think it would be even better if @dirkvdb can at meanwhile provide cmake options that lib users can choose runtime lib as they wish. many thanks in advance. cheers.
Author
Owner

@jmcnamara commented on GitHub (May 21, 2018):

Merged. Thanks.

<!-- gh-comment-id:390638337 --> @jmcnamara commented on GitHub (May 21, 2018): Merged. Thanks.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#143
No description provided.