Compile and link dynamic library under Linux

For the C++ deployment under Linux, the most troublesome is the dynamic library dependency. A program that runs well locally may not work well for another machine. (Docker is not considered here for now)

Because the local dynamic library is not installed on the deployed machine, even if there is a path, it may not be the same, and the version may not be the same. The most common is to rely on opencv. The general solution is to use the opencv static library, and then compile the opencv static library Link in, so that there is no problem with opencv dependent libraries.

There is also the problem of cuda dependency library. On the Internet, CUDA starts from CUDA 5.5 and supports static library.

If there is no static library we will have a lot of trouble, so consider other solutions.

Compile and link dynamic library under Linux

1, use makefile

 

2、使用CMakeLists.txt

The following CMakeLists.txt is for reference only:

cmake_minimum_required(VERSION 3.5)
 
project(dbnet_demo LANGUAGES CXX)
 
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenCV 3.1.0 REQUIRED)
set(OPENCV_DYNAMIC_LIBS  "${CMAKE_CURRENT_SOURCE_DIR}/../libs")       # 动态 opencv native 库路径
add_library(libopencv_core SHARED IMPORTED )
set_target_properties(libopencv_core PROPERTIES   IMPORTED_LOCATION "${OPENCV_DYNAMIC_LIBS}/libopencv_core.so")
add_library(libopencv_imgcodecs SHARED IMPORTED )
set_target_properties(libopencv_imgcodecs PROPERTIES   IMPORTED_LOCATION "${OPENCV_DYNAMIC_LIBS}/libopencv_imgcodecs.so")
add_library(libopencv_imgproc SHARED IMPORTED )
set_target_properties(libopencv_imgproc PROPERTIES   IMPORTED_LOCATION "${OPENCV_DYNAMIC_LIBS}/libopencv_imgproc.so")
set(mOpenCV_LIBS  libopencv_core libopencv_imgcodecs  libopencv_imgproc)

message(STATUS "Opencv library status: ")
message(STATUS "> version: ${OpenCV_VERSION} ")
message(STATUS "> library: ${OpenCV_LIBS} ")
message(STATUS "> include: ${OpenCV_INCLUDE_DIRS} ")

# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIR})
 
set(Torch_DIR /mnt/hgfs/vmsharefolders/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
 
add_executable(dbnet_demo test_db.cpp clipper.cpp)
target_link_libraries(dbnet_demo ${mOpenCV_LIBS}
		"${TORCH_LIBRARIES}" )
set_property(TARGET dbnet_demo PROPERTY CXX_STANDARD 17)

The above content only specifies the relative path of opencv. It has been verified that the method is correct. As for how to find these dependent libraries, they are compiled without using the relative path, and then the dependent libraries are found through ldd, and then the dependent libraries are placed under the relative path. In addition to the three libraries of opencv, there are libz, libzstd, libjasper, libpng, libjpeg.

At the same time, it should be noted that we have to copy the real library and link library, otherwise the package will not be found; another possible solution is to try to replace libopencv_core.so with libopencv_core.so. 3.4.2 (Real library), other library operations are similar.

 

 

 

Reference: CUDA static library under linxu-under

Reference: linux cmake respectively specify the dynamic library link path at compile/run time

 

 

 

Guess you like

Origin blog.csdn.net/juluwangriyue/article/details/108519559