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

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

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.

  1. TARGET_LINK_LIBRARIES(
  2. LIB1
  3. LIB2
  4. LIB3
  5. )

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

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

  1. target_link_libraries(${TARGET}
  2. common_target
  3. $<${CXX_COMPILER_ID}:MSVC>:msvc_only_target>
  4. $<${PLATFORM_ID}:Linux>:linux_only_target>
  5. )

或者使用if()命令:

  1. target_link_libraries(${TARGET} common_target)
  2. if(WIN32)
  3. target_link_libraries(${TARGET} windows_only_target)
  4. endif()
英文:

You can use generator expressions to do this inline:

  1. target_link_libraries(${TARGET}
  2. common_target
  3. $&lt;$&lt;CXX_COMPILER_ID&gt;:MSVC&gt;:msvc_only_target&gt;
  4. $&lt;$&lt;PLATFORM_ID:Linux&gt;:linux_only_target&gt;
  5. )

Or, use the if() command:

  1. target_link_libraries(${TARGET} common_target)
  2. if(WIN32)
  3. target_link_libraries(${TARGET} windows_only_target)
  4. 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:

确定