cmake uses the tutorial to call opencv external libraries and its own generated libraries

Posted by pbase on Sat, 04 Dec 2021 03:52:51 +0100

This section is what I encounter when I really use it. I need to build a dynamic library, but this dynamic library depends on some external libraries, such as opencv and matlab. How to build such a dynamic library? How to test when the build is ready? This chapter will introduce the process in detail:

1.ubuntu installation opencv

        You can read this article directly: Click opencv installation tutorial , we recommend that you install version 3.x

2. Start building your own library, which relies on opencv  

  Layout of my project files: src is the source file directory of my construction library, and build is the generation directory of the construction library files. At the same time, two CMakeLists.txt are created, one in the root directory and the other in src.

It should be emphasized here that although construction and testing are put together, they can also be separated. How to separate them will be described later.

2.1 CMakeLists.txt in the root directory

# Version judgment
cmake_minimum_required(VERSION 3.0)
# project name

# If you add through the installation method, you need to include the following two commands, otherwise the compilation will not pass
# The installation method is not used here, so it is commented out and added here for completeness
# include_directories(/home/ubuntu/work/usr/include)
# link_directories(/home/ubuntu/work/usr/lib)

# Find the path of opencv
find_package(OpenCV REQUIRED)
# Print information about opencv
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Add a subdirectory, which is the library directory. The cmake file of the subdirectory will be called to build the library
# Build executable
add_executable(main main.cpp)

# Add link library
# Link the dynamic library files compiled from the subdirectory src here
target_link_libraries(main mylib ${OpenCV_LIBS})
# Link the static library compiled from the subdirectory src here
# target_link_libraries(main libmylib.a)
# target_link_libraries(main PRIVATE ${OpenCV_LIBS})
# target_link_libraries(main PRIVATE ${OpenCV_LIBS})

    Here, simply introduce CMakeLists.txt in the root directory. The name of the dynamic library generated here is called When linking the dynamic library, you can directly fill in mylib. cmake will automatically add the complete When linking the static library, you need to fill in the complete name, libmylib.a:

        1. To ensure that your opencv installation is successful, you can   find_ The package (openCV required) instruction obtains opencv related information, such as lib, include, version, etc

        2. The main function of the cmake file is to start CMakeLists.txt of src to build the library and build the executable file main

        3. In particular, you should pay attention to the link library command:   

target_link_libraries(main mylib ${OpenCV_LIBS})

When the command links the dynamic library, all the external libraries that the dynamic library depends on must be added, otherwise the construction will fail. The external library that the library built in this project depends on is only opencv, so you only need to add the opencv library.

When linking a static library, you must fill in the complete library name as follows:

target_link_libraries(main libmylib.a ${OpenCV_LIBS})

The above is CMakeLists.txt in the root directory. The following describes CMakeLists.txt in src.

2.2 CMakeLists.txt in SRC directory

# Get the list of all files in this directory
aux_source_directory(. DIR_LIB_SRCS)
# Set c++11 to compile. If the pre research feature of c++11 is used, this command must be written, otherwise an error will be reported
SET( CMAKE_CXX_FLAGS "-std=c++11 -O3")
# Obtain the relevant information of opencv through the following commands. This method is not used here. The method of adding a directory is used to increase the diversity of methods
# find_package(OpenCV REQUIRED)
# message(STATUS "OpenCV library status:")
# message(STATUS "    version: ${OpenCV_VERSION}")
# message(STATUS "    libraries: ${OpenCV_LIBS}")
# message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
# include_directories(${OpenCV_INCLUDE_DIRS})

# When installing opencv, we can know the installation location of OpenCV, such as: / usr/local/include
# The source file here also depends on the runtime of matlab, so the header file is also loaded in
include_directories(/usr/local/MATLAB/MATLAB_Runtime/v96/extern/include /usr/local/include)
# Add the corresponding library file directory
link_directories(/usr/local/lib /usr/local/MATLAB/MATLAB_Runtime/v96/extern/bin/glnxa64  /usr/local/MATLAB/MATLAB_Runtime/v96/extern/lib/glnxa64)

# Specify the output location of the compilation library, which can or can not be set
# Generate dynamic library
add_library(mylib SHARED ${DIR_LIB_SRCS})

# Generate static library
add_library(mylib_static STATIC  ${DIR_LIB_SRCS}) 

# To generate a library file with the same name as the dynamic library and the static library, you need a set instruction_ target_ properties
# Here, we just rename them so that they have the same name. They should not be the same during construction
set_target_properties(mylib_static PROPERTIES OUTPUT_NAME "mylib")

# The installation command is also written here for completeness, which does not apply to him
# Install shared libraries and header files
# Install dynamic and static libraries to < prefix > / lib
# Install header file
install(FILES func.h DESTINATION include)

Here, we need to pay attention to the fact that external library dependency is not required during compilation, but during linking. The process of building the library is the same as that in the previous section. There is no more explanation here. Let's take a look at the generated results:

Create a build, and then:

cd build 
cmake ..
make install 


  2.3 follow the link path through ldd main

  To test the dynamic library here, you need to open the dynamic link from the link library under the root directory and execute ldd main

  When testing the static library, you need to open the static library command cmakelists.txt in the root directory:

  The complete example ends here

Topics: cmake OpenCV Ubuntu AI