CMake find_package兼容add_subdirectory

阅读量: searchstar 2023-08-25 17:15:50
Categories: Tags:

如果依赖是通过add_subdirectory添加的,那么find_package的时候会报错找不到xxx-config.cmake

实际上通过add_subdirectory添加的话,对应的target已经存在了。所以我们可以直接判断如果target已经存在就跳过find_package:

if (NOT TARGET 依赖1)
	find_package(依赖1 CONFIG REQUIRED)
endif()

参考文献

https://stackoverflow.com/questions/27339329/how-to-check-whether-a-target-has-been-added-or-not

CMake find_package dependency on subproject: https://stackoverflow.com/a/64786025