CMake Error: TARGETS given no LIBRARY DESTINATION

2019-01-23 11:23发布

问题:

When building an opensource project with CMake (in my case, it was the lemon graph library), I got this error when I tried to build shared libaries via -DBUILD_SHARED_LIBS=1:

TARGETS given no LIBRARY DESTINATION for shared library target

Where does this error come from and how do I fix it?

回答1:

In my CMakeLists.txt, my INSTALL command had no LIBRARY parameter.

Changing from this:

INSTALL(
  TARGETS lemon
  ARCHIVE DESTINATION lib
  COMPONENT library
)

to this:

INSTALL(
  TARGETS lemon
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib  # <-- Add this line
  COMPONENT library
)

fixed my problem.



回答2:

I got this... Another reason this happens is when you create a shared library

add_library(${NAME} SHARED sources )

then when Cmake reaches the install command on Windows platform, it complains of these error, solution is to use RUNTIME instead of LIBRARY, like

if(WIN32)
  install(TARGETS ${NAME}
    RUNTIME DESTINATION path)
else()
  install(TARGETS ${NAME}
    LIBRARY DESTINATION path)
endif()  


回答3:

After DESTINATION, it should have bin, lib, include.

install lib or 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
)

For example, install .h file:

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}
)

see https://cmake.org/cmake/help/v3.0/command/install.html for more detail:

install(TARGETS myExe mySharedLib myStaticLib
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib/static)
install(TARGETS mySharedLib DESTINATION /some/full/path)