linux基于libtorch框架 c++工程 调用opencv库,完成打包,生成.so库,并调用
一. 生成.so库- c++工程目录
我的c++工程myProject目录结构如下:
1. include -----det.h -----ut.h 2. src -----det.cpp -----main.cpp 3. build 4. CMakelists.txt
- 编写CMakeLists.txt
cmake_minimum_required(VERSION 3.5.1)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
# Try to find OpenCV
# set(OpenCV_DIR ....)
find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}" n)
else ()
message(FATAL_ERROR "Could not locate OpenCV" n)
endif()
set(Torch_DIR /home/xxx/Downloads/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
include_directories(OpenCV_INCLUDE_DIRS)
include_directories(Torch_INCLUDE_DIRS)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/dnn/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/videoio/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/imgcodecs/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/highgui/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/imgproc/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/build)
include_directories(/home/xxx/Downloads/opencv/opencv-master/modules/core/include)
include_directories(/home/xxx/Downloads/opencv/opencv-master/include)
include_directories(/home/xxx/Downloads/libtorch/include)
include_directories(/home/xxx/Downloads/libtorch/include/torch/csrc/api/include)
file(GLOB SOURCE_FILES src/*.cpp)
add_library(mylib SHARED src/det.cpp include/det.h include/ut.h)
ADD_LIBRARY (mylib_static STATIC src/det.cpp include/det.h include/ut.h)
target_link_libraries (
${CMAKE_PROJECT_NAME}
${OpenCV_LIBS}
${TORCH_LIBRARIES}
)
其中总会出现找不到opencv、libtorch的一些头文件,我这里找到头文件的绝对路径,用include_directories()加进去就可以了。
- 执行命令生成
在工程目录myProject下,执行一下命令,生成.so文件和.a文件
mkdir build && cd build cmake .. make二. 调用.so库
- 调用.so库的c++的工程目录
将刚才生成的.so文件复制的lib文件夹下。
我的调用.so库的c++工程myProject2目录结构如下:
1. include -----det.h -----ut.h 2. src -----det.cpp -----main.cpp 3. build 4. lib -----mylib.so -----mylib.a 5. CMakelists.txt
- 编写CMakeLists.txt
cmake_minimum_required(VERSION 3.5.1)
project(myProject2)
set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
# Try to find OpenCV
# set(OpenCV_DIR ....)
find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}" n)
else ()
message(FATAL_ERROR "Could not locate OpenCV" n)
endif()
set(Torch_DIR /home/xxx/Downloads/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
include_directories(OpenCV_INCLUDE_DIRS)
include_directories(Torch_INCLUDE_DIRS)
include_directories(${PROJECT_SOURCE_DIR}/include)
set(LIB_DIR /home/xxx/vsProjects/myProject2/lib)
#链接库路径
link_directories(${LIB_DIR})
file(GLOB SOURCE_FILES src/*.cpp)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries (
${CMAKE_PROJECT_NAME}
${OpenCV_LIBS}
${TORCH_LIBRARIES}
${PROJECT_SOURCE_DIR}/lib/mylib.so
)
- 执行命令生成
在工程目录myProject2下,执行一下命令,生成.so文件和.a文件
mkdir build && cd build cmake .. make ./myProject2
- 各个 .cpp 和 .h文件示例
ut.h 文件
#pragma once
enum Det {
x = 0,
y = 1,
w = 2,
h = 3,
s = 4,
};
det.h 文件
#ifndef DET_H #define DET_H // 打印 Hello World! void hello();
det.cpp 文件
#include#include "det.h" void hello() { std::cout << "Hello, World!" << std::endl; }
main.cpp 文件调用.so库里面的文件
#include参考链接#include "det.h" using std::cout; using std::endl; int main() { hello(); cout << "1 + 2 = " << sum(1,2) << endl; cout << "1 + 2 + 3 = " << sum(1,2,3) << endl; return 0; }
链接: link.
https://blog.csdn.net/cindywry/article/details/86063930



