1:基本语法规则
无规矩不成方圆,每个工具都有自己的语言和语法,我们先看下最基本的语法规则:
- 变量用${VAR}获取变量的值,凡事都有例外,在IF语句中可以直接使用变量名,这个有点那个。
- 指令(参数1 参数2...)跟函数调用很像吧。参数之间可以用空格或者;隔开。
- 指令大小写无关,但是参数却相关,这个也有点那个。我们指令还是都用大写的吧,好记。
2:何谓内外
- 内部构建(in-source build)
- 外部构建(out-of-source build)
从字面上我们已经知道了,内部构建就是和源文件在一起构建,外部构建就是和源文件不在一个目录。上面我们已经看到了,构建后在我们的源文件目录下多了很多临时文件,因为cmake没有理想的办法执行命令来删除这些临时文件,这就有了我们的外部构建,我们以后还是使用外部构建吧。
3:重新构建(外部构建)
前面一节我们看到的就是所谓内部构建,下面我们来看看常用的外部构建。
- 删除临时文件,只保留我们的源文件和CMakeLists.txt文件
- 在同一目录建立一个build目录,并进入
- 外部构建我们的工程
- [onezeroone@ ex-1]$ ls
-
CMakeLists.txt hello.c
-
[onezeroone@ ex-1]$ mkdir build
-
[onezeroone@ ex-1]$ cd build/
-
[onezeroone@ build]$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - 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
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/backup/cmake/ex-1/build
-
[onezeroone@ build]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[100%] Building C object CMakeFiles/hello.dir/hello.c.o
-
Linking C executable hello
-
[100%] Built target hello
-
[onezeroone@ build]$ ./hello
-
Hello world
-
[onezeroone@ build]$
注意现在我们cmake的时候,跟的路径是CMakeLists.txt文件所在的路径“..”父目录,现在所有生成的文件都在build目录下面,这样我的源文件看起来还是很整齐的,不影响心情就好。
4:丰富下我们的CMakeLists.txt
- PROJECT(EX-1)
-
SET(SRC_LIST hello.c)
-
MESSAGE(STATUS "The BINARY dir is ${PROJECT_BINARY_DIR}")
-
MESSAGE(STATUS "The SOURCE dir is ${PROJECT_SOURCE_DIR}")
-
ADD_EXECUTABLE(hello ${SRC_LIST})
我们可以看到我们的project名字和生成的可执行文件名字是没有关系的。
SET指令的语法:
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
用来定义变量的,前面提到我们可以用 ${VAR}来获取变量值。
MESSAGE指令的语法:
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message" ...)
SEND_ERROR:表示产生错误信息,过程将被跳过。
STATUS:表示一般的状态信息。前面我们看到会自动加前缀“-”
FATAL_ERROR:我们知道肯定是严重错误信息,cmake会立即停止执行。
PROJECT会定义两个隐式的变量“PROJECT_BINARY_DIR”和“PROJECT_SOURCE_DIR”,前者就是我们构建时的目录,后者就是源文件所在的目录。其中的PROJECT也可以有PROJECT指令指定的工程名代替,例如“EX-1_BINARY_DIR”和“EX-1_SOURCE_DIR”我们推荐前者,这样不用每次都修改。
5:再构建下吧
- [onezeroone@ ex-1]$ ls
-
build CMakeLists.txt hello.c
-
[onezeroone@ ex-1]$ cd build/
-
[onezeroone@ build]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake hello Makefile
-
[onezeroone@ build]$ rm -rf *
-
[onezeroone@ build]$ ls
-
[onezeroone@ build]$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - 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
-
-- The BINARY dir is /home/onezeroone/work/backup/cmake/ex-1/build
-
-- The SOURCE dir is /home/onezeroone/work/backup/cmake/ex-1
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/backup/cmake/ex-1/build
-
[onezeroone@ build]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[100%] Building C object CMakeFiles/hello.dir/hello.c.o
-
Linking C executable hello
-
[100%] Built target hello
-
[onezeroone@ build]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake hello Makefile
-
[onezeroone@ build]$ ./hello
-
Hello world
-
[onezeroone@ build]$
我们可以看到期望的BINARY_DIR和SOURCE_DIR了。这就是我们的外部构建了。
阅读(723) | 评论(0) | 转发(0) |