解决:Target “XXX“ INTERFACE_INCLUDE_DIRECTORIES property contains path

报错:Target “XXX” INTERFCE_INCLUDE_DERECTORIES property contains path:

在某个cmake项目中出现以下报错:

CMake Error in thirdPart/Message/CMakeLists.txt:
	Target "Message" INTERFCE_INCLUDE_DERECTORIES property contains path:
		"/home/code/CMakeLearning/10Install/02output/thirdPart/Message/include"
which is prefixed in the source directory.

cmake项目源码如下:

..........
target_include_directories(Message PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}
)
......
install(TARGETS Message
EXPORT MessageTargets
ARCHIVE DESTINATION ${INSTALL_LIBDIR} COMPONENT lib
RUBTIME DESTINATION ${INSTALL_BINDIR} COMPONET lib
LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT lib
PUBLIC_HEADER_DESTINATION ${INSTALL_INCLUDEDIR}/Message COMPONENT dev
)
install(EXPORT MessageTargets
FILE MessageTargets.cmake
NAMESPACE "Message::"
DESTINATION ${INSTALL_CMAKEDIR}
COMPONENT dev)

原因分析

产生该错误的原因是:target_include_directories命令和第二个install命令产生了冲突。

  • target_include_directories命令其实就是给Message库设置了属性 INTERFCE_INCLUDE_DERECTORIES。即Message库在构建和安装时配置信息中都会包含target_include_directories命令中的路径。

  • 而在第二个install中,MessageTarget为该库导出的对象文件,该对象文件会包含Message的配置相关信息。而再指定该对象文件的安装DESITNATION会和target_include_directories中的设置冲突。而安装该导出文件有不能不指定DESTINATION,当然直接注释第二个install 也行。

解决办法

使用生成器表达式,分开定义构建时和安装时的头文件路径。
代码如下:

target_include_directories(Message PUBLIC
#构建时,包含如下路径
#第一个:源码下的include路径
#第二个: build目录下的include路径
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}>
#安装时包含如下路径,安装位置下的include路径
$<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>
)
......
install(TARGETS Message
EXPORT MessageTargets
ARCHIVE DESTINATION ${INSTALL_LIBDIR} COMPONENT lib
RUBTIME DESTINATION ${INSTALL_BINDIR} COMPONET lib
LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT lib
PUBLIC_HEADER_DESTINATION ${INSTALL_INCLUDEDIR}/Message COMPONENT dev
)
install(EXPORT MessageTargets
FILE MessageTargets.cmake
NAMESPACE "Message::"
DESTINATION ${INSTALL_CMAKEDIR}
COMPONENT dev)

意思猜测是这个意思,具体原因没太深入。

你可能感兴趣的:(CMake笔记,linux,cmake)