cmake中的${CMAKE_MATCH_1}是什么
时间: 2024-06-02 16:09:50 浏览: 8
${CMAKE_MATCH_1} 是一个 CMake 变量,它表示在比对字符串过程中被捕获的匹配模式的第一个部分。具体来说,它是由 CMake 内部的正则表达式引擎设置的,当使用正则表达式函数(如 string(REGEX MATCH))进行字符串匹配时,可以在指定匹配模式的同时使用括号将需要捕获的部分括起来,这些被括起来的部分就可以使用 ${CMAKE_MATCH_1}、${CMAKE_MATCH_2} 等变量来引用。
相关问题
CMake Error: The source "/home/tao/Desktop/xyl/astra_camera_ws/src/CMakeLists.txt" does not match the source "/home/tao/Desktop/xyl/astra_camera_ws/src/ros_astra_camera/CMakeLists.txt" used to generate cache. Re-run cmake with a different source directory. Makefile:1416: recipe for target 'cmake_check_build_system' failed make: *** [cmake_check_build_system] Error 1
这个错误是由于你在运行CMake时使用的源文件目录与之前生成缓存时使用的源文件目录不匹配所导致的。根据错误信息,你可以尝试重新运行cmake命令,并指定一个不同的源文件目录来解决这个问题。你可以尝试以下步骤:
1. 删除之前生成的构建文件夹(如果存在),执行以下命令:
```
rm -rf /home/tao/Desktop/xyl/astra_camera_ws/build
```
2. 创建一个新的构建文件夹并进入该目录:
```
mkdir /home/tao/Desktop/xyl/astra_camera_ws/build
cd /home/tao/Desktop/xyl/astra_camera_ws/build
```
3. 运行cmake命令,并指定正确的源文件目录:
```
cmake ../src/ros_astra_camera
```
这样应该能够解决这个问题。请确保你在运行cmake命令时指定了正确的源文件目录,并且删除了之前生成的构建文件夹。如果问题仍然存在,请检查你的项目配置是否正确。
cmake 正则处理 获取 #include "xx" 中的xx
可以使用正则表达式来匹配 `#include "xx"` 中的 `xx`。以下是一个示例 CMakeLists.txt 文件中如何使用正则表达式来获取 `#include` 中的文件名:
```cmake
cmake_minimum_required(VERSION 3.10)
project(my_project)
# 匹配 #include "xx"
set(include_regex "#include \"([^\"]+)\"")
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp file_contents)
string(REGEX MATCHALL ${include_regex} include_matches ${file_contents})
foreach(match ${include_matches})
string(REGEX REPLACE ${include_regex} "\\1" include_file ${match})
message("Found include file: ${include_file}")
endforeach()
```
这个例子中,我们使用 `file(READ ...)` 命令读取 `main.cpp` 文件的内容,并将其存储在变量 `file_contents` 中。然后,我们使用 `string(REGEX MATCHALL ...)` 命令匹配所有 `#include "xx"` 的语句,并将匹配结果存储在变量 `include_matches` 中。
接下来,我们使用 `foreach()` 循环遍历 `include_matches` 中的每一个匹配结果,并使用 `string(REGEX REPLACE ...)` 命令将匹配结果中的 `#include "` 和 `"` 删除,得到文件名 `include_file`。最后,我们使用 `message()` 命令输出找到的文件名。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)