cmake practice一文中安装可执行文件的方法

在学习cmake practice第四章中,第四章的任务如下

修改 Helloworld 支持安装
在本节开头我们定义了本节的任务如下:
1,为工程添加一个子目录 src,用来存储源代码;
2,添加一个子目录 doc,用来存储这个工程的文档 hello.txt
3,在工程目录添加文本文件 COPYRIGHT, README;
4,在工程目录添加一个 runhello.sh 脚本,用来调用 hello 二进制
4,将构建后的目标文件放入构建目录的 bin 子目录;
5,最终安装这些文件:将 hello 二进制与 runhello.sh 安装至/<prefix>/bin,将
doc 目录中的 hello.txt 以及 COPYRIGHT/README 安装到
/<prefix>/share/doc/cmake/t2,将

给出的解决方案如下

首先我们先补上为添加的文件。
添加 doc 目录及文件:

cd /backup/cmake/t2
mkdir doc
vi doc/hello.txt
随便填写一些内容并保存

在工程目录添加 runhello.sh 脚本,内容为:
hello
添加工程目录中的 COPYRIGHT 和 README
touch COPYRIGHT
touch README

下面改写各目录的 CMakeLists.txt 文件。
1,安装 COPYRIGHT/README,直接修改主工程文件 CMakelists.txt,加入以下指令:
INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/t2)
2,安装 runhello.sh,直接修改主工程文件 CMakeLists.txt,加入如下指令:
INSTALL(PROGRAMS runhello.sh DESTINATION bin)
3,安装 doc 中的 hello.txt,这里有两种方式:一是通过在 doc 目录建立
CMakeLists.txt 并将 doc 目录通过 ADD_SUBDIRECTORY 加入工程来完成。另一种方法
是直接在工程目录通过
INSTALL(DIRECTORY 来完成),前者比较简单,各位可以根据兴趣自己完成,我们来尝试
后者,顺便演示以下 DIRECTORY 的安装。
因为 hello.txt 要安装到/<prefix>/share/doc/cmake/t2,所以我们不能直接安装
整个 doc 目录,这里采用的方式是安装 doc 目录中的内容,也就是使用 ” doc/”
在工程文件中添加
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/t2)

但作者忘记了加上对可执行文件hello的安装

导致执行完

cmake -DCMAKE_INSTALL_PREFIX=/tmp/t2/usr ..
make
make install

让我们进入/tmp/t2 目录看一下安装结果:
./usr
./usr/share
./usr/share/doc

./usr/share/doc/cmake
./usr/share/doc/cmake/t2
./usr/share/doc/cmake/t2/hello.txt
./usr/share/doc/cmake/t2/README
./usr/share/doc/cmake/t2/COPYRIGHT
./usr/bin
./usr/bin/hello
./usr/bin/runhello.sh

对./usr/bin/hello这一项是找不到的,为了解决这一问题,我尝试在t2目录下的CMakeLists.txt文件中添加如下命令

INSTALL(TARGETS hello RUNTIME DESTINATION bin)

但是发现报错

zn@zn-N95-KP6:~/cmake/t2/build$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/t2/usr ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:12 (INSTALL):
  install TARGETS given target "hello" which does not exist in this
  directory.


-- Configuring incomplete, errors occurred!
See also "/home/zn/cmake/t2/build/CMakeFiles/CMakeOutput.log".
zn@zn-N95-KP6:~/cmake/t2/build$ 

经过思考后发现应该在src文件夹下的CMakeLists.txt文件中加入 INSTALL(TARGETS hello RUNTIME DESTINATION bin)  经测试可以正确安装

 

posted @ 2018-02-28 21:12  feifanren  阅读(1459)  评论(0编辑  收藏  举报