install and cmake compilation

Posted by kvnirvana on Tue, 08 Feb 2022 16:42:30 +0100

Generally, when compiling the link library, we use the following commands:
The link library example is ceres-solver-1.14.0

mkdir build && cd build
cmake ..
make -j8
sudo make install

We can choose to install the lib and include files compiled by make in the system, that is, make install
install copies include and lib to the system directory, usually the default folder / usr.

ceres-solver-1.14 is in
lib: /usr/locallib/cmake/
include: /usr/local/include/
in


(these two files are also available under usr/include and usr/lib/cmake (I don't know which one is used, but it is usually used under local). The installation here may be repeated, or Ceres solver is automatically installed for other dependencies)

problem

However, if all dependencies are installed in the system, it is convenient to use, but it is troublesome to install different versions or conflict between dependency libraries. Therefore, we install the dependent libraries in the corresponding folders, and then directly use the compiled libraries in these folders in the projects using these dependent libraries to prevent conflicts and facilitate copy and migration.

The folder in the install make library depends on:

mkdir build && cd build
cmake -D CMAKE_INSTALL_PREFIX=../install ..
make -j8
make install

Install the install folder in the folder of the link library, as shown in the figure:

After that, it is called directly from the install of the link library in the project.
1. Convenient version management without conflict
2. Facilitate project migration (may)

In a general project, if we want to call the link library to compile the project, we need to use cmakelists Txt to add the instructions to compile the link library.

A cmakelists with a link library Txt is written as follows:
The link library example is ceres-solver-1.14.0

# Declare the minimum version of cmake required
cmake_minimum_required( VERSION 2.8 )

# Declare a cmake project
project( test )

# Set compilation mode
set( CMAKE_BUILD_TYPE "Debug" )
#set(CMAKE_CXX_FLAGS "-std=c++11")

#Set query link library
#set(Ceres_DIR "~/pkg/ceres-solver-1.14.0/install/lib/cmake/Ceres")
find_package(Ceres REQUIRED)
#find_package(Glog REQUIRED)
#find_package(OpenCV  REQUIRED)

#Add variables from the link library to (the project)
include_directories( ${CERES_INCLUDE_DIRS} )

# Add an executable
# Syntax: add_ Executable (program name source code file)
add_executable( test test.cpp )
target_link_libraries(test ceres)

When setting the query link library, it is generally written as:

find_package(Ceres REQUIRED)

Therefore, you need to find the lib library named Ceres in the system path,
For example, install of ceres-solver-1.14 is in the system:

/usr/local/lib/cmake/Ceres/
and
/usr/local/inlcude/ceres/

Directly specify the link library of the query

#Set query link library
set(Ceres_DIR "~/pkg/ceres-solver-1.14.0/install/lib/cmake/Ceres")

In this way, cmake will directly search the link library according to the directory and compile it.

Some cmakelists Txt understanding

#Set query link library
#Set the path to ${library name _DIR}, eg:ceres as follows. Note the case, which is consistent with / Ceres in ~ / pkg/ceres-solver-1.14.0/install/lib/cmake /
set(Ceres_DIR "~/pkg/ceres-solver-1.14.0/install/lib/cmake/Ceres")
find_package(Ceres REQUIRED)#Find the required link library
#find_package(Glog REQUIRED)
#find_package(OpenCV  REQUIRED)

#Add variables from the link library to (the project)
include_directories( ${CERES_INCLUDE_DIRS} )
a key:
set(Ceres_DIR "~/pkg/ceres-solver-1.14.0/install/lib/cmake/Ceres")
#Set the path to ${library name _DIR}, eg:ceres as follows. Note the case, which is consistent with / Ceres in ~ / pkg/ceres-solver-1.14.0/install/lib/cmake /

#Add variables from the link library to (the project)
include_directories( ${CERES_INCLUDE_DIRS} )
#You can XXX in / Ceres / in install/lib/cmake / Seen in cmake file (found),
#XXX.cmake adds many variables to ${CERES_INCLUDE_DIRS}
#CeresConfig. eg in cmake file:
#  list(APPEND CERES_INCLUDE_DIRS ${CERES_INCLUDE_DIR})
#  list(APPEND EIGEN_INCLUDE_DIR_HINTS /usr/include/eigen3)

Topics: cmake Ubuntu