自定义目标作为cmake中的目标库

时间:2015-07-07 16:41:22

标签: cmake

我有一个自定义目标,实际上是一个外部生成的库,我想在我的构建中集成。

add_custom_command(
       OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/liblib2.a
       COMMAND make -f ${CMAKE_CURRENT_SOURCE_DIR}/makefile liblib2.a)

add_custom_target(lib2  
       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/liblib2.a)

我如何告诉cmake这个目标实际上是一个库,哪里可以找到它?哪些是标题

要明确:我不希望使用此库的上层CMakeList必须手动指定包含文件夹和库位置文件夹它必须自动完成(来自目标属性)。

在标准的cmake库中,我只需要在库CMakeLists中添加INTERFACE_INCLUDE_DIRECTORIES属性,使我的应用程序与相关的-I和-L gcc参数进行cmake链接:

set_target_properties(lib1
  PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES
  ${CMAKE_CURRENT_SOURCE_DIR})

但是在自定义目标的情况下,我不知道该怎么做。

有任何线索吗?

感谢您的帮助。

感谢zaufi它有效!

对于其他可能对cmake内嵌式外部构建目标感兴趣的人,我就是这样做的:

cmake_minimum_required(VERSION 2.8)

SET(LIB_FILE ${CMAKE_CURRENT_SOURCE_DIR}/bin/liblib2.a)
SET(LIB_HEADER_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/include)

# how to build the result of the library
add_custom_command(OUTPUT  ${LIB_FILE}
                   COMMAND make 
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

# create a target out of the library compilation result
add_custom_target(lib2_target DEPENDS ${LIB_FILE})

# create an library target out of the library compilation result
add_library(lib2 STATIC IMPORTED GLOBAL)
add_dependencies(lib2 lib2_target)

# specify where the library is and where to find the headers
set_target_properties(lib2
    PROPERTIES
    IMPORTED_LOCATION ${LIB_FILE}
    INTERFACE_INCLUDE_DIRECTORIES ${LIB_HEADER_FOLDER})

现在在CMakeLists.txt中,我可以做类似

的事情
add_subdirectory(${ROOT_DIR}/lib1 bin/lib1)
add_subdirectory(${ROOT_DIR}/lib2 bin/lib2)
add_executable(app app.c )
target_link_libraries(app lib1 lib2)

无需指定.a和.h所在的位置。

2 个答案:

答案 0 :(得分:15)

您可以使用add_library()并告诉它imported。然后,使用set_target_properties()您可以为其设置所需的INTERFACE_XXX属性。之后,您可以像项目所构建的其他目标一样将其用作序数目标。

答案 1 :(得分:1)

感谢您发布解决方案。我已将您的代码段包装为一个函数:

function(add_external_library)
    set(options)
    set(oneValueArgs TARGET WORKING_DIRECTORY OUTPUT COMMENT)
    set(multiValueArgs COMMAND INCLUDE_DIRS)
    cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" ${multiValueArgs}" ${ARGN})

    # Specify how to build the result of the library
    add_custom_command(OUTPUT "${ARGS_OUTPUT}"
        COMMAND ${ARGS_COMMAND}
        WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
        COMMENT "${ARGS_COMMENT}")

    # Create a target out of the library compilation result
    add_custom_target(${ARGS_TARGET}_target DEPENDS ${ARGS_OUTPUT})

    # Create an library target out of the library compilation result
    add_library(${ARGS_TARGET} STATIC IMPORTED GLOBAL)
    add_dependencies(${ARGS_TARGET} ${ARGS_TARGET}_target)

    # Specify where the library is and where to find the headers
    set_target_properties(${ARGS_TARGET}
        PROPERTIES
        IMPORTED_LOCATION "${ARGS_OUTPUT}"
        INTERFACE_INCLUDE_DIRECTORIES "${ARGS_INCLUDE_DIRS}")
endfunction()

# Example
add_external_library(TARGET YourLib
    COMMAND           /bin/bash compile_your_lib.sh
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
    OUTPUT            "output/yourlib.a"
    INCLUDE_DIRS      "include/a" "include/b"
    COMMENT           "Building YourLib")

add_executable(YourExe)
target_link_libraries(YourExe YourLib)