当前位置:网站首页>Study notes of cmake

Study notes of cmake

2022-06-25 14:47:00 HELLOWORLD2424

cmake Learning notes of

1. CMAKE_PREFIX_PATH Add dependent search path

Semicolon-separated list of directories specifying installation prefixes to be searched by the find_package(), find_program(), find_library(), find_file(), and find_path() commands. Each command will add appropriate subdirectories (like bin, lib, or include) as specified in its own documentation.
By default this is empty. It is intended to be set by the project.

2. CMAKE_INSTALL_PREFIX Set installation directory

Install directory used by install.

If “make install” is invoked or INSTALL is built, this directory is prepended onto all install directories. This variable defaults to /usr/local on UNIX and c:/Program Files on Windows.

On UNIX one can use the DESTDIR mechanism in order to relocate the whole installation. DESTDIR means DESTination DIRectory. It is commonly used by makefile users in order to install software at non-default location. It is usually invoked like this:

make DESTDIR=/home/john install
which will install the concerned software using the installation prefix, e.g. “/usr/local” prepended with the DESTDIR value which finally gives “/home/john/usr/local”.

WARNING: DESTDIR may not be used on Windows because installation prefix usually contains a drive letter like in “C:/Program Files” which cannot be prepended with some other prefix.

The installation prefix is also added to CMAKE_SYSTEM_PREFIX_PATH so that find_package, find_program, find_library, find_path, and find_file will search the prefix for other software.

3. CMake + OpenCV + pkg-config ( Updated on 20220105)

stay CMake Use pkg-config lookup OpenCV The catalog of

In use today CMake compile opencv Use in the project CMake Of find_package Order to find OpenCV Installation directory , The code is as follows :

# OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

The above command can be found successfully OpenCV And it can successfully pass the pre compilation detection , however , We can't find it when compiling lib Catalog , As a result, the static library link is not found , An error is reported during compilation , An error is as follows :

====================[ Build | caffe_demo | Debug ]==============================
/root/clion-2021.3.2/bin/cmake/linux/bin/cmake --build /root/CLionProjects/caffe_demo/cmake-build-debug --target caffe_demo
[2/2] Linking CXX executable caffe_demo
FAILED: caffe_demo 
: && /usr/bin/c++ -g  CMakeFiles/caffe_demo.dir/main.cpp.o -o caffe_demo -L//home/ethan/Documents/projects/caffe/build/lib  && :
/usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `main': /root/CLionProjects/caffe_demo/main.cpp:7: undefined reference to `cv::imread(cv::String const&, int)' /usr/bin/ld: /root/CLionProjects/caffe_demo/main.cpp:8: undefined reference to `cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' /usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `cv::String::String(char const*)':
/usr/local/include/opencv2/core/cvstd.hpp:602: undefined reference to `cv::String::allocate(unsigned long)' /usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `cv::String::~String()': /usr/local/include/opencv2/core/cvstd.hpp:648: undefined reference to `cv::String::deallocate()' /usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `cv::String::operator=(cv::String const&)':
/usr/local/include/opencv2/core/cvstd.hpp:656: undefined reference to `cv::String::deallocate()' /usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `cv::Mat::~Mat()': /usr/local/include/opencv2/core/mat.inl.hpp:774: undefined reference to `cv::fastFree(void*)' /usr/bin/ld: CMakeFiles/caffe_demo.dir/main.cpp.o: in function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.inl.hpp:886: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I found a round on the Internet , Found a way to use pkg-config Tools to add static library links , Don't use cmake While using g++ The compiled command is as follows :

g++ main.cpp -o test `pkg-config --cflags --libs opencv`

The above command obviously uses ,pkg-config The order found OpenCV Static library , Can be directly in shell Execute the following command in , And return the library link results :

pkg-config --cflags --libs opencv

How to be in CMake Use in pkg-config The library? ?

Just add the following lines , First , utilize pkg find opencv

#  Set the Library Directory 
# OpenCV
SET(ENV{
    PKG_CONFIG_PATH} /usr/local/lib/pkgconfig/)
find_package(PkgConfig)
pkg_search_module(PKG_OPENCV REQUIRED opencv)
message("PKG_OPENCV_INCLUDE_DIRS ${PKG_OPENCV_INCLUDE_DIRS}")
message("PKG_OPENCV_LDFLAGS" ${PKG_OPENCV_LDFLAGS})
#  Add third party opencv The header file path of -- -/usr/local/include/opencv;/usr/local/include
INCLUDE_DIRECTORIES(${PKG_OPENCV_INCLUDE_DIRS})

because pkg-config Tools are mainly used to *.pc Suffix to search for library files , So in the first line of the above code pkg-config The path can be used find Order to search for opencv.pc file , Here are the search results , You can know my computer .pc File is in /usr/local/lib/pkgconfig

[email protected]:~/CLionProjects/caffe_demo# find / -name "opencv.pc"
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/local/lib/pkgconfig/opencv.pc

It is not enough for the above code to find the header file and add it to the import package directory , Next, we need to link the static library file to the project , We use TARGET_LINK_LIBRARIES command :

add_executable(caffe_demo main.cpp)
#  For the specified bin File add third party link library 
TARGET_LINK_LIBRARIES(caffe_demo ${PKG_OPENCV_LDFLAGS})

Be careful :
TARGET_LINK_LIBRARIES Orders must be placed on add_executable Command below , Otherwise, an error will be reported during compilation .
That's all for this part .

4. CMake + find_package+ opencv

End of study pkg-config After the library , I just found out that CMake Self contained find_package Library files can also be found , And it's more convenient , Really run away , Let's go directly to the results

# OpenCV
#SET(OpenCV_ROOT /usr/local)
#include_directories(OpenCV_ROOT)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

The first two lines of the above code are not required , If the system cannot be found opencv The installation directory of is specified by itself through one or two lines , find OpenCV in the future , You also need to link library files , And it is similar to the above method . But this time the variable name is OpenCV_LIBRARIES, It also needs to be placed in add_executable Behind .

add_executable(caffe_demo main.cpp)
TARGET_LINK_LIBRARIES(caffe_demo ${OpenCV_LIBRARIES})

5. option command

option The command is used to set a command similar to Boolean The variable of

# cpu only
option(USE_OPENCV "whether use opencv" ON)
if (USE_OPENCV)
    add_definitions(-DUSE_OPENCV)
endif()

The command above , Set up a USE_OPENCV The variable of , And use add_definitions Go into the overall situation , Be careful add_definitions What's in it , You need to prefix the variable name with -D, namely -D change { Quantity name }, This command is equivalent to passing in parameters... On the command line :

-DUSE_OPENCV=ON

What is the use of setting global variables ?

You can conditionally compile the functions in the file , The following is the use of conditional compilation in the source code , Obviously , After setting ,USE_OPENCV by ON When will it be executed if The code in the condition .

#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif // USE_OPENCV
原网站

版权声明
本文为[HELLOWORLD2424]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200519299724.html