自定义目标作为cmake中的目标库 [英] custom target as a target library in cmake

查看:77
本文介绍了自定义目标作为cmake中的目标库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom target that is in fact an externally generated library that I want to integrate in my build.

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这个目标实际上是一个库, 在哪里可以找到它以及标题在哪里

How can I tell cmake that this target is in fact a library, where it can be found and where are the headers ?

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

To be clear : I don't want the upper CMakeList using this library having to manually specify include folders and the library location folder It must be done automatically (from the target properties).

在标准cmake库中,我会只需在库CMakeLists中添加INTERFACE_INCLUDE_DIRECTORIES属性,即可使cmake将我的应用程序与相关的-I和-L gcc参数链接起来:

On a standard cmake library I would just have to add the INTERFACE_INCLUDE_DIRECTORIES property in the library CMakeLists to make cmake link my app with the relevant -I and -L gcc parameters :

set_target_properties(lib1
  PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES
  ${CMAKE_CURRENT_SOURCE_DIR})

但是对于自定义目标,我不知道如何操作。

But in the case of a custom target I don't know how to to it.

有任何线索吗?

感谢您的帮助。

感谢zaufi,它可以正常工作!

Thanks to zaufi it works!

对于其他可能对在cmake内外部嵌入目标物感兴趣的人,我是这样做的:

For others who may be interested in embedded externally build target inside cmake here is what I did :

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中,我可以做类似的事情

Now in a CMakeLists.txt I can do somthing like

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的位置。

No need to specify where the .a and the .h are.

推荐答案

您可以使用 add_library()并说出它实际上是< a href = http://www.cmake.org/cmake/help/v3.3/command/add_library.html#imported-libraries rel = noreferrer>已导入。然后,使用 set_target_properties()可以为其设置必需的 INTERFACE_XXX 属性。之后,您可以将其用作项目目标的序数目标。

You can use add_library() and tell that it actually imported. Then, using set_target_properties() you can set required INTERFACE_XXX properties for it. After that, you can use it as an ordinal target like every other built by your project.

这篇关于自定义目标作为cmake中的目标库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆