CMake:如何创建别名以安装不同的目标? [英] CMake: How to create alias for installing different targets?

查看:807
本文介绍了CMake:如何创建别名以安装不同的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下库:

  add_library(myLib_static STATIC $ {SRC_FILES})
add_library(myLib SHARED $ {SRC_FILES})

#安装头文件
install(FILES $ {H_FILES} DESTINATION $ {INSTDIRHEADER})

#安装二进制文件
install(TARGETS myLib_static
DESTINATION $ {INSTDIRBIN})

install(TARGETS myLib
DESTINATION $ {INSTDIRBIN})

如果我执行以下命令,将安装共享库和静态库:

  make install 

如何分别安装?像这样:

  make install-static 
make install-shared

更新



需要:

  install(FILES $ {H_FILES} DESTINATION $ {INSTDIRHEADER})


解决方案

将它们分别放入不同的组件并为安装设置自定义目标。

  add_library(foo_static STATIC foo.cpp)
add_library(foo SHARED foo.cpp)

install(TARGETS foo_static
DESTINATION bin
COMPONENT static)

install(TARGETS foo
DESTINATION bin
COMPONENT shared)

add_custom_target(foo- install
DEPENDS foo
COMMAND
$ {CMAKE_COMMAND}-DCMAKE_INSTALL_COMPONENT = shared
-P$ {CMAKE_BINARY_DIR} /cmake_install.cmake


add_custom_target(foo_static-install
DEPENDS foo_static
COMMAND
$ {CMAKE_COMMAND}-DCMAKE_INSTALL_COMPONENT = static
-P$ {CMAKE_BINARY_DIR} /cmake_install.cmake


install(FILES foo.h DESTINATION包括COMPONENT static)
install(FILES foo.h DESTINATION包括COMPONENT共享)



然后调用自定义目标。

  stephen @hal:〜/ dev / src / playground / cmake / build {master} $ cmake .. -DCMAKE_INSTALL_PREFIX = prefix 
- C编译器标识为GNU 4.8.1
- CXX编译器标识是GNU 4.8.1
- 检查工作C编译器:/ usr / lib / icecc / bin / cc
- 检查工作C编译器:/ usr / lib / icecc / - works
- 检测C编译器ABI info
- 检测C编译器ABI info - done
- 检查CXX编译器是否正常:/ usr / lib / icecc / bin / c ++
- 检查工作的CXX编译器:/ usr / lib / icecc / bin / c ++ - 工作
- 检测CXX编译器ABI信息
- 检测CXX编译器ABI信息 - 完成
- 配置完成
- 生成完成
- 生成文件已写入:/ home / stephen / dev / src / playground / cmake / build
stephen @ hal:〜/ dev / src / playground / cmake / build {master} $ make foo_static-install
makeobj [0]:输入目录`/ home / stephen / dev / src / playground / cmake / build'
扫描依赖的目标foo_static
[100%]构建CXX对象CMakeFiles / foo_static.dir / foo.cpp.o
链接CXX静态库libfoo_static.a
[100%]建立目标foo_static
扫描目标foo_static-install的依赖关系
- 安装配置:
- 安装:/home/stephen/dev/src/playground/cmake/build/prefix/bin/libfoo_static.a
- 安装:/home/stephen/dev/src/playground/cmake/build/prefix/include/foo.h

[100%]内置目标foo_static-install
makeobj [0]:离开目录`/ home / stephen / dev / src / playground / cmake / build'
stephen @ hal:〜/ dev / src / playground / cmake / build {master} $ make foo-install
makeobj [0]:输入目录`/ home / stephen / dev / src / playground / cmake / build'
扫描目标foo的依赖关系
[100%]构建CXX对象CMakeFiles / foo .dir / foo.cpp.o
链接CXX共享库libfoo.so
[100%]建立目标foo
扫描目标foo-install的依赖关系
- 安装配置:
- 安装:/home/stephen/dev/src/playground/cmake/build/prefix/bin/libfoo.so
- 最新版本:/ home / stephen / dev /src/playground/cmake/build/prefix/include/foo_p.h
[100%]内置目标foo-install
makeobj [0]:离开目录`/ home / stephen / dev / src / playground / cmake / build'

请注意,cpack使用组件允许用户安装软件包决定要安装哪些组件。因此,对于库,头可能是Development组件的一部分。在这种情况下,我们使用共享和静态组件来安装头。如果要以这种方式使用cpack,则可以使用Development组件另外安装它。


Suppose that I've got the following libraries:

add_library(myLib_static STATIC ${SRC_FILES})
add_library(myLib SHARED ${SRC_FILES})

# installing header files
install(FILES ${H_FILES} DESTINATION ${INSTDIRHEADER})

# installing binaries
install(TARGETS myLib_static
         DESTINATION ${INSTDIRBIN})

install(TARGETS myLib
         DESTINATION ${INSTDIRBIN})

If I execute the following command, both shared and static libraries will be installed:

make install

How can I have separate install commands for each of them? Something like this:

make install-static
make install-shared

Update:

Header files should also be installed when needed:

install(FILES ${H_FILES} DESTINATION ${INSTDIRHEADER})

解决方案

Put them each in a different component and set up custom targets for the installations.

add_library(foo_static STATIC foo.cpp)
add_library(foo SHARED foo.cpp)

install(TARGETS foo_static
        DESTINATION bin
        COMPONENT static)

install(TARGETS foo
        DESTINATION bin
        COMPONENT shared)

add_custom_target(foo-install
  DEPENDS foo
  COMMAND 
      "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=shared
      -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)

add_custom_target(foo_static-install
  DEPENDS foo_static
  COMMAND
     "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=static 
     -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)

install(FILES foo.h DESTINATION include COMPONENT static)
install(FILES foo.h DESTINATION include COMPONENT shared)

Then invoke the custom targets.

stephen@hal:~/dev/src/playground/cmake/build{master}$ cmake .. -DCMAKE_INSTALL_PREFIX=prefix
-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: /usr/lib/icecc/bin/cc
-- Check for working C compiler: /usr/lib/icecc/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/lib/icecc/bin/c++
-- Check for working CXX compiler: /usr/lib/icecc/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/stephen/dev/src/playground/cmake/build
stephen@hal:~/dev/src/playground/cmake/build{master}$ make foo_static-install
makeobj[0]: Entering directory `/home/stephen/dev/src/playground/cmake/build'
Scanning dependencies of target foo_static
[100%] Building CXX object CMakeFiles/foo_static.dir/foo.cpp.o
Linking CXX static library libfoo_static.a
[100%] Built target foo_static
Scanning dependencies of target foo_static-install
-- Install configuration: ""
-- Installing: /home/stephen/dev/src/playground/cmake/build/prefix/bin/libfoo_static.a
-- Installing: /home/stephen/dev/src/playground/cmake/build/prefix/include/foo.h

[100%] Built target foo_static-install
makeobj[0]: Leaving directory `/home/stephen/dev/src/playground/cmake/build'
stephen@hal:~/dev/src/playground/cmake/build{master}$ make foo-install
makeobj[0]: Entering directory `/home/stephen/dev/src/playground/cmake/build'
Scanning dependencies of target foo
[100%] Building CXX object CMakeFiles/foo.dir/foo.cpp.o
Linking CXX shared library libfoo.so
[100%] Built target foo
Scanning dependencies of target foo-install
-- Install configuration: ""
-- Installing: /home/stephen/dev/src/playground/cmake/build/prefix/bin/libfoo.so
-- Up-to-date: /home/stephen/dev/src/playground/cmake/build/prefix/include/foo_p.h
[100%] Built target foo-install
makeobj[0]: Leaving directory `/home/stephen/dev/src/playground/cmake/build'

Note that components are used by cpack to allow the user installing a package to decide which components to install. So, for a library, the headers might be part of the Development component. In this case we install the header with both the shared and static component. It might make sense to additionally install it with a Development component if cpack is to be used in that way.

这篇关于CMake:如何创建别名以安装不同的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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