FAILED: CMakeFiles/gazebo_aruco_plugin.dir/src/gazebo_aruco_plugin.cpp.o /usr/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_IOSTREAMS_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_TEST_DYN_LINK -DBOOST_THREAD_DYN_LINK -DLIBBULLET_VERSION=3.05 -DLIBBULLET_VERSION_GT_282 -Dgazebo_aruco_plugin_EXPORTS -I/home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/include -I/home/yxz/PX4_Firmware/build/px4_sitl_default/build_gazebo-classic -I/usr/include/eigen3/eigen3 -I/usr/include/gazebo-11/gazebo/msgs -I/home/yxz/PX4_Firmware/build/px4_sitl_default/mavlink -I/home/yxz/PX4_Firmware/build/px4_sitl_default/mavlink/mavlink/v2.0 -I/home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/external/OpticalFlow/include -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /usr/include/gazebo-11 -isystem /usr/include/bullet -isystem /usr/include/simbody -isystem /usr/include/sdformat-9.7 -isystem /usr/include/ignition/math6 -isystem /usr/include/OGRE -isystem /usr/include/OGRE/Terrain -isystem /usr/include/OGRE/Paging -isystem /usr/include/ignition/transport8 -isystem /usr/include/ignition/msgs5 -isystem /usr/include/ignition/common3 -isystem /usr/include/ignition/fuel_tools4 -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -isystem /usr/include/eigen3 -isystem /usr/local/include/opencv4 -isystem /usr/include/sdformat-9.7/sdf/.. -isystem /usr/include/ignition/cmake2 -isystem /usr/include/uuid -O2 -g -DNDEBUG -fPIC -Wno-deprecated-declarations -Wno-address-of-packed-member -I/usr/include/uuid -MD -MT CMakeFiles/gazebo_aruco_plugin.dir/src/gazebo_aruco_plugin.cpp.o -MF CMakeFiles/gazebo_aruco_plugin.dir/src/gazebo_aruco_plugin.cpp.o.d -o CMakeFiles/gazebo_aruco_plugin.dir/src/gazebo_aruco_plugin.cpp.o -c /home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/src/gazebo_aruco_plugin.cpp In file included from /home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/src/gazebo_aruco_plugin.cpp:31: /home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/include/gazebo_aruco_plugin.h:41: fatal error: opencv2/aruco.hpp: No such file or directory 41 | #include <opencv2/aruco.hpp> | compilation terminated. [72/142] Building CXX object CMakeFile...erface.dir/src/mavlink_interface.cpp.o In file included from /usr/include/boost/smart_ptr/detail/sp_thread_sleep.hpp:22, from /usr/include/boost/smart_ptr/detail/yield_k.hpp:23, from /usr/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp:14, from /usr/include/boost/smart_ptr/detail/spinlock.hpp:42, from /usr/include/boost/smart_ptr/detail/spinlock_pool.hpp:25, from /usr/include/boost/smart_ptr/shared_ptr.hpp:29, from /usr/include/boost/shared_ptr.hpp:17, from /usr/include/boost/date_time/time_clock.hpp:17, from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:10, from /usr/include/boost/asio/time_traits.hpp:23, from /usr/include/boost/asio/detail/timer_queue_ptime.hpp:22, from /usr/include/boost/asio/detail/deadline_timer_service.hpp:29, from /usr/include/boost/asio/basic_deadline_timer.hpp:25, from /usr/include/boost/asio.hpp:25, from /home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/include/mavlink_interface.h:36, from /home/yxz/PX4_Firmware/Tools/simulation/gazebo-classic/sitl_gazebo-classic/src/mavlink_interface.cpp:1: /usr/include/boost/bind.hpp:36:1: note: ‘#pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.’ 36 | BOOST_PRAGMA_MESSAGE( | ^~~~~~~~~~~~~~~~~~~~ [77/142] Building CXX object CMakeFile...src/gazebo_camera_manager_plugin.cpp.o ninja: build stopped: subcommand failed. [899/903] Linking CXX executable bin/px4 FAILED: external/Stamp/sitl_gazebo-classic/sitl_gazebo-classic-build /home/yxz/PX4_Firmware/build/px4_sitl_default/external/Stamp/sitl_gazebo-classic/sitl_gazebo-classic-build cd /home/yxz/PX4_Firmware/build/px4_sitl_default/build_gazebo-classic && /usr/bin/cmake --build /home/yxz/PX4_Firmware/build/px4_sitl_default/build_gazebo-classic -- -j 7 ninja: build stopped: subcommand failed. make: *** [Makefile:227: px4_sitl_default] Error 1
时间: 2025-07-19 14:26:19 浏览: 4
<think>我们正在解决一个编译错误:在编译gazebo_aruco_plugin时出现opencv2/aruco.hpp头文件缺失的问题。
根据错误信息,可能是由于系统中安装的OpenCV版本不包含aruco模块,或者OpenCV的路径没有正确设置。
步骤分析:
1. 确认系统中安装的OpenCV版本是否包含aruco模块。
注意:aruco模块在OpenCV 3.0以上版本中提供,但需要安装opencv_contrib模块。在OpenCV 4.0中,aruco模块被移到了主仓库。
2. 如果系统中没有安装合适版本的OpenCV,则需要安装包含aruco模块的OpenCV。
3. 如果已经安装,则需要确保CMake能够找到正确的OpenCV路径。
参考引用[1]中提到,如果编译新版本OpenCV有问题,可以切换到2.4分支。但注意,aruco模块在2.4版本中不存在,因此我们需要的是OpenCV 3.x或4.x。
另外,引用[2]中提到了一些CMake变量,虽然不推荐,但我们可以尝试在CMakeLists.txt中设置OpenCV的路径。
解决方案:
步骤1:检查OpenCV版本和aruco模块是否存在
可以编写一个简单的程序来检查:
```cpp
#include <opencv2/aruco.hpp>
int main() {
return 0;
}
```
编译这个程序,看是否能找到头文件。如果找不到,说明OpenCV安装不包含aruco。
步骤2:安装包含aruco模块的OpenCV(如果尚未安装)
对于OpenCV 3.x,需要同时安装opencv和opencv_contrib,并在编译时设置OPENCV_EXTRA_MODULES_PATH为opencv_contrib/modules。
对于OpenCV 4.x,aruco模块已经包含在主要模块中,但可能仍然需要安装opencv-contrib-package(根据不同的安装方式)。
在Ubuntu上,可以通过以下命令安装(以OpenCV 4为例):
```bash
sudo apt install libopencv-dev
# 如果上述命令安装的版本不包含aruco,可以尝试安装额外的包或者从源码编译
```
如果使用apt安装的版本不包含aruco,则需要从源码编译安装OpenCV。
从源码编译OpenCV 4.x的步骤(简化):
1. 安装依赖:
```bash
sudo apt update
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python3-dev python3-numpy
# 其他依赖根据需要安装
```
2. 下载源码:
```bash
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
```
3. 切换到稳定版本(例如4.5.5):
```bash
cd opencv
git checkout 4.5.5
cd ../opencv_contrib
git checkout 4.5.5
```
4. 创建构建目录并配置:
```bash
cd ../opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
```
5. 编译并安装:
```bash
make -j$(nproc)
sudo make install
```
步骤3:在gazebo_aruco_plugin的CMakeLists.txt中指定OpenCV路径
如果系统中安装了多个OpenCV版本,需要确保CMake使用正确的版本。
可以在CMakeLists.txt中添加:
```cmake
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(your_target ${OpenCV_LIBS})
```
如果CMake无法自动找到,可以手动指定OpenCV_DIR:
```cmake
set(OpenCV_DIR "/path/to/opencv/build") # 例如:/usr/local/share/OpenCV
```
步骤4:清理并重新编译gazebo_aruco_plugin
在修改CMakeLists.txt后,需要重新运行cmake和make。
另外,引用[3]中提到了网络问题,如果编译过程中需要下载依赖,确保网络正常。
如果问题仍然存在,请检查错误日志,确认是否因为多个OpenCV版本冲突。如果系统中存在旧版本OpenCV,可能需要先卸载(如引用[1]所述)。
例如,卸载旧版本OpenCV(如果之前通过apt安装):
```bash
sudo apt purge libopencv*
```
然后重新安装或编译所需版本的OpenCV。
注意:在编译gazebo_aruco_plugin时,确保使用的OpenCV版本与插件要求的版本兼容。
相关问题:
1. 如何检查系统中安装的OpenCV版本?
2. 如何从源码编译安装OpenCV并包含contrib模块?
3. 在CMake中如何指定使用特定版本的OpenCV?
4. 如何解决多个OpenCV版本冲突的问题?
5. gazebo_aruco_plugin对OpenCV版本的具体要求是什么?</think>### 解决OpenCV头文件缺失问题:`opencv2/aruco.hpp`找不到
#### 问题原因分析
1. **OpenCV版本不匹配**:`aruco`模块在OpenCV 3.0+才被引入(需额外安装`opencv_contrib`),OpenCV 4.x中已集成到主库[^1]。
2. **头文件路径缺失**:CMake未正确配置OpenCV包含路径。
3. **多版本冲突**:系统存在旧版OpenCV(如2.x),导致CMake优先调用旧版本[^1]。
---
### 解决方案
#### 步骤1:安装兼容的OpenCV版本
```bash
# 卸载旧版OpenCV(避免冲突)
sudo apt purge libopencv*
# 安装OpenCV 4.x(含aruco模块)
sudo apt install libopencv-dev libopencv-contrib-dev
```
> 验证安装:`pkg-config --modversion opencv4` 应显示4.x版本
#### 步骤2:配置CMakeLists.txt
在`gazebo_aruco_plugin`的CMake文件中添加:
```cmake
# 强制指定OpenCV 4路径
set(OpenCV_DIR "/usr/share/opencv4") # 根据实际路径调整
find_package(OpenCV 4 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(your_target_name ${OpenCV_LIBS})
```
#### 步骤3:重新编译
```bash
cd ~/catkin_ws
rm -rf build devel # 清理旧编译
catkin_make -DCMAKE_BUILD_TYPE=Release
```
#### 备选方案:源码编译OpenCV(若apt版本不兼容)
```bash
git clone https://github.com/opencv/opencv_contrib.git
cmake -DOPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules ..
make -j$(nproc)
sudo make install
```
---
### 关键验证点
1. 检查头文件是否存在:`ls /usr/include/opencv4/opencv2/aruco.hpp`
2. 在CMake输出中确认找到正确版本:
```txt
-- Found OpenCV: /usr (found version "4.5.4")
```
> **注意**:ROS环境可能覆盖OpenCV路径,建议在`~/.bashrc`中显式导出:
> ```bash
> export OpenCV_DIR="/usr/share/opencv4"
> ```
阅读全文
相关推荐


















