改写paddledetection为cmake版(c++)

下载源代码

官方地址:
https://gitee.com/paddlepaddle/PaddleDetection

网盘:
paddledetection
链接:https://pan.baidu.com/s/1g0z5SYQNDR1pwe9iAtvR3A?pwd=ktl6 
提取码:ktl6
paddleocr
链接:https://pan.baidu.com/s/1QcLbGJD7NB9UVPbUAulCuA?pwd=o68i 
提取码:o68i

找到c++部署代码

改写paddledetection为cmake版(c++)_第1张图片
改写paddledetection为cmake版(c++)_第2张图片
我个人为了整体性把src文件里的后缀都改为了cpp
改写paddledetection为cmake版(c++)_第3张图片

改CMakeLists.txt文件

原版cmake文件很繁琐,我改写了一个简洁版的
移植的时候只需要改头文件地址和库文件地址即可

cmake_minimum_required(VERSION 3.5)

set(CMAKE_BUILD_TYPE "Release")   #option: Debug / Release

if (CMAKE_BUILD_TYPE MATCHES "Debug" 
        OR CMAKE_BUILD_TYPE MATCHES "None")
    message(STATUS "CMAKE_BUILD_TYPE is Debug")
elseif (CMAKE_BUILD_TYPE MATCHES "Release")
    message(STATUS "CMAKE_BUILD_TYPE is Release")
endif()

# 设置c++标准
set(CMAKE_CXX_STANDARD 20)
project(DsYolo)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

option(WITH_MKL        "Compile demo with MKL/OpenBlas support, default use MKL."       ON)
option(WITH_GPU        "Compile demo with GPU/CPU, default use CPU."                    ON)
option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static."   ON)
option(WITH_TENSORRT "Compile demo with TensorRT."   ON)

macro(safe_set_static_flag)
    foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
      if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
      endif(${flag_var} MATCHES "/MD")
    endforeach(flag_var)
endmacro()

if (WITH_MKL)
    ADD_DEFINITIONS(-DUSE_MKL)
endif()

if (MSVC)
    add_definitions(-w)
    #add_definitions(-W0)
endif()

if (WIN32)
    add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
    set(CMAKE_C_FLAGS /source-charset:utf-8)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
    if(WITH_MKL)
        set(FLAG_OPENMP "/openmp")
    endif()
    set(CMAKE_C_FLAGS_DEBUG   "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
    set(CMAKE_C_FLAGS_RELEASE  "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
    set(CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
    set(CMAKE_CXX_FLAGS_RELEASE   "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
    if (WITH_STATIC_LIB)
        safe_set_static_flag()
        add_definitions(-DSTATIC_LIB)
    endif()
    message("cmake c debug flags " ${CMAKE_C_FLAGS_DEBUG})
    message("cmake c release flags " ${CMAKE_C_FLAGS_RELEASE})
    message("cmake cxx debug flags " ${CMAKE_CXX_FLAGS_DEBUG})
    message("cmake cxx release flags " ${CMAKE_CXX_FLAGS_RELEASE})
endif()


# 头文件
include_directories(${PROJECT_SOURCE_DIR}/include)
#include_directories(/home/nvidia/paddleOCR/PaddleOCR-release-2.6/deploy/cpp_infer/include)
#include_directories(/home/nvidia/paddleOCR/PaddleOCR-release-2.6/deploy/cpp_infer)
#include_directories(/usr/include)
include_directories(./)
include_directories(./include)
include_directories(./Ds_inference/opencv410/include/opencv4)
include_directories(./Ds_inference/opencv410/include/opencv4/opencv2)
include_directories(./Ds_inference/third_party/install/mklml/include)
include_directories(./Ds_inference/third_party/install/mkldnn/include)
include_directories(./Ds_inference/third_party/install/glog/include)
include_directories(./Ds_inference/third_party/AutoLog-main)
include_directories(./Ds_inference/third_party/install/gflags/include)
include_directories(./Ds_inference/third_party/install/protobuf/include)
include_directories(./Ds_inference/third_party/threadpool)
include_directories(./Ds_inference/third_party/yaml-cpp)
include_directories(./Ds_inference/paddle_gpu11.6/include)
# 库文件
#link_directories(/usr/lib)
link_directories(./Ds_inference/third_party/install/mklml/lib)
link_directories(./Ds_inference/third_party/install/mkldnn/lib)
link_directories(./Ds_inference/third_party/install/glog/lib)
link_directories(./Ds_inference/opencv410/lib)
link_directories(./Ds_inference/third_party/install/protobuf/lib)
link_directories(./Ds_inference/third_party/install/gflags/lib)
link_directories(./Ds_inference/paddle_gpu11.6/lib)
link_directories(./Ds_inference/third_party/yaml-cpp/lib)


aux_source_directory (src SRC_LIST)
add_executable (yoloV5 ${SRC_LIST})

set(LIBRARY_OUTPUT_PATH  ${PROJECT_SOURCE_DIR}/bin)
set(output)

target_link_libraries(yoloV5
opencv_world410
#opencv_highgui opencv_core opencv_imgproc opencv_imgcodecs opencv_calib3d opencv_features2d opencv_videoio 
paddle_inference mklml libiomp5md mkldnn glog gflags_static libprotobuf libcmt shlwapi yaml-cpp
)

# 注意测试
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

(windows平台)创造一个文件夹将生成可执行文件需要的库放入

改写paddledetection为cmake版(c++)_第4张图片
改写paddledetection为cmake版(c++)_第5张图片
需要用到gpu,就需要事先配置好cuda这一套的环境变量,就不用在cmakelists里面专门写来引用了,配置方法见http://t.csdn.cn/mgIMD

编译的时候遇到的bug

1.xxx已经在obj中定义

在这里插入图片描述
这三个关于main的cpp中只能同事存在一个,看自己需要那个,将不需要的文件里面代码全部注释,,我是使用了main.cpp,反正遇到这种报错通常把一些东西注释掉就能解决

2.error C7555: … requires at least ‘/std:c++latest’ when build in Win10

意思是c++版本低了
在这里插入图片描述
找到cmakelists这句函数,然后我发现我之前写的是17,我改成20之后完美解决。

3.fatal error C1083: 无法打开包括文件: “yaml-cpp/yaml.h”: No such file or directory

其实源cmakelists中是套了一个include(cmake/yaml-cpp.cmake),来编译


find_package(Git REQUIRED)

include(ExternalProject)

message("${CMAKE_BUILD_TYPE}")

ExternalProject_Add(
        ext-yaml-cpp
        URL https://bj.bcebos.com/paddlex/deploy/deps/yaml-cpp.zip
        URL_MD5 9542d6de397d1fbd649ed468cb5850e6
        CMAKE_ARGS
        -DYAML_CPP_BUILD_TESTS=OFF
		-DYAML_CPP_BUILD_TOOLS=OFF
        -DYAML_CPP_INSTALL=OFF
        -DYAML_CPP_BUILD_CONTRIB=OFF
		-DMSVC_SHARED_RT=OFF
		-DBUILD_SHARED_LIBS=OFF
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
        -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
        -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
        -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/ext/yaml-cpp/lib
        -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/ext/yaml-cpp/lib
        PREFIX "${CMAKE_BINARY_DIR}/ext/yaml-cpp"
        # Disable install step
        INSTALL_COMMAND ""
	    LOG_DOWNLOAD ON
        LOG_BUILD 1
)

我还是想自己来编译,因为根据之前的经验,这种需要远程下载源码的总是有这样那样的问题。

下载源码

https://github.com/jbeder/yaml-cpp

改写paddledetection为cmake版(c++)_第6张图片
我以为只是缺少头文件,cmakelists里面补上之后
改写paddledetection为cmake版(c++)_第7张图片
出现无法解析的外部符号,大概率是缺少库,然后我去编译yaml-cpp的库。
改写paddledetection为cmake版(c++)_第8张图片
就是很常规的操作,用cmake … 直接就编译出了一个lib库,很顺利,但是使用的时候显示无法打开此lib
改写paddledetection为cmake版(c++)_第9张图片
我再重新生成找原因
改写paddledetection为cmake版(c++)_第10张图片
注意到这一句Defining YAML_CPP_API for DLL import,然后我搜索这个关键词,搜到了https://blog.csdn.net/m0_37833142/article/details/115180471,
即在cmake后多加了一个参数,就成功可以正常调用了,到底是什么原理,我还得继续学习

cmake -DYAML_BUILD_SHARED_LIBS=ON ..

改写paddledetection为cmake版(c++)_第11张图片

下载模型测试

代码报错查找,实例化

添加dll到exe目录

你可能感兴趣的:(c++,开发语言)