39

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?

Artem Zankovich
  • 2,319
  • 20
  • 36
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • 5
    I got the same problem and fixed it just by adding the [`include(GNUInstallDirs)`](https://github.com/risoflora/libsagui/blob/master/CMakeLists.txt#L73). – silvioprog Sep 01 '18 at 04:05

6 Answers6

43

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.

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
17

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()  
  • 1
    Are the two usages of `path` in your example different? If not you can achieve the same effect with `install(TARGETS ${NAME} RUNTIME DESTINATION path LIBRARY DESTINATION path )` – Unapiedra Oct 26 '16 at 23:30
5

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)
Jayhello
  • 5,931
  • 3
  • 49
  • 56
1

I just faced a similar problem. As @Fernando said in his answer, you can go with that solution. Another and similar solution is to just 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}
)
arsdever
  • 1,111
  • 1
  • 11
  • 32
1

I had a similar, but not identical error message:

CMake Error at xxx/CMakeLists.txt:123 (install):
  install Library TARGETS given no DESTINATION!

The fix in this case was to simply update CMake; from version 3.13.3 to 3.25.1 or really any newer version.

Note: I had originally posted my own question at CMake Error: install Library TARGETS given no DESTINATION but it was closed as a duplicate (although the error message was different), so I posted my own answer here.

I do not know if this would fix the TARGETS given no LIBRARY DESTINATION for shared library target error.

The other answers all talk about modifying the CMake configuration files (i.e. CMakeLists.txt), so this answer is new - and easier.

Our fix was just to update, and leave the configuration alone. As the error was thrown from another open source project, this was by far the better fix, for us.

J. Gwinner
  • 931
  • 10
  • 15
0

Since CMake 3.14 the option DESTINATION is no longer required for executables, shared/static libraries and some other artifacts: CMake provides reasonable defaults for their installation directories. Alternatively, one could include module GNUInstallDirs, and set corresponding CMAKE_INSTALL_<dir> variable.

For regular executables, static libraries and shared libraries, the DESTINATION argument is not required. For these target types, when DESTINATION is omitted, a default destination will be taken from the appropriate variable from GNUInstallDirs, or set to a built-in default value if that variable is not defined. The same is true for the public and private headers associated with the installed targets through the PUBLIC_HEADER and PRIVATE_HEADER target properties.

https://cmake.org/cmake/help/v3.14/command/install.html#targets

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153