CMake入门学习+实战<二> helloworld完善

来源:互联网 发布:windows 10 虚拟显存 编辑:程序博客网 时间:2024/06/05 07:32

文章整合自:

CMake Practice

http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf

CMake 官方文档

https://cmake.org/cmake/help/latest/

以及其他网络资料

目标:

代码移植

静动态库编译调用

文章为CMake Practice的笔记以及一些错误整改或者补充。请参照源文章学习,官方文章以供深入学习。


思维导图为cmake practice目录,阅读时间建议为半天


<二>cmake helloWorld 改进


  • ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])

src——>源代码目录 bin——>目标输出目录#just like what it names


  • SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)#设置二进制输出路径build/bin
  • SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)#设置库文件输出路径build/lib
上述命令加入到含有ADD_EXECUTABLE/ADD_LIBRARY命令的CMakeLists.txt 文件下 
  • 安装工作
make install #安装至/usr/bin/目录下

make install
DESTDIR=/tmp/test #安装至/tmp/test/usr/bin目录下

make install
DESTDIR=
PREFIX=/usr #PREFIX设定

注意PREFIX和DESTDIR的区别:

编号1决定了软件包安装后的位置,以及软件包在运行时在哪里查找相关文件.这是你应该使用,如果你只是编译的东西在单个主机上使用.
determines where the package will go when it is installed, and where it will look for its associated files when it is run. It's what you should use if you're just compiling something for use on a single host.

编号3用于安装到临时目录,这不是从哪个程序包运行.例如,这在构建 deb 包时使用.构建包的人实际上并不将所有东西安装在其自己的系统上的最终位置.他可能已经安装了不同的版本,不想打扰它,或者他甚至可能不是根.所以他使用configure --prefix = / usr,所以程序将运行时预期安装在/ usr中,然后make install DESTDIR = debian / tmp来实际创建目录结构.
installing to a temporary directory which is not where the package will be run from. For example this is used when building deb packages. The person building the package doesn't actually install everything into its final place on his own system. He may have a different version installed already and not want to disturb it, or he may not even be root. So he uses configure --prefix=/usr so the program will expect to be installed in /usr when it runs, then make install DESTDIR=debian/tmp to actually create the directory structure.

INSTALL(TARGETS myrun mylib mystaticlib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION libstatic
)
上面的例子会将:
可执行二进制myrun 安装到${CMAKE_INSTALL_PREFIX}/bin 目录
动态库libmylib安装到${CMAKE_INSTALL_PREFIX}/lib目录
静态库libmystaticlib 安装到${CMAKE_INSTALL_PREFIX}/libstatic目录

章节总结:

原创粉丝点击