Make Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- The C compiler identification is GNU 7.5.0 -- The CXX compiler identification is GNU 7.5.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done Build type: Release -- Performing Test COMPILER_SUPPORTS_CXX11 -- Performing Test COMPILER_SUPPORTS_CXX11 - Success -- Performing Test COMPILER_SUPPORTS_CXX0X -- Performing Test COMPILER_SUPPORTS_CXX0X - Success -- Using flag -std=c++11. -- Found OpenCV: /usr/local (found suitable version "4.2.0", minimum required is "4.2") OPENCV VERSION: 4.2.0 CMake Warning at CMakeLists.txt:43 (find_package): By not providing "Findrealsense2.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "realsense2", but CMake did not find one. Could not find a package configuration file provided by "realsense2" with any of the following names: realsense2Config.cmake realsense2-config.cmake Add the installation prefix of "realsense2" to CMAKE_PREFIX_PATH or set "realsense2_DIR" to a directory containing one of the above files. If "realsense2" provides a separate development package or SDK, be sure it has been installed. CMake Error at CMakeLists.txt:116 (add_subdirectory): add_subdirectory given source "Thirdparty/g2o" which is not an existing directory. -- Configuring incomplete, errors occurred! See also "/home/robot/XTDrone/sensing/slam/vslam/ORB_SLAM3/build/CMakeFiles/CMakeOutput.log". make: *** 没有指明目标并且找不到 makefile。 停止。 ./build.sh: 第 9 行: cd: ../../g2o: 没有那个文件或目录 Configuring and building Thirdparty/g2o ... CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. Build type: Release -- Using flag -std=c++11. OPENCV VERSION: 4.2.0 CMake Warning at CMakeLists.txt:43 (find_package): By not providing "Findrealsense2.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "realsense2", but CMake did not find one. Could not find a package configuration file provided by "realsense2" with any of the following names: realsense2Config.cmake realsense2-config.cmake Add the installation prefix of "realsense2" to CMAKE_PREFIX_PATH or set "realsense2_DIR" to a directory containing one of the above files. If "realsense2" provides a separate development package or SDK, be sure it has been installed. CMake Error at CMakeLists.txt:116 (add_subdirectory): add_subdirectory given source "Thirdparty/g2o" which is not an existing directory. -- Configuring incomplete, errors occurred! See also "/home/robot/XTDrone/sensing/slam/vslam/ORB_SLAM3/build/CMakeFiles/CMakeOutput.log". make: *** 没有指明目标并且找不到 makefile。 停止。 ./build.sh: 第 18 行: cd: ../../Sophus: 没有那个文件或目录 Configuring and building Thirdparty/Sophus ... CMake Error: The source directory "/home/robot/XTDrone/sensing/slam/vslam/ORB_SLAM3/build/build" does not appear to contain CMakeLists.txt. Specify --help for usage, or press the help button on the CMake GUI. make: *** 没有指明目标并且找不到 makefile。 停止。 Uncompress vocabulary ... Configuring and building ORB_SLAM3 ... mkdir: 无法创建目录"build": 文件已存在 CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. Build type: Release -- Using flag -std=c++11. OPENCV VERSION: 4.2.0 CMake Warning at CMakeLists.txt:43 (find_package): By not providing "Findrealsense2.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "realsense2", but CMake did not find one. Could not find a package configuration file provided by "realsense2" with any of the following names: realsense2Config.cmake realsense2-config.cmake Add the installation prefix of "realsense2" to CMAKE_PREFIX_PATH or set "realsense2_DIR" to a directory containing one of the above files. If "realsense2" provides a separate development package or SDK, be sure it has been installed. CMake Error at CMakeLists.txt:116 (add_subdirectory): add_subdirectory given source "Thirdparty/g2o" which is not an existing directory. -- Configuring incomplete, errors occurred! See also "/home/robot/XTDrone/sensing/slam/vslam/ORB_SLAM3/build/CMakeFiles/CMakeOutput.log". make: *** 没有指明目标并且找不到 makefile。 停止。
时间: 2025-07-22 10:41:51 浏览: 3
<think>我们正在处理用户提出的三个CMake构建问题:
1. CMake弃用警告:要求更新cmake_minimum_required的版本
2. 找不到realsense2包
3. add_subdirectory调用错误(目录Thirdparty/g2o不存在)
我们将逐一解决这三个问题。
问题1: 更新cmake_minimum_required版本
根据之前的讨论,我们需要在CMakeLists.txt中更新cmake_minimum_required命令的版本号。
问题2: 找不到realsense2包
用户提到错误信息“realsense2 package not found”。这通常是因为CMake的find_package命令无法定位到realsense2的配置文件。我们需要确保realsense2 SDK已正确安装,并且设置了相应的环境变量或CMake变量来帮助CMake找到它。
问题3: add_subdirectory错误
错误信息“add_subdirectory Thirdparty/g2o”表示在项目的Thirdparty目录下没有g2o子目录,或者该路径不正确。我们需要检查项目结构,确保g2o库的路径正确,或者调整add_subdirectory的路径。
下面我们详细解决每个问题。
### 解决方案
#### 1. 解决CMake弃用警告
在CMakeLists.txt文件的开头,找到`cmake_minimum_required`命令,将其版本更新为较新的版本(至少2.8.12以上,推荐使用当前使用的CMake版本)。
例如,如果当前CMake版本是3.10,则修改为:
```cmake
cmake_minimum_required(VERSION 3.10)
```
或者,为了兼容一个范围,可以这样写(推荐):
```cmake
cmake_minimum_required(VERSION 3.10...3.25)
```
这表示项目支持从3.10到3.25的CMake版本[^1]。
修改后,重新运行CMake,警告应该消失。
#### 2. 解决realsense2包找不到的问题
首先,确保已经安装了realsense2 SDK。安装方法取决于操作系统:
- **Ubuntu**:
```bash
sudo apt-get install librealsense2-dev
```
- **Windows**: 从Intel Realsense的GitHub发布页面下载SDK并安装。
如果已经安装但CMake仍然找不到,可以尝试以下方法:
方法1:在CMakeLists.txt中显式指定realsense2的路径
```cmake
find_package(realsense2 REQUIRED
PATHS
"C:/Program Files (x86)/Intel RealSense SDK 2.0" # Windows示例路径
/usr/local/lib/cmake/realsense2 # Linux示例路径
)
```
方法2:在运行CMake时通过命令行指定realsense2_DIR(假设realsense2Config.cmake文件在`<path_to_realsense2>/cmake`目录下):
```bash
cmake -D realsense2_DIR=<path_to_realsense2>/cmake ..
```
方法3:如果使用vcpkg等包管理器,确保设置了工具链文件,例如:
```bash
cmake -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake ..
```
如果以上方法都不行,检查realsense2的安装位置,确认`realsense2Config.cmake`文件是否存在。
#### 3. 解决add_subdirectory错误
错误信息表明`add_subdirectory`指定的目录不存在。我们需要检查:
- 项目目录结构:确认`Thirdparty/g2o`目录是否存在?如果不存在,需要下载g2o库。
- 如果g2o是作为子模块(git submodule)提供的,确保已经初始化并更新子模块:
```bash
git submodule init
git submodule update
```
- 如果项目中没有包含g2o,需要手动下载并放置在正确位置。或者,使用其他方式引入g2o(如find_package)。
如果g2o库不在`Thirdparty/g2o`,而是其他路径,修改`add_subdirectory`的路径参数。例如:
```cmake
add_subdirectory(Thirdparty/g2o-modified) # 修改为正确的相对路径
```
如果项目不需要g2o,或者想使用系统安装的g2o,可以改用`find_package`:
```cmake
find_package(G2O REQUIRED)
# 然后链接到目标
target_link_libraries(your_target PRIVATE G2O::core)
```
但是,请注意,系统安装的g2o可能版本不匹配。因此,推荐使用项目自带的g2o。
### 操作步骤总结
1. 更新CMakeLists.txt中的`cmake_minimum_required`版本。
2. 确保realsense2 SDK已安装,并在CMake中正确配置其路径。
3. 检查g2o目录是否存在,若不存在则下载或更新子模块;或修改路径,或改用find_package。
重新运行CMake,应该可以解决这些问题。
如果问题仍然存在,请检查CMake输出日志,定位具体错误信息。
### 相关问题
1. 如何查看当前安装的CMake版本?
2. 如何安装特定版本的realsense2 SDK?
3. 如何使用git子模块管理第三方库?</think>### 解决CMake构建三大问题:版本警告、realsense2缺失、g2o目录错误
针对您遇到的CMake构建问题(版本兼容警告、realsense2包缺失、g2o目录错误),以下是完整的解决方案:
---
### 一、解决CMake弃用警告(更新cmake_minimum_required)
**问题现象**
`CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed...`
**解决方案**
1. 打开主`CMakeLists.txt`文件
2. 修改开头的版本声明(根据您的CMake版本调整):
```cmake
# 推荐方案:指定兼容范围(示例)
cmake_minimum_required(VERSION 3.5...3.25)
# 或指定固定版本(运行 cmake --version 查看实际版本)
cmake_minimum_required(VERSION 3.21)
```
3. **关键技巧**:
- 使用`...`范围语法兼容新旧版本[^1]
- 若需兼容旧系统,最低版本不低于`2.8.12`
- 删除`build/`目录后重新配置
---
### 二、解决realsense2包缺失(find_package失败)
**问题现象**
`realsense2 package not found`
**解决方案**
#### 步骤1:安装依赖
```bash
# Ubuntu
sudo apt-get install librealsense2-dev
# Windows(vcpkg)
vcpkg install realsense2:x64-windows
```
#### 步骤2:修改CMakeLists.txt
```cmake
# 添加realsense2查找逻辑
find_package(realsense2 REQUIRED)
# 链接到目标
target_link_libraries(your_target PRIVATE
realsense2::realsense2
)
```
#### 步骤3:手动指定路径(若自动查找失败)
```cmake
# 添加自定义查找路径
set(realsense2_DIR "/usr/local/lib/cmake/realsense2") # 替换为实际路径
find_package(realsense2 REQUIRED)
```
#### 验证安装:
```bash
# 检查包是否存在
ls /usr/include/librealsense2/rs.h # Linux
dir "C:/Program Files/Intel RealSense SDK 2.0/include" # Windows
```
---
### 三、解决add_subdirectory目录错误
**问题现象**
`add_subdirectory Thirdparty/g2o error (目录不存在)`
**解决方案**
#### 步骤1:检查目录结构
```bash
project_root/
├── CMakeLists.txt
└── Thirdparty/
└── g2o/ # 确认此目录存在
├── CMakeLists.txt
└── ...
```
#### 步骤2:修复路径引用
```cmake
# 正确写法(相对路径)
add_subdirectory(Thirdparty/g2o)
# 或使用绝对路径
add_subdirectory(${CMAKE_SOURCE_DIR}/Thirdparty/g2o)
```
#### 步骤3:自动克隆依赖(推荐)
```cmake
# 在CMakeLists.txt中添加自动下载
include(FetchContent)
FetchContent_Declare(
g2o
GIT_REPOSITORY https://github.com/RainerKuemmerle/g2o.git
GIT_TAG 20230223_git # 指定稳定版本
)
FetchContent_MakeAvailable(g2o)
```
#### 步骤4:链接库文件
```cmake
target_link_libraries(your_target PRIVATE
g2o_core g2o_stuff # 按需添加具体模块
)
```
---
### 完整问题修复流程
1. **更新CMake版本声明**
2. **安装realsense2并配置CMake路径**
3. **验证g2o目录结构或添加自动下载**
4. 清理构建缓存:
```bash
rm -rf build/ # Linux
rmdir /s /q build # Windows
```
5. 重新生成项目:
```bash
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
```
> **重要提示**:若使用conda环境(如引用[2]),需激活环境后再编译:
> ```bash
> conda activate pytorch-build
> pip install pyyaml # 解决No module named 'yaml'[^2]
> ```
---
### 相关问题
1. 如何配置CMake使用vcpkg管理的第三方库?
2. 当CMake找不到自定义.cmake文件时如何解决?
3. 如何调试CMake中的target_link_libraries链接错误?
4. 在Windows上如何正确设置realsense2的环境变量?
5. CMake的FetchContent和ExternalProject有什么区别?
[^1]: CMake模块化管理和版本兼容性控制
[^2]: Conda环境配置与Python依赖解决
阅读全文
相关推荐

















