使用cmake 文件操作时不可避免需要操作相关文件,比如读取文件内容,创建新文件的等等操作,都需要通过file命令进行操作。
常用的从文件中读取操作为file(READ
file(read <filename> <variable> [OFFSET <offset>] [LIMIT <max-in>] [HEX])
LIMIT
以一个例子说明,实验目录下有两个文件,使用tree命令查看:
- $ tree
- .
- ├── CMakeLists.txt
- └── test.cpp
-
- 0 directories, 2 files
- $
test.cpp文件内容如下:
- #include
- #include
-
-
-
- int main(){
- printf("hello world!\n");
- }
CMakeList.txt文件内容如下:
- cmake_minimum_required(VERSION 3.13.4)
-
- project(cmake_read)
-
- message("--------Proj Src Dir: " ${PROJECT_SOURCE_DIR})
- file(READ ${PROJECT_SOURCE_DIR}/test.cpp TEST_RESULT)
- message("--------TEST RESULT: " ${TEST_RESULT})
使用file(read)命令将文件test.cpp的内容读取到TEST_RESULT变量中,并打印出来,使用cmake运行:
- $ /public/home/huozhikun/cmake/cmake-3.17.0/bin/cmake ..
- CMake Error: The source directory "/public/home/huozhikun/test/cmake/read" does not appear to contain CMakeLists.txt.
- Specify --help for usage, or press the help button on the CMake GUI.
- [huozhikun@login09 example]$ cd build/
- [huozhikun@login09 build]$ /public/home/huozhikun/cmake/cmake-3.17.0/bin/cmake ..
- -- The C compiler identification is GNU 7.3.1
- -- The CXX compiler identification is GNU 7.3.1
- -- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
- -- Check for working C compiler: /opt/rh/devtoolset-7/root/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: /opt/rh/devtoolset-7/root/usr/bin/c++
- -- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++ - works
- -- Detecting CXX compiler ABI info
- -- Detecting CXX compiler ABI info - done
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- --------Proj Src Dir: /public/home/huozhikun/test/cmake/read/example
- --------TEST RESULT: #include
- #include
-
-
-
- int main(){
- printf("hello world!\n")
- }
-
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /public/home/huozhikun/test/cmake/read/example/build
将test.cpp的文件内容原封不动读取到TEST_RESULT变量中。
file STRINGS命令是将文件的内容读取成一串ASCII字符串到变量中,其中二进制文件将会被忽略,在读取文件中 回车及\r将会被忽略:
file(STRINGS [ ...])
1、file WRITE命令
2、file APPEND命令
3、file TOUCH命令
4、file GENERATE命令
file GLOB命令主要用于匹配规则在指定的目录内匹配到所需要的文件,命令行格式:
file(GLOB [LIST_DIRECTORIES true[false]] [RELATIVE ] [CONFIGURE_DEPENDS] [ ...])
- *.cxx - match all files with extension cxx
- *.vt? - match all files with extension vta,...,vtz
- f[3-5].txt - match files f3.txt, f4.txt, f5.txt
用例
- cmake_minimum_required(VERSION 3.13.4)
-
- project(cmake_filesystem)
-
- message("--------Proj Src Dir: " ${CMAKE_CURRENT_SOURCE_DIR})
-
- file(GLOB TEST_RESULT LIST_DIRECT true RELATIVE /public/home *.cpp)
- message("--------TEST_RESULT: ${TEST_RESULT}")
寻找当前路径下的cpp文件,且返回的结果中为/public/home的相对路径,结果:
- $ /public/home/huozhikun/cmake/cmake-3.17.0/bin/cmake ..
- ... ...
- --------Proj Src Dir: /public/home/huozhikun/test/cmake/filesystem/example_3
- --------TEST_RESULT: huozhikun/test/cmake/filesystem/example_3/test_2.cpp
- ... ...
file COPY命令用于将文件copy到目标目录中,命令行格式如下:
- file(COPY
... DESTINATION -
- [FILE_PERMISSIONS
...] -
- [DIRECTORY_PERMISSIONS
... ] -
- [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
-
- [FILES_MATCHING]
-
- [ [PATTERN
| REGEX ] -
- [EXCLUDE] [PERMISSIONS
... ] ] -
- [...])
OWNER_READ
OWNER_WRITE
OWNER_EXECUTE
GROUP_READ
GROUP_WRITE
GROUP_EXECUTE
WORLD_READ
WORLD_WRITE
WORLD_EXECUTE
1、file RELATIVE_PATH
2、file TO_CMAKE_PATH
3、 file TO_NATIVE_PATH