【问题标题】:CMake: How to create alias for installing different targets?CMake:如何为安装不同的目标创建别名?
【发布时间】:2014-03-18 03:36:15
【问题描述】:

假设我有以下库:

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})

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

make install

我怎样才能为它们分别设置单独的安装命令?像这样的:

make install-static
make install-shared

更新

头文件也应该在需要时安装:

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

【问题讨论】:

    标签: cmake installation shared-libraries static-libraries alias


    【解决方案1】:

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

    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)
    

    然后调用自定义目标。

    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'
    

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

    【讨论】:

    • 非常感谢!您能否根据更新的问题更新您的答案?您的解决方案没有安装头文件。
    • 在添加install(FILES foo.h DESTINATION include COMPONENT static)install(FILES foo.h DESTINATION include COMPONENT shared)之后,现在所有的头文件在执行make install之后会被复制两次到include目录中。有没有办法解决这个问题?
    • 每次我回答时你都在移动球门柱:)。我不知道。
    • @Meysam:安装命令的 EXCLUDE_FROM_ALL 选项可以提供帮助
    • @vsh "安装一个将 EXCLUDE_FROM_ALL 目标属性设置为 TRUE 的目标具有未定义的行为。"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多