cmake install_error ai_generated true

CMake Error: install(EXPORT) generated target "mylib" but target "mylib" has library output path "/build/lib" that does not match the install destination "/usr/local/lib"

ID: cmake/export-library-path-mismatch

Also available as: JSON · Markdown · 中文
85%Fix Rate
90%Confidence
1Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
cmake 3.26.0 active
cmake 3.27.0 active
cmake 3.28.0 active

Root Cause

The target's LIBRARY_OUTPUT_DIRECTORY or ARCHIVE_OUTPUT_DIRECTORY differs from the DESTINATION specified in install(TARGETS), causing CMake to detect a mismatch during export generation.

generic

中文

目标的 LIBRARY_OUTPUT_DIRECTORY 或 ARCHIVE_OUTPUT_DIRECTORY 与 install(TARGETS) 中指定的 DESTINATION 不同,导致 CMake 在生成导出时检测到不匹配。

Official Documentation

https://cmake.org/cmake/help/latest/command/install.html#exporting-targets

Workarounds

  1. 90% success Set the target's output directory to match the install destination using generator expressions: set_target_properties(mylib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib" ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib" ) Then ensure install(TARGETS mylib DESTINATION lib) matches.
    Set the target's output directory to match the install destination using generator expressions:
    
    set_target_properties(mylib PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib"
      ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib"
    )
    
    Then ensure install(TARGETS mylib DESTINATION lib) matches.
  2. 85% success Use a relative path in install(DESTINATION) that matches the build output: set_target_properties(mylib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") install(TARGETS mylib DESTINATION "${CMAKE_BINARY_DIR}/lib") Then generate the export with install(EXPORT ... DESTINATION "${CMAKE_BINARY_DIR}/lib")
    Use a relative path in install(DESTINATION) that matches the build output:
    
    set_target_properties(mylib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
    install(TARGETS mylib DESTINATION "${CMAKE_BINARY_DIR}/lib")
    
    Then generate the export with install(EXPORT ... DESTINATION "${CMAKE_BINARY_DIR}/lib")

中文步骤

  1. 使用生成器表达式将目标的输出目录设置为与安装目标匹配:
    
    set_target_properties(mylib PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib"
      ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib"
    )
    
    然后确保 install(TARGETS mylib DESTINATION lib) 匹配。
  2. 使用与构建输出匹配的 install(DESTINATION) 相对路径:
    
    set_target_properties(mylib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
    install(TARGETS mylib DESTINATION "${CMAKE_BINARY_DIR}/lib")
    
    然后使用 install(EXPORT ... DESTINATION "${CMAKE_BINARY_DIR}/lib") 生成导出。

Dead Ends

Common approaches that don't work:

  1. Change LIBRARY_OUTPUT_DIRECTORY to match the install destination 60% fail

    LIBRARY_OUTPUT_DIRECTORY affects build-time output, not install; changing it may break build scripts or tests that expect the library in the original build directory.

  2. Remove install(EXPORT) entirely 90% fail

    Removes the export mechanism, so downstream projects using find_package will fail to locate the package.