linux生成.so库,调用.so库函数

wuchangjian2021-11-16 17:45:29编程学习

这里写自定义目录标题

  • 欢迎使用Markdown编辑器
    • 一. 生成.so库
    • 二. 调用.so库
    • 参考链接

欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

一. 生成.so库

  1. c++工程目录
    我的c++工程myProject目录结构如下:
 1. include
 		-----det.h
 		-----ut.h
 2. src
 		-----det.cpp
 		-----main.cpp
 3. build
 4. CMakelists.txt
  1. 编写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/cpe/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/cpe/Downloads/opencv/opencv-master/modules/dnn/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules/videoio/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules/imgcodecs/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules/highgui/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules/imgproc/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/build)
include_directories(/home/cpe/Downloads/opencv/opencv-master/modules/core/include)
include_directories(/home/cpe/Downloads/opencv/opencv-master/include)
include_directories(/home/cpe/Downloads/libtorch/include)
include_directories(/home/cpe/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}
)
  1. 执行命令生成

在工程目录myProject下,执行一下命令,生成.so文件和.a文件

mkdir build && cd build
cmake ..
make

二. 调用.so库

  1. 调用.so库的c++的工程目录
    我的调用.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
  1. 编写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
)
  1. 执行命令生成

在工程目录myProject2下,执行一下命令,生成.so文件和.a文件

mkdir build && cd build
cmake ..
make
./myProject2
  1. 各个 .cpp 和 .h文件实例
    det.h 文件
#ifndef DET_H
#define DET_H

// 打印 Hello World!
void hello(); 

det.cpp 文件

   #include <iostream>
   #include "det.h"
    
   void hello() {
       std::cout << "Hello, World!" << std::endl;
   }

main.cpp 文件调用.so库里面的文件

 #include <iostream>
#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

相关文章

javaswing各类管理系统

链接:https://pan.baidu.com/s/1K1he8FlPY...

满满科技感,记者花1.9元体验自动驾驶出租车

满满科技感,记者花1.9元体验自动驾驶出租车

2022-08-15 19:22:48 近日,湖南阿波罗智行运营的百度...

AB遇到的问题- 多重检验

目录 多重检验问题(Multiple Testing Problem) 多重检验会发...

MySQL/MariaDB 时间函数

获取年月日时分秒 SELECT NOW();#年月日及当前时间 SELECT...

会员号的百位数字等于产生的随机数字即为幸运会员

import java.util.Scanner; public class chouj...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。