【任务描述】CPP开发了一些功能,打包成so文件对外发布,并且调用这个so文件。
自己写了两个小功能(1)show_img和(2)print_string;就是调用opencv的imread函数将图像读到内存并,以及打印输入的字符串。
将这两个小功能分别封装成动态链接库文件,并在另一个main函数中调用他们。
【一、文件目录】
如下图所示,show_img文件夹包括:实现show_img.h和show_img.cpp文件,以及CMakeLists.txt文件(编译so库),build文件夹是防止编译中间文件的,lib文件夹是用来放编译出来的so文件的。
同理,print_string文件夹也是这样的目录结构。
project文件夹包括:test.cpp是调用so文件的main函数实现,以及CMakeLists.txt文件(编译可执行文件)。
【二、代码解析 + CMakeLists.txt解析】
(二.一)以show_img为例:
show_img.h的内容如下:
#ifndef SHOW_IMG_H_
#define SHOW_IMG_H_
#include <iostream>
#include <opencv2/opencv.hpp>
cv::Mat show_img(const std::string &path);
#endif
show_img.cpp的内容如下:
#include "show_img.h"
cv::Mat show_img(const std::string &path){
cv::Mat img = cv::imread(path, cv::IMREAD_UNCHANGED);
return img;
}
CMakeList.txt的内容如下:
cmake_minimum_required(VERSION 3.0)
project(demo CXX)
# 设置编译为Release模式
set(DCMAKE_BUILD_TYPE "Release")
# 添加opencv的头文件地址
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# 添加show_img内头文件的地址,如果目录结构有变化,修改“.”的内容
aux_source_directory(. DIR_SRC)
message(STATUS "SRC: ${DIR_SRC}")
# 添加opencv库地址
add_library(showimg SHARED ${DIR_SRC})
# 连接opencv库
target_link_libraries(showimg ${OpenCV_LIBS})
# 设置编译得到的so文件的保存目录为【${PROJECT_SOURCE_DIR}】
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
# 设置so文件的名字为【showimg】,生成的so文件名字为【libshowimg】
# 并设置文件在保存目录下的保存文件夹为【lib】,该文件夹会自动被创立
install(TARGETS showimg LIBRARY DESTINATION lib)
(二.二)print_string
print_string.h的内容如下
#ifndef PRINT_STRING_H_
#define PRINT_STRING_H_
#include <iostream>
#include <opencv2/opencv.hpp>
bool print_string(const std::string &str);
#endif
print_string.cpp
#include "print_string.h"
bool print_string(const std::string &str){
std::cout << str << std::endl;
return true;
}
CMakeList.txt的内容如下:
cmake_minimum_required(VERSION 3.0)
project(demo CXX)
set(DCMAKE_BUILD_TYPE "Release")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
aux_source_directory(. DIR_SRC)
message(STATUS "SRC: ${DIR_SRC}")
add_library(printstring SHARED ${DIR_SRC})
target_link_libraries(printstring ${OpenCV_LIBS})
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
install(TARGETS printstring LIBRARY DESTINATION lib)
(二.三)test代码
#include <iostream>
#include <opencv2/opencv.hpp>
#include "show_img.h"
#include "print_string.h"
int main()
{
std::string str = R"(Hello World!)";
print_string(str);
std::string path = R"(/home/sz/Desktop/test/test.jpeg)";
cv::Mat img = show_img(path);
cv::imshow("Src", img);
cv::waitKey(0);
cv::destroyAllWindows();
return 1;
}
CMakeList.txt的内容如下:
cmake_minimum_required (VERSION 3.0)
project(demo CXX)
set(DCMAKE_BUILD_TYPE "Release")
# opencv的include文件路径
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# 私有so的include文件路径
include_directories("../show_img")
include_directories("../print_string")
# 待编译文件的地址
aux_source_directory(. DIR_SRC)
# 添加so文件的连接地址
link_directories ("../show_img/lib")
link_directories ("../print_string/lib")
# 生成可执行文件
add_executable (demo ${DIR_SRC})
# 连接opencv库文件
target_link_libraries(demo ${OpenCV_LIBS})
# 连接私有so的库文件
target_link_libraries (${PROJECT_NAME} libshowimg.so libprintstring.so)
# 设置可执行文件的保存路径
set(EXECUTABLE_OUTPUT_PATH ..)
【三、编译指令】
以show_img的编译为例。
首先进入预先建立好的build文件夹内,分别运行(1)cmake .. (2)make (3)make install这三个指令,如下所示
sz@Clover:~/Desktop/test/show_img/build$ cmake ..
-- The CXX compiler identification is GNU 7.5.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "3.4.1")
-- SRC: ./show_img.cpp
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sz/Desktop/test/show_img/build
sz@Clover:~/Desktop/test/show_img/build$ make
Scanning dependencies of target showimg
[ 50%] Building CXX object CMakeFiles/showimg.dir/show_img.cpp.o
[100%] Linking CXX shared library libshowimg.so
[100%] Built target showimg
sz@Clover:~/Desktop/test/show_img/build$ make install
[100%] Built target showimg
Install the project...
-- Install configuration: ""
-- Installing: /home/sz/Desktop/test/show_img/lib/libshowimg.so
-- Set runtime path of "/home/sz/Desktop/test/show_img/lib/libshowimg.so" to ""
sz@Clover:~/Desktop/test/show_img/build$
这是可以在对应的lib文件夹下看到libshowimg.so文件。
同理,在print_string也是执行这三个指令。
进入到project文件夹内的build路径,分别执行(1)cmake .. (2)make两个指令,如下所示
sz@Clover:~/Desktop/test/project/build$ cmake ..
-- The CXX compiler identification is GNU 7.5.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "3.4.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sz/Desktop/test/project/build
sz@Clover:~/Desktop/test/project/build$ make
Scanning dependencies of target demo
[ 50%] Building CXX object CMakeFiles/demo.dir/test.cpp.o
[100%] Linking CXX executable ../demo
[100%] Built target demo
此时可以在build文件夹外看到demo可执行程序,执行后可以输出字符串和图像。