当前位置:网站首页>Cmake actual combat record (I)

Cmake actual combat record (I)

2022-06-10 14:54:00 CV-deeplearning

One . A simple example

The project catalogue is as follows :

├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── libmy_add.so
│   ├── Makefile
│   └── my_test
├── CMakeLists.txt
├── include
│   └── my_add.h
├── my_test.cpp
└── src
    └── my_add.cpp

my_add.h

#include <iostream>

int my_add(int x, int y);

my_add.cpp

#include "my_add.h"

int my_add(int x, int y)
{
    
    return x+y;
}

my_test.cpp

#include<iostream>
#include "my_add.h"

int main()
{
    
    int c;
    c = my_add(2,3);
    std::cout << "test my_add:" << c << std::endl;
    std::cout << "success" << std::endl;

}

CMakeLists.txt

# Required Cmake Minimum version 
cmake_minimum_required(VERSION 3.18)

# Project name 
project(my_test)

set(CMAKE_CXX_STANDARD 11)

set(my_add ${
    PROJECT_SOURCE_DIR}/src/my_add.cpp)

include_directories("${PROJECT_SOURCE_DIR}/include")

# Generating shared libraries 
add_library(my_add SHARED ${
    my_add})

# Generate executable files 
add_executable(my_test my_test.cpp)

# Connect to shared library 
target_link_libraries(my_test my_add)

compile

mkdir build
cd build
cmake ..
make

build There is a shared library under the folder libmy_add.so, Executable file my_test

Two . Call your own shared library file

All external dependent libraries are like this , such as opencv ,openni, eigen wait , The principle is the same , But they have been installed in the system , Can find , This one needs to be configured by ourselves .
That is, the shared library file I generated above is essentially the same as opencv The library is the same . But this shared library needs to be manually configured .
For example, I have built a new project , You need to call the above shared library libmy_add.so.
The project catalogue is as follows :

.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── Makefile
│   └── my_test
├── CMakeLists.txt
├── include
│   └── my_add.h
├── lib
│   └── libmy_add.so
├── my_test.cpp
└── src
    └── my_add.cpp

CMakeLists.txt

# Required Cmake Minimum version 
cmake_minimum_required(VERSION 3.18)

# Project name 
project(my_test)

set(CMAKE_CXX_STANDARD 11)

include_directories("${PROJECT_SOURCE_DIR}/include")

set(ADD_SO "${PROJECT_SOURCE_DIR}/lib/libmy_add.so")

# Generate executable files 
add_executable(my_test my_test.cpp)

# Connect to shared library 
target_link_libraries(my_test ${ADD_SO})

In fact, the main thing is to specify when the shared library is called , The header file used , And the location of the shared library itself , Then include the link .

3、 ... and . Call the installed shared library

Opencv Add dependencies for
such as Opencv, Its header file and .so The files have been placed in the system variables , Don't define yourself to the above ( The addresses of the header file and the shared library file in the above example are set by myself )
its CMakeLists.txt as follows :
find_package(OpenCV REQUIRED)
include_directories(${OPENCV_INCLUDE_DIRS})
target_link_libraries(MAIN ${OpenCV_LIBS})
Just look it up ,OpenCV_LIBS and OPENCV_INCLUDE_DIRS All are defined by the system , So it's easier
The project catalogue is as follows :

.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── cv_example
│   └── Makefile
├── CMakeLists.txt
├── cv_example.cpp
└── test.jpg

cv_example.cpp

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"


int main() {
    
    std::string img_path = "../test.jgp";
    cv::Mat M = cv::imread(img_path);
    std::cout << "load img success" << std::endl;
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.18)

project(cv_example)

set(CMAKE_CXX_STANDARD 14)

add_executable(cv_example cv_example.cpp)

find_package(OpenCV REQUIRED)

include_directories(${
    OPENCV_INCLUDE_DIRS})

target_link_libraries(cv_example ${
    OpenCV_LIBS})

Compile implementation :

mkdir build
cd build
cmake ..
make
./cv_example

Four . A complete project CMakeLists.txt

# Project name 
project(Camera_sugan)                  
 
# Compile minimum cmake edition 
cmake_minimum_required(VERSION 2.6)    
 
# Set up c++ compiler 
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )  
 
# Look all over the computer opencv package 
find_package(OpenCV REQUIRED)    
 
# Include header file path 
include_directories(             
    ./include/inudev/
    ./src/
)
 
# List all source files as a collection , The collection name is SRC_LISTS
set(SRC_LISTS                   
    ./src/inuitive.cpp
    ./src/runCamera_Qfeeltech.cpp
)
 
# Generate a static library from all the source files in the collection , The name of the static library libsugan,
#  Be careful , Throughout CmakeLists Use it all in libsugan This 
add_library(libsugan ${
    SRC_LISTS})   
 
# Name to replace the library generated by the previous collection .
target_link_libraries(libsugan    # The dependent libraries needed to link the static libraries 
    ${
    OpenCV_LIBS}
    ${
    PROJECT_SOURCE_DIR}/lib/libCommonUtilities.so
    ${
    PROJECT_SOURCE_DIR}/lib/libInuStreams.so
)

5、 ... and . CMake Appoint g++ Version compilation

stay CMakeLists.txt Add... Before calling the compiler in :

SET(CMAKE_C_COMPILER "/usr/local/bin/gcc")

perform cmake Before the command , stay shell The terminal first sets the following two variables :

export CXX=/usr/local/bin/g++
# /usr/local/gcc-xxx/lib  It's your new gcc Of lib Location 
export LD_LIBRARY_PATH=/usr/local/gcc-xxx/lib:$LD_LIBRARY_PATH

6、 ... and . Set the directory to find the dynamic library when compiling and when the program is running

Program runtime , The order in which dynamic libraries are searched ( priority ) The priority is :

  1. RPATH , Add... When compiling links -rpath Parameters Specified directory
  2. LD_LIBRARY_PATH The directory specified by this environment variable
  3. /etc/ld.so.conf The configuration file .
  4. /usr/lib 、 /lib and /usr/local/lib , System default path .
    So we set up RPATH , also RPATH There is a dynamic library to be found under , The program loads it first
    Be careful :
    You can see ,RPATH And RUNPATH In the middle LD_LIBRARY_PATH. To allow users to modify LD_LIBRARY_PATH To specify the .so file , Most compilers will output RPATH leave a blank , And use RUNPATH Instead of RPATH.

7、 ... and . other

#  Put the current directory /src The files under the , Specify in variable SRC_LIST in 
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/ SRC_LIST)

add_subdirectory(my-sdk)

#  Add local so
set_target_properties(decode PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/lib/fastertransformer.so)

Reference resources :https://blog.csdn.net/bandaoyu/article/details/115165199
https://blog.csdn.net/Guo_Python/article/details/124846646?spm=1001.2014.3001.5501

原网站

版权声明
本文为[CV-deeplearning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101453053798.html