我们的CMakeList.txt有一个target_include_directories爬行。它打破了我们的底层客户,如Ubuntu,CentOS和Solaris。
我知道我可以用以下方法来保护它(谢谢ZW),但是我需要一些CmakeVersion2.8.11和更早版本( ???)的东西。
cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
...
if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
target_include_directories(...)
else()
???
endif()我们的目录结构相当简单。有一个目录中有头文件,而同一个目录中有源文件。这是设计的,这样用户就不会在调试器下遇到麻烦。
考虑到我们的配置(如果是@steveire答复适用于我们,我不清楚),是否可以使用以下内容:
if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
target_include_directories(...)
else()
include_directories("${ROOT_SOURCE_DIR}")
endif()如果不是,对于2.8.11和更早版本,应该使用什么来代替target_include_directories?
发布于 2016-06-20 08:00:32
问题
与target_include_directories()命令相比,include_directories()命令扩展了功能。因此,我同意@Tsyvarev的观点,这主要取决于您如何使用target_include_directories()。
溶液
如果使用target_include_directories(my_target PRIVATE ...),则等效为set_property(TARGET my_target APPEND PROPERTY INCLUDE_DIRECTORIES ...)。但是与之相反的是,include_directories()为当前CMakeLists.txt中的所有目标以及所有的add_subdirectory()兄弟节点设置了这些目录。
如果您使用target_include_directories(my_target INTERFACE/PUBLIC ...),那么在2.8.11版本之前的CMake中没有这样的功能。您可以模拟自传播包含目录的行为,但这要复杂得多。
Recommendation
如果仍然希望支持较早版本的CMake,请不要使用新命令。因为这不仅仅是target_include_directories() (参见CMake版本兼容性矩阵/命令)。
参考资料
向后兼容的target_include_directories()
下面的代码显示了较早的CMake版本需要做什么来模拟target_include_directories()
function(target_include_directories _target)
set_property(GLOBAL APPEND PROPERTY GLOBAL_TARGETS "${_target}")
set(_mode "PRIVATE")
foreach(_arg ${ARGN})
if (_arg MATCHES "SYSTEM|BEFORE")
message(FATAL_ERROR "target_include_directories: SYSTEM or BEFORE not supported")
endif()
if (_arg MATCHES "INTERFACE|PUBLIC|PRIVATE")
set(_mode "${_arg}")
else()
get_filename_component(_inc_dir "${_arg}" ABSOLUTE)
if (_mode MATCHES "PUBLIC|PRIVATE")
set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES "${_inc_dir}")
endif()
if (_mode MATCHES "INTERFACE|PUBLIC")
set_property(TARGET ${_target} APPEND PROPERTY MY_INTERFACE_INCLUDE_DIRECTORIES "${_inc_dir}")
endif()
endif()
endforeach()
endfunction(target_include_directories)
function(target_link_libraries _target)
set_property(GLOBAL APPEND PROPERTY GLOBAL_TARGETS "${_target}")
set(_mode "PUBLIC")
foreach(_arg ${ARGN})
if (_arg MATCHES "INTERFACE|PUBLIC|PRIVATE|LINK_PRIVATE|LINK_PUBLIC|LINK_INTERFACE_LIBRARIES")
set(_mode "${_arg}")
else()
if (NOT _arg MATCHES "debug|optimized|general")
set_property(TARGET ${_target} APPEND PROPERTY MY_LINK_LIBARIES "${_arg}")
endif()
endif()
endforeach()
_target_link_libraries(${_target} ${ARGN})
endfunction(target_link_libraries)
function(my_update_depending_inc_dirs _targets _target _dep_target)
get_property(_libs TARGET ${_dep_target} PROPERTY MY_LINK_LIBARIES)
if (NOT _libs)
return()
endif()
foreach(_lib ${_libs})
list(FIND _targets "${_lib}" _idx)
if (NOT _idx EQUAL -1)
get_property(_inc_dirs TARGET ${_lib} PROPERTY MY_INTERFACE_INCLUDE_DIRECTORIES)
set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES "${_inc_dirs}")
# to prevent cyclic dependencies getting us into an endless loop
# remove the target we already processed from the list
list(REMOVE_AT _targets ${_idx})
my_update_depending_inc_dirs("${_targets}" "${_target}" "${_lib}")
endif()
endforeach()
endfunction(my_update_depending_inc_dirs)
function(my_update_inc_dirs)
get_property(_targets GLOBAL PROPERTY GLOBAL_TARGETS)
list(REMOVE_DUPLICATES _targets)
foreach(_target ${_targets})
my_update_depending_inc_dirs("${_targets}" "${_target}" "${_target}")
endforeach()
endfunction(my_update_inc_dirs)使用以下代码进行测试(注意:需要在最后调用my_update_inc_dirs() ):
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(TargetIncludeDirectories)
...
add_library(PLib plib/source/plib.cpp)
target_include_directories(PLib PRIVATE plib/source PUBLIC plib/include)
add_library(Lib lib/source/lib.cpp)
target_include_directories(Lib PRIVATE lib/source PUBLIC lib/include)
target_link_libraries(Lib PRIVATE PLib)
add_executable(App main.cpp)
target_include_directories(App PRIVATE . PUBLIC include)
target_link_libraries(App PRIVATE Lib)
my_update_inc_dirs()https://stackoverflow.com/questions/37900332
复制相似问题