英文:
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
$<$<CXX_COMPILER_ID>:MSVC>:msvc_only_target>
$<$<PLATFORM_ID:Linux>:linux_only_target>
)
Or, use the if()
command:
target_link_libraries(${TARGET} common_target)
if(WIN32)
target_link_libraries(${TARGET} windows_only_target)
endif()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论