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 条评论
登录 后参与评论

相关文章

来自专栏PHP技术

apache服务器配置

昨天弄了个PHP小程序,想在本地跑一下测试,可是工作电脑没有安装环境,于是下载了一个wamp,一路畅通,Apache、Mysql、PHP就 全有了。启动wamp...

2916
来自专栏gaoqin31

基于php+shell的发布系统

  一个比较完善的发布系统首先肯定是要能发布文件,其次当发布出现问题时要能支持撤销,避免长时间无法解决问题影响产品的正常运营。 对于发布脚本语言如PHP,she...

632
来自专栏醉程序

一个校园类微信公众平台

1093
来自专栏Web 开发

申请SSL开启SPDY服务

12月10日的时候,整个站点就已经装上了Nginx 1.4,配置文件里面已经可以开启SPDY功能,不过那时候一直缺少一枚SSL证书。

480
来自专栏搜云库

使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上

一个用于构建和推送Docker镜像的Maven插件。

1938
来自专栏杨建荣的学习笔记

容灾切换中的数据库宕机问题简单分析(一) (r9笔记第12天)

最近对一个统计库做了计划内的容灾切换,即主备切换。操作的过程其实还是蛮顺利的。但是灾难切换中如果出现在问题,那就是灾难中的灾难了。 按照计划对配置信息做了同步,...

3096
来自专栏吴小龙同學

Android之emulator ERROR x86 emulation currently requires hardware acceleration

一直在用Genymotion模拟器,话说Android studio将出2.0,速度提速50倍,这是要断Genymotion的路,于是我在家里电脑运行AVD,却...

7045
来自专栏Laoqi's Linux运维专列

Kubernetes 1.8.6 集群部署–部署Web UI界面(七)

3116
来自专栏Danny的专栏

【MyEclipse】——MyEclipse10配置Tomcat7+Web项目发布

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huyuyang6688/article/...

843
来自专栏KK的小酒馆

利用Gradle 的Kotlin DSL 搭建Tomcat项目(我是新手啊)

为什么都是红的…………有时候等一会儿就不红了,有时候,需要关闭IDEA再启动,等Indexing,重建目录后才OK,Build Project啊,gradlew...

904

扫码关注云+社区