使用 CMake 构建开源项目时(在我的例子中,它是柠檬图形库),当我尝试通过以下方式构建共享库时出现此错误-DBUILD_SHARED_LIBS=1
:
TARGETS given no LIBRARY DESTINATION for shared library target
这个错误来自哪里,我该如何解决?
使用 CMake 构建开源项目时(在我的例子中,它是柠檬图形库),当我尝试通过以下方式构建共享库时出现此错误-DBUILD_SHARED_LIBS=1
:
TARGETS given no LIBRARY DESTINATION for shared library target
这个错误来自哪里,我该如何解决?
在我的CMakeLists.txt
中,我的 INSTALL 命令没有 LIBRARY 参数。
从此改变:
INSTALL(
TARGETS lemon
ARCHIVE DESTINATION lib
COMPONENT library
)
对此:
INSTALL(
TARGETS lemon
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib # <-- Add this line
COMPONENT library
)
解决了我的问题。
我明白了...发生这种情况的另一个原因是当您创建共享库时
add_library(${NAME} 共享资源)
然后当 Cmake 到达 Windows 平台上的安装命令时,它会抱怨这些错误,解决方案是使用 RUNTIME 而不是 LIBRARY,比如
if(WIN32)
install(TARGETS ${NAME}
RUNTIME DESTINATION path)
else()
install(TARGETS ${NAME}
LIBRARY DESTINATION path)
endif()
之后DESTINATION
,它应该有bin
, lib
, include
。
安装lib
或bin
install(TARGETS snappy
EXPORT SnappyTargets
# RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # DESTINATION error
RUNTIME DESTINATION bin ${CMAKE_INSTALL_BINDIR} # should add bin or other dir
LIBRARY DESTINATION lib ${CMAKE_INSTALL_LIBDIR}
# ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR # DESTINATION error
ARCHIVE DESTINATION lib ${CMAKE_INSTALL_LIBDIR} # should add lib
)
例如安装.h
文件:
install(
FILES
"${PROJECT_SOURCE_DIR}/test_hard1.h"
"${PROJECT_BINARY_DIR}/config.h"
# DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # error install FILES given no DESTINATION!
# add include after DESTINATION, then it works
DESTINATION include ${CMAKE_INSTALL_INCLUDEDIR}
)
有关更多详细信息,请参见https://cmake.org/cmake/help/v3.0/command/install.html :
install(TARGETS myExe mySharedLib myStaticLib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
install(TARGETS mySharedLib DESTINATION /some/full/path)
我刚刚遇到了类似的问题。正如@Fernando 在他的回答中所说,您可以采用该解决方案。另一个类似的解决方案是include(GNUInstallDirs)
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
EXPORT "${PROJECT_NAME}Config"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)