如何根据操作系统在CMake中包含特定的库?

huangapple go评论86阅读模式
英文:

How do you include specific libraries based on the OS in CMake

问题

I want this list to contain all of the libraries that currently reside in it, while also adding the windows specific library if we are building on a windows system. How do I go about doing this?

我希望这个列表包含当前存在的所有库,同时如果我们在Windows系统上构建,还要添加Windows特定的库。我该如何做到这一点?

英文:

I am trying to modify a project that was initially only for Linux systems to compile and run on Windows. Syntactically, my code is correct. I am using CMake to make my build, and thus have a CMakeLists.txt file. There is a library I only want to link on Windows, but I am not sure of how to do that.

TARGET_LINK_LIBRARIES(
   LIB1
   LIB2
   LIB3
)

I want this list to contain all of the libraries that currently reside in it, while also adding the windows specific library if we are building on a windows system. How do I go about doing this?

答案1

得分: 2

你可以使用生成器表达式来内联执行此操作:

target_link_libraries(${TARGET}
    common_target
    $<${CXX_COMPILER_ID}:MSVC>:msvc_only_target>
    $<${PLATFORM_ID}:Linux>:linux_only_target>
)

或者使用if()命令:

target_link_libraries(${TARGET} common_target)
if(WIN32)
    target_link_libraries(${TARGET} windows_only_target)
endif()
英文:

You can use generator expressions to do this inline:

target_link_libraries(${TARGET}
    common_target
    $&lt;$&lt;CXX_COMPILER_ID&gt;:MSVC&gt;:msvc_only_target&gt;
    $&lt;$&lt;PLATFORM_ID:Linux&gt;:linux_only_target&gt;
)

Or, use the if() command:

target_link_libraries(${TARGET} common_target)
if(WIN32)
    target_link_libraries(${TARGET} windows_only_target)
endif()

huangapple
  • 本文由 发表于 2023年6月8日 21:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定