因为想用python3 + ros,
所以按照此处介绍的方法,得知cv_bridge是python2.7才支持的,
所以要用python3编译cv_bridge.
之前选了python3.8后踩坑
什么坑呢
注意到cakin配置时要用到python3.xm, 但是在网上查的说python3.8已经没有m了。。(就没有试下去)
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.socatkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
然后换成3.7,从源码安装
sudo apt update
sudo apt install build-essential zlib1g-dev libbz2-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar -xzvf Python-3.7.4.tgz
cd Python-3.7.4
./configure --prefix=/usr/local/python3.7 # 此处有坑,看第6条
sudo make
sudo make install
sudo ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3
sudo ln -s /usr/local/python3.7/bin/pip3.7 /usr/bin/pip3
sudo ln -s /usr/local/src/python37/bin/python3.7m /usr/bin/python3.7m
第2坑,没有python3.7-dev
这个直接跳过了
这几步都还好的
pip3 install rospkg
pip3 install catkin-tools
mkdir py3_ros_ws && cd py3_ros_ws
mkdir src && cd src
## 下载vision_opencv 库
git clone https://github.com/ros-perception/vision_opencv
git checkout kinetic #选择自己的ros版本
开始编译
cd py3_ros_ws
source /opt/ros/kinetic/setup.bash
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/local/python3.7/include/python3.7m
-DPYTHON_LIBRARY=/usr/local/python3.7/lib/libpython3.7m.a #此处有坑,后面会变成.so,看第6条
# 注意选择对应的python3版本
catkin config --install #此处有坑,看第8条
# 编译
catkin build cv_bridge
然后就开始报错,以下是曾经遇到的坑(不分先后)
ImportError: No module named ‘_ctypes’ when using Value from module multiprocessing
解决方法:
sudo apt-get install libffi-dev
2.找不到libboost_python.so
# locate libboost_python
/usr/lib/x86_64-linux-gnu/libboost_python-py27.a
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
/usr/lib/x86_64-linux-gnu/libboost_python-py35.a
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so.1.58.0
确实是没有叫libboost_python.so的,所以要自己做一个link
但是发现没有,这个只有python3.5的libboost_python, python3.7的去哪了?
解决方法:
安装boost,下载地址
我这里下载的是1.76的版本
注意了!ubuntu16的gcc版本是5.4,默认是不用c++11编译的,
所以需要打开 c++11 的支持, gcc5.4 下, 只打开 --std=c++11 是不能编译fiber的, 使用 --std=c++14 或者 --std=c++17 可以成功
cxxflags一定要写c++14或c++17, 不然后面用的时候会报错,很多函数都没有
参考链接
tar -xzf boost_1_76_0.tar.gz
cd boost_1_76_0
./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.7 --with-python-root=/usr/local/python3.7
./b2 cflags='-fPIC' cxxflags='--std-c++14' --with-python include="/usr/local/python3.7/include/python3.7m"
./b2 install
这样就安装好了,在/usr/local/lib
下,
不要copy到/usr/lib/x86_64-linux-gnu
下,不然用的时候会报warning还是error
cd /usr/lib/x86_64-linux-gnu
ln -s /usr/local/lib/libboost_python-py37.so libboost_python.so
ln -s /usr/local/lib/libboost_python-py37.a libboost_python.a
3.Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.
解决方法
sudo mv /usr/bin/lsb_release /usr/bin/lsb_release_bak
4.CMake Error at /usr/local/lib/cmake/Boost-1.76.0/BoostConfig.cmake:240 (if):
if given arguments:
"ALL" "IN_LIST" "Boost_FIND_COMPONENTS"
Unknown arguments specified
Call Stack (most recent call first):
/usr/share/cmake-3.5/Modules/FindBoost.cmake:245 (find_package)
CMakeLists.txt:9 (find_package)
解决方法:
参考链接
5./usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
解决方法:
修改vision_opencv下的CmakeLists.txt,添加c++11的支持
cd vision_opencv/
find ./ -name CMakeList*
在每一个(除了python)的CmakeLists.txt里添加SET(CMAKE_CXX_FLAGS "-std=c++0x")
6.`_Py_NotImplementedStruct’ can not be used when making a shared object; recompile with -fPIC
这个很坑
为什么,因为要重新编译python3.7!
刚刚编译python时,仅用了./configure --prefix=/usr/local/python3.7
,并没有fPIC模式
重新编译吧
./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC --enable-optimizations
make
make install
make完之后你会发现/usr/local/python3.7/lib下多了libpython3.7m.so(之前只有.a)
重新配置
cd py3_ros_ws
source /opt/ros/kinetic/setup.bash
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/local/python3.7/include/python3.7m
-DPYTHON_LIBRARY=/usr/local/python3.7/lib/libpython3.7m.so
7./usr/bin/python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory
此坑是上一坑的延续版,
上一坑python重新编译后,再build就出现此问题
解决方法:
添加环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python3.7/lib
8.error: option --install-layout not recognized
上一坑的延续
需要把catkin config --install
改为
catkin config --install -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF
catkin build cv_bridge
build成功。
为避免还用之前python2.7的cv_bridge, 移除之前的(也可以不移除,这里为了测试python3的能不能用)
cd /opt/ros/kinetic/lib/python2.7/dist-packages
mv cv_bridge cv_bridge_BK
python3的cv_bridge库安装在py3_ros_ws的install文件夹,需要使用的时候将这个环境变量导入即可
source devel/setup.bash --extend
roslaunch以后提示No module named ‘cv_bridge’
找到使用cv_bridge的python文件,
第一行改为
!/usr/bin/env python3
指明使用python3
记住要添加环境变量,不然又找不到libpython3.7m.so.1.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python3.7/lib
然后ros就可以使用python3啦(之前python2.7下安装的要在python3下重新安装)
参考链接:
https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
https://www.jianshu.com/p/2f86607c98d1