1. cmake 的学习入口
cmake 手册文档一把一把的, 就是不带tutorial, ubuntu16 下的apt文件就是这样,无语了!
我不需要读那么多,我只需要一个tutorial就可以了,而tutorial只能从网上不能从man手册或
apt文件中得到了. 不过github上有很多例子,也有tutorial文档及中文翻译文档,这里给个地址可以参考一下
https://github.com/Campanula/CMake-tutorial
2. cmake 语句实例
cmake 的hello-world只需要两句命令就可以了,在CMakeLists.txt中书写
project(hello_prj)
add_executable(hello hello.c)
其它知识照着tutorial 或者其它practice或者learn 练习一下就可以了.
遇到新命令也可以查看man手册页了.
2.1. project(hello_prj) 的解释,
如果去掉该行, cmake 会给出警告, 意思是说在顶部需要定义project,如果你不定义
它也认为存在一个假想的默认的project(Project)
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
2.2. add_executable(hello hello.c) 语句解释.
显然该句的意思是从hello.c 生成执行文件hello 的意思.
3. 本贴目的
这里要说的是:
cmake --trace 选项可以跟踪生成Makefile的过程.
cmake 生成Makefile 后,默认的make 过程隐藏了编译连接过程,要想看到编译连接过程
make VERBOSE=1 , 这样make 过程就不再是silent 的了. 这个过程有时候很重要.
make 也有 --trace 选项, make --trace 也有详细的追踪过程
我之所以学习cmake, 是因为它生成的Makefile,已经包含了很多目标,例如宏展开,中间文件,汇编文件等
方便我们执行莫一个目标.
当然,有的新项目也在使用cmake, 可见cmake还是有一定生命力的.
给一个简单的例子: 包含了
添加第三方include头文件: include_directories()
添加第三方库的连接: target_link_libraries()
示例:
cmake_minimum_required (VERSION 2.6)
project(hello_prj)
include_directories("/opt/ffmpeg_build/include/")
add_executable(hello hello.c)
target_link_libraries(hello "/opt/ffmpeg_build/lib/libavcodec.so")