将预编译库作为安装(TARGET ...)提供给CMake中的其他项目

时间:2016-04-22 17:49:12

标签: cmake

我想定义一个能够将预编译库导出到源外输出文件夹的CMakeLists。

目前的情况如下:

cmake_minimum_required (VERSION 3.3)

project(duktape)

include_directories("./code" "./code")

file(GLOB allCodeFiles
    "./code/*.h"
    #"./code/*.cpp"
)

add_library(${PROJECT_NAME} SHARED IMPORTED)
set_property(TARGET ${PROJECT_NAME} PROPERTY IMPORTED_LOCATION ./bin/duktape.dll)
set_property(TARGET ${PROJECT_NAME} PROPERTY IMPORTED_IMPLIB ./lib/duktape.lib)

export(PACKAGE ${PROJECT_NAME})

Message(INSTALL_INCLUDE_DIR=${INSTALL_INCLUDE_DIR})

install(TARGETS ${PROJECT_NAME}
  RUNTIME DESTINATION "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
  ARCHIVE DESTINATION "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}"
  LIBRARY DESTINATION "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
  PUBLIC_HEADER DESTINATION "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}/code"
  )

我目前对安装的理解(TARGETS ...)是它将我的libs移动到我的soruce输出文件夹中并将标题共享到项目(可能是某些二进制文件),这取决于lib。

问题在于导出比告诉:

CMake Error at C:/Test1/duktape-1.4.0.win32/CMakeLists.txt:30 (install):
install TARGETS given target "duktape" which does not exist in this directory.

[编辑]: 我仍然没有找到解决方案来定义一个"项目"链接预编译的lib以实现集成目的作为依赖项到其他项目?!我在google等上看到了这样的问题~10次,但仍然不能相信我没有找到一个明确的简单回答这个通用的东西...

这就是我现在最终尝试的方法:

cmake_minimum_required (VERSION 3.3)

project(duktape)
#include(${CMAKE_BINARY_DIR}/DebugCmakeStuff.wtf)

# https://cmake.org/cmake/help/v3.0/command/add_library.html
add_library(${PROJECT_NAME} SHARED IMPORTED GLOBAL)

set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ./include)
set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_COMPILE_DEFINITIONS "")
set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_COMPILE_OPTIONS "")

set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll)
set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib)
set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll)
set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib)
set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll)
set_target_properties(${PROJECT_NAME} PROPERTIES IMPORTED_LOCATION_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib)

#target_link_libraries(${PROJECT_NAME} LINK_PUBLIC "duktape.lib")

#add_custom_target(${PROJECT_NAME}_Export MODULE)
#add_dependencies(${PROJECT_NAME} duktape)

#export(PACKAGE ${PROJECT_NAME})

#install(TARGETS ${PROJECT_NAME}
#  #EXPORT ${PROJECT_NAME}_Targets
#  RUNTIME DESTINATION "${EXECUTABLE_OUTPUT_PATH}"
#  ARCHIVE DESTINATION "${LIBRARY_OUTPUT_PATH}"
#  LIBRARY DESTINATION "${LIBRARY_OUTPUT_PATH}"
#  PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/${PROJECT_NAME}/include")

#install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll DESTINATION "${EXECUTABLE_OUTPUT_PATH}")
#install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib DESTINATION "${LIBRARY_OUTPUT_PATH}")

#add_custom_command(TARGET ${PROJECT_NAME}
#                  PRE_LINK
#                  COMMAND copy ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll "${EXECUTABLE_OUTPUT_PATH}"
#                  COMMAND copy ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib "${LIBRARY_OUTPUT_PATH}"
#                  )

#add_custom_command(OUTPUT duktape.dll
#                   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll ${EXECUTABLE_OUTPUT_PATH}/duktape.dll
#                  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll ${EXECUTABLE_OUTPUT_PATH}/duktape.dll
#                  COMMAND "copy ${CMAKE_CURRENT_SOURCE_DIR}/lib/duktape.lib ${LIBRARY_OUTPUT_PATH}"
#                  )

所以经过2天浪费时间与CMake及其崩溃......我只是意识到我最快90%的速度,如果我会手动编写构建文件......

解决方案很不错

1 个答案:

答案 0 :(得分:1)

命令流install(TARGETS)仅在项目中安装内置目标。它不会安装导入的目标。例如,参见this bugreport

对于安装导入的库,您可以使用install(FILES)命令流:

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.dll
        DESTINATION <...>)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/bin/duktape.lib
        DESTINATION <...>)

(在您的代码中,您安装到CMAKE_RUNTIME_OUTPUT_DIRECTORY和类似的dirs,它们引用 build 树。永远不要在构建树下安装!