专栏首页10km的专栏linux下用cmake对caffe静态编译时-static-libstdc++参数无效的问题

linux下用cmake对caffe静态编译时-static-libstdc++参数无效的问题

以下是用于cmake 生成 Makefile文件对Caffe进行静态库连接编译的shell脚本,

#!/bin/bash
# cmake 静态编译 caffe-ssd 代码脚本
# author guyadong@gdface.net
shell_folder=$(cd "$(dirname "$0")";pwd)
. $shell_folder/build_funs
. $shell_folder/build_vars
# 项目安装路径
install_path=$SSD_INSTALL_PATH
echo install_path:$install_path
# gflags 安装路径
gflags_root=$GFLAGS_INSTALL_PATH
exit_if_not_exist "$gflags_root/lib/libgflags.a" "not found $gflags_root/lib/libgflags.a,please build $GFLAGS_PREFIX"
# glog 安装路径
glog_root=$GLOG_INSTALL_PATH
exit_if_not_exist "$glog_root/lib/libglog.a" "not found $glog_root/lib/libglog.a,please build $GLOG_PREFIX"
# hdf5 cmake 位置  
hdf5_cmake_dir=$HDF5_INSTALL_PATH/share/cmake
exit_if_not_exist $hdf5_cmake_dir "not found $hdf5_cmake_dir,please build $HDF5_PREFIX"
exit_if_not_exist $BOOST_INSTALL_PATH "not found $BOOST_INSTALL_PATH,please build $BOOST_PREFIX"
exit_if_not_exist $OPENBLAS_INSTALL_PATH "not found $OPENBLAS_INSTALL_PATH,please build $OPENBLAS_PREFIX"
exit_if_not_exist $PROTOBUF_INSTALL_PATH "not found $PROTOBUF_INSTALL_PATH,please build $PROTOBUF_PREFIX"
# protobuf lib 路径,
# centos下安装路径可能是lib64
[ -e "$PROTOBUF_INSTALL_PATH/lib" ] && protobuf_lib=$PROTOBUF_INSTALL_PATH/lib
[ -e "$PROTOBUF_INSTALL_PATH/lib64" ] && protobuf_lib=$PROTOBUF_INSTALL_PATH/lib64
exit_if_not_exist $SNAPPY_INSTALL_PATH "not found $SNAPPY_INSTALL_PATH,please build $SNAPPY_PREFIX"
# lmdb 安装路径根目录
lmdb_install_root=$LMDB_INSTALL_PATH/usr/local
exit_if_not_exist $lmdb_install_root "not found $lmdb_install_root,please build lmdb"
exit_if_not_exist $LEVELDB_INSTALL_PATH "not found $LEVELDB_INSTALL_PATH,please build $LEVELDB_PREFIX"
# opencv 配置文件(OpenCVConfig.cmake)所在路径
opencv_cmake_dir=$OPENCV_INSTALL_PATH/share/OpenCV
exit_if_not_exist $opencv_cmake_dir "not found $opencv_cmake_dir,please build $OPENCV_PREFIX"

pushd $SOURCE_ROOT/$SSD_FOLDER
clean_folder build.gcc
#mkdir_if_not_exist build.gcc
pushd build.gcc
# 指定 OpenBLAS 安装路径 参见 $caffe_source/cmake/Modules/FindOpenBLAS.cmake
export OpenBLAS_HOME=$OPENBLAS_INSTALL_PATH
# 指定 lmdb 安装路径 参见 $caffe_source/cmake/Modules/FindLMDB.cmake.cmake
export LMDB_DIR=$lmdb_install_root
# 指定 leveldb 安装路径 参见 $caffe_source/cmake/Modules/FindLevelDB.cmake.cmake
export LEVELDB_ROOT=$LEVELDB_INSTALL_PATH
# GLOG_ROOT_DIR 参见 $caffe_source/cmake/Modules/FindGlog.cmake
# GFLAGS_ROOT_DIR 参见 $caffe_source/cmake/Modules/FindGFlags.cmake
# HDF5_ROOT 参见 https://cmake.org/cmake/help/v3.8/module/FindHDF5.html
# BOOST_ROOT,Boost_NO_SYSTEM_PATHS 参见 https://cmake.org/cmake/help/v3.8/module/FindBoost.html
# SNAPPY_ROOT_DIR 参见 $caffe_source/cmake/Modules/FindSnappy.cmake
# PROTOBUF_LIBRARY,PROTOBUF_PROTOC_LIBRARY... 参见 https://cmake.org/cmake/help/v3.8/module/FindProtobuf.html
# OpenCV_DIR 参见https://cmake.org/cmake/help/v3.8/command/find_package.html
$CMAKE_EXE "$(dirs +1)" $CMAKE_VARS_DEFINE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$install_path \
    -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc" \
    -DGLOG_ROOT_DIR=$glog_root \
    -DGFLAGS_ROOT_DIR=$gflags_root \
    -DHDF5_ROOT=$HDF5_INSTALL_PATH \
    -DBOOST_ROOT=$BOOST_INSTALL_PATH \
    -DBoost_NO_SYSTEM_PATHS=on \
    -DSNAPPY_ROOT_DIR=$SNAPPY_INSTALL_PATH \
    -DOpenCV_DIR=$opencv_cmake_dir \
    -DPROTOBUF_LIBRARY=$protobuf_lib/libprotobuf.a \
    -DPROTOBUF_PROTOC_LIBRARY=$protobuf_lib/libprotoc.a \
    -DPROTOBUF_LITE_LIBRARY=$protobuf_lib/libprotobuf-lite.a \
    -DPROTOBUF_PROTOC_EXECUTABLE=$PROTOBUF_INSTALL_PATH/bin/protoc \
    -DPROTOBUF_INCLUDE_DIR=$PROTOBUF_INSTALL_PATH/include \
    -DCPU_ONLY=ON \
    -DBLAS=Open \
    -DBUILD_SHARED_LIBS=off \
    -DBUILD_docs=off \
    -DBUILD_python=off \
    -DBUILD_python_layer=off \
    -DUSE_LEVELDB=on \
    -DUSE_LMDB=on \
    -DUSE_OPENCV=on 
exit_on_error
remove_if_exist $install_path
make -j $MAKE_JOBS install
exit_on_error
popd
rm -fr build.gcc
popd

在脚本中,调用cmake生成Makefile时,添加了-DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc"参数用于指定 将libstdc++,libgcc以静态库形式连接,然而在实际使用过程发现当指定 USE_OPENCV=on(即使用OpenCV,opencv已经预先做了静态库编译)时,编译出的caffe,用ldd查看还是会依赖libstdc++.so和libgcc.so,也就是说-static-libstdc++ -static-libgcc无效了。。。 USE_OPENCV=off时则能正常静态连接。

这个问题困扰了几天,后来通过比较<project>.dir下的link.txt(cmake生成的),发现,当USE_OPENCV=on时生成的link.txt中,自动在opencv静态库加了-lstdc++ 参数。。。 如下是caffe.bin项目link.txt(./tools/CMakeFiles/caffe.bin.dir/link.txt),太长我加了分行符:

/usr/local/bin/g++   -fPIC -Wall -Wno-sign-compare -Wno-uninitialized -O3 -DNDEBUG  -static-libstdc++ -static-libgcc -rdynamic CMakeFiles/caffe.bin.dir/caffe.cpp.o  -o caffe -Wl,-rpath,:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -Wl,--whole-archive ../lib/libcaffe.a -Wl,--no-whole-archive ../lib/libproto.a 
/home/hadoop/tmp/dl/depends/release/boost_linux_x86_64/lib/libboost_system.a 
/home/hadoop/tmp/dl/depends/release/boost_linux_x86_64/lib/libboost_thread.a 
/home/hadoop/tmp/dl/depends/release/boost_linux_x86_64/lib/libboost_filesystem.a 
/home/hadoop/tmp/dl/depends/release/boost_linux_x86_64/lib/libboost_regex.a -lpthread 
/home/hadoop/tmp/dl/depends/release/glog_linux_x86_64/lib/libglog.a 
/home/hadoop/tmp/dl/depends/release/gflags_linux_x86_64/lib/libgflags.a 
/home/hadoop/tmp/dl/depends/release/protobuf_linux_x86_64/lib64/libprotobuf.a -lpthread 
/home/hadoop/tmp/dl/depends/release/glog_linux_x86_64/lib/libglog.a 
/home/hadoop/tmp/dl/depends/release/gflags_linux_x86_64/lib/libgflags.a 
/home/hadoop/tmp/dl/depends/release/protobuf_linux_x86_64/lib64/libprotobuf.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_cpp.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_hl_cpp.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_hl.a /home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_cpp.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_hl_cpp.a 
/home/hadoop/tmp/dl/depends/release/hdf5_linux_x86_64/lib/libhdf5_hl.a 
/home/hadoop/tmp/dl/depends/release/lmdb_linux_x86_64/usr/local/lib/liblmdb.a 
/home/hadoop/tmp/dl/depends/release/leveldb_linux_x86_64/lib/libleveldb.a 
/home/hadoop/tmp/dl/depends/release/snappy_linux_x86_64/lib/libsnappy.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/lib/libopencv_highgui.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/liblibjpeg.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/liblibpng.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/liblibtiff.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/liblibjasper.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/libIlmImf.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/lib/libopencv_imgproc.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/lib/libopencv_core.a 
/home/hadoop/tmp/dl/depends/release/opencv_linux_x86_64/share/OpenCV/3rdparty/lib/libzlib.a 
-lstdc++ -lm -lpthread -lrt 
/home/hadoop/tmp/dl/depends/release/OpenBLAS_linux_x86_64/lib/libopenblas.a 
-ldl -pthread -lrt 

就是这尾部的-lstdc++参数导致-static-libstdc++ -static-libgcc无效,尝试手工删除 -lstdc++,则编译通过,stdc++,libgcc都能静态连接进来了。于是果然在cmake生成Makefile后,添加了如下代码,则问题解决:

# 修改所有 link.txt 删除-lstdc++ 选项,保证静态连接libstdc++库,否则在USE_OPENCV=on的情况下,libstdc++不会静态链接
for file in $(find . -name link.txt)
do 
    echo "modifing file: $file"
    #sed -i -r "s/-lstdc\+\+/ /g" $file
done

静态编译后,ldd 查看bin下的caffe依赖关系显示如下,除了系统库之外,不再依赖任何其他动态库,文件大小也达到28MB。

[hadoop@t2-centos6 bin]$ ldd ../release/caffe-ssd_linux_x86_64/bin/caffe linux-vdso.so.1 => (0x00007fff8fb58000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c24600000) librt.so.1 => /lib64/librt.so.1 (0x0000003c24e00000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003c24a00000) libm.so.6 => /lib64/libm.so.6 (0x0000003c25200000) libc.so.6 => /lib64/libc.so.6 (0x0000003c24200000) /lib64/ld-linux-x86-64.so.2 (0x0000003c23e00000)

但是为什么opencv的库会导致这个问题。一直没搞明白。

本文贴出的脚本并不完整 关于caffe静态编译的完整脚本,请从从csdn CODE获取: https://code.csdn.net/10km/caffe-static

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • minigui:解决gvfb编译报错undefined reference to symbol 'XkbGetIndicatorState'

    版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net...

    用户1148648
  • minigui:静态编译连接mgncs库时遇到的xml2的问题

    版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net...

    用户1148648
  • guava:java:java.util.Map和java.util.Set的Key类型转换

    昨天写了一博客《java:java.util.Map和java.util.Set的Key类型转换》,主要是想实现以java.util.MapKey类型转换,今天...

    用户1148648
  • C++17使用std::apply和fold expression对tuple进行遍历

    先来看这个std::apply函数,这个函数定义在tuple头文件中,函数签名如下:

    卡尔曼和玻尔兹曼谁曼
  • Flutter 在macOS的安装步骤

    Flutter至今为止,还是个比较新的跨平台方案,不过既然是谷歌推出的,我们空余时间就来研究下

    爱因斯坦福
  • Docker系列教程24-Docker Compose网络设置

    用户1516716
  • Android Wear 开发初探

    Android Wear从2014年3月发布到现在已经从1.0发展到2.0(目前还没正式发布)。其产品定位也发化了巨大变化,因为Android Wear 1.0...

    QQ音乐技术团队
  • 【新版典型案例】如何使用智能钛TI-ONE轻松解决银行场景下的营销推荐问题

    营销推荐能帮助企业根据其经营策略和用户历史情况,对企业产品的目标用户进行更加精准的定位,预估企业产品能向哪些客户进行介绍,哪些用户的购买率更高。这样的推荐销售不...

    腾讯智能钛AI开发者
  • 因谈判破裂,意大利60万用户邮箱数据在暗网被出售!

    “又是比特币勒索!“黑客一次次给予Email.it公司机会”,然后谈判破裂后,被激怒的黑客愤而把这一窃取的数据公开在暗网出售。

    数据猿
  • 为什么Netty这么火?与Mina相比有什么优势?

    Netty是什么?为什么这么火? Netty是目前最流行的由JBOSS提供的一个Java开源框架NIO框架,Netty提供异步的、事件驱动的网络应用程序框架和工...

    Java技术栈

扫码关注云+社区

领取腾讯云代金券