[Cmake] CMakeList add library|add header file|add path

CMake: 1-CMakeLists generate and add dependent libraries

https://blog.csdn.net/qq_37761077/article/details/88750711

 

Opencv dependency library addition:

cmake_minimum_required( VERSION 2.8 )
project( imageBasics )
 
# 添加c++ 11标准支持
set( CMAKE_CXX_FLAGS "-std=c++11" )
 
# 寻找OpenCV库
set(OpenCV_DIR /home/chaofan/opt/opencv-3.4.4/release)
find_package( OpenCV 3 REQUIRED )
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
 
add_executable( imageBasics imageBasics.cpp )
# 链接OpenCV库
target_link_libraries( imageBasics ${OpenCV_LIBS} )

The main steps for adding dependent libraries are explained above:

First: add header files

Second: find the source file

Third: Link with the target

 

 

If you need to specify different versions of opencv in the project, you can follow the steps below:

1. Specify the library path

file(GLOB_RECURSE Opencv3.0_LIB "/home/LiuMC/software/third_lib/opencv3.0-install/lib/*.so")

2. Specify the header file path

set(Opencv3_INLCUDE_DIRS "/home/LiuMC/software/third_lib/opencv3.0-install/include")

3. Add header files to the project

include_directories(include
    ${Opencv3_INLCUDE_DIRS}
  )

4. Add library files to the project

target_link_libraries(rovioLib  ${Opencv3.0_LIB})


Note: In order to avoid unnecessary troubles, try to add header files and library files to the first item, such as:

target_link_libraries(rovioLib  
    ${Opencv3.0_LIB}
    ${Opencv3.0_HAL_LIB}
    ${catkin_LIBRARIES}
    )

Original link: https://blog.csdn.net/liumingchun13/article/details/77089577

 

 

What is the difference between link_libraries(dcn_v2_cuda_forward_v2) and target_link_libraies?

 

Simple example:

 

1. Generate .so shared library files.
Here are a few of my files:

1hello.cpp

//hello.cpp
 int Calculate_sum_Of_Two_Number(int x,int y)
{
   int z=0;
   z=x+y;
   return (z);
}
2hello.hpp

//hello.hpp
#ifndef     __HELLO_H
#define     __HELLO_H
int Calculate_sum_Of_Two_Number(int x,int y);
#endif
3 main.cpp

//main.cpp
#include "hello.hpp"
#include <stdio.h>
int main(void)
{
   int a=0,b=0,c=0;
   printf("please input two parameter:");
   scanf("%d",&a);
   scanf("%d",&b);
   c=Calculate_sum_Of_Two_Number(a,b);
   printf("the sum is : %d",c);
   return 0;
}
4 CMakeLists.txt

#Required minimum version of
Cmake CMAKE_MINIMUM_REQUIRED( VERSION 2.8) #Project
 
name
PROJECT(main) #Set the
 
compiler compilation mode:
set( CMAKE_BUILD_TYPE "Debug") #Generate
 
shared library#
get the shared package
#here needs no .hpp
add_library(calculate_shared SHARED hello.cpp) #Generate
 
executable files
add_executable(main main.cpp) #Connect the
 
shared library
target_link_libraries(main calculate_shared) in
CmakeLists.txt above, the name of the shared library is calculate_shared, which we can change by ourselves. The generated executable file is main, and the name can also be changed.

But it should be noted that there is no need to include hello.hpp in hello.cpp. (Khan, because of this caused an error, the prompt said that the function was defined repeatedly);

Compile and generate:

mkdir build
cd build
cmake ..
make
We can see that the build generated the following files:

CMakeCache.txt  cmake_install.cmake     main
CMakeFiles      libcalculate_shared.so  Makefile

 libcalculate_shared.so is the generated shared library file.

Their path is: /home/fan/dev/cmake/4-exer/

There are build folders below, as well as main.cpp, hello.cpp, hello.hpp, 

There is a shared library libcalculate_shared.so.so under the build folder

2. Calling shared library files
All external dependent libraries are like this, such as opencv, openni, eigen, etc. The principle is the same, but they are already installed in the system and can be searched, and this requires our own Go to configuration.

That is, the shared library file I generated above is essentially the same as the opencv library. But this shared library needs to be configured manually.

For example, I have created a new project and need to call the above shared library libcalculate_shared.so.

main.cpp is as follows:

//main.cpp
#include <stdio.h>
#include <iostream>
#include "hello.hpp"
using namespace std;
int main(void)
{    int x=2,y=3;    int z=0;    z= Calculate_sum_Of_Two_Number(x,y);    cout<<"the result is:"<<z<<endl;    return 0; } Then in CMakeLists.txt, I need to tell CMake where to find this header file. Where can the defined function be found.






The path of hello.hpp above is: /home/fan/dev/cmake/4-exer/hello.hpp

The path of libcalculate_shared.so is /home/fan/dev/cmake/4-exer/build/libcalculate_shared.so

Then CMakeLists.txt is as follows:

CMAKE_MINIMUM_REQUIRED( VERSION 2.8)
 
PROJECT(main) #Set the
compiler compilation mode:
SET( CMAKE_BUILD_TYPE "Debug")
 
SET(HELLO_INCLUE 
    /home/fan/dev/cmake/4-exer/)
 
SET(HELLO_SRC 
    /home/fan/dev/ cmake/4-exer/build/libcalculate_shared.so)
 
INCLUDE_DIRECTORIES(${HELLO_INCLUE})
 
add_executable(main main.cpp)
 
target_link_libraries(main ${HELLO_SRC})
Here are some details (for my scum)

1. This form of ${} represents a variable, such as the above, HELLO_INCLUE, which is a variable defined by myself.

2. The header file is included in the folder where the header file is located, namely /home/fan/dev/cmake/4-exer/

3. The shared library should specify the specific shared library, accurate to .so

In fact, the main thing is to specify the header file used when calling this shared library, and the location of the shared library itself, and then include the link.

Installed shared libraries (such as opencv) do not have to be so troublesome, because their addresses are placed in variables.

Opencv dependency addition. For
example, Opencv, its header files and .so files are already placed in system variables, so you don’t need to define them yourself (the addresses of the header files and shared library files in the above example are all set by myself)

Its CMakeLists.txt is as follows:

find_package(OpenCV REQUIRED)

include_directories(${OPENCV_INCLUDE_DIRS})

target_link_libraries(MAIN ${OpenCV_LIBS})

Just find it, OpenCV_LIBS and OPENCV_INCLUDE_DIRS are already defined for us by the system, so it is easier

Reference blog:

1. How to write your own CmakeLists.txt https://www.cnblogs.com/chaofn/p/10160555.html

2. [OpenCV] Use CMake to link the OpenCV library under your own path https://blog.csdn.net/twt520ly/article/details/81981473

Original link: https://blog.csdn.net/qq_37761077/article/details/ 88750711

 

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/115165199