当前位置:网站首页>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
边栏推荐
- 【Try to Hack】vulnhub DC1
- How to choose a technology stack for web applications in 2022
- 买卖股票的最佳时机
- [world history] Episode II: Dawn of civilization
- 两种方法实现pycharm中代码回滚到指定版本(附带截图展示)
- laravel8实现图片验证码
- Biscuit distribution
- [world history] Episode 1: people in the Stone Age
- Go语言Zap库Logger的定制化和封装使用详解
- Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts
猜你喜欢

Stream竟然还有应用进阶学习?作为程序员的你知道吗

China has made major breakthroughs in battery technology. Japan, South Korea and the United States are lagging behind. China has consolidated its leading edge

Summary of common functions in Oracle Database

JVM uses tools to analyze classic cases of OOM
![[Ocean University of China] Data Sharing for retest of initial Examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Ocean University of China] Data Sharing for retest of initial Examination

Shell string variable

【中國海洋大學】考研初試複試資料分享

【中国海洋大学】考研初试复试资料分享

两种方法实现pycharm中代码回滚到指定版本(附带截图展示)

有哪个瞬间让你觉得这个世界出bug了?
随机推荐
分饼干问题
[world history] Episode II: Dawn of civilization
Heavyweight! The domestic IDE is released and developed by Alibaba. It is completely open source! (high performance + high customization)
dmsetup命令
About reconnection of STM32 using lan8720a plug-in network cable
PubSub JS library realizes "cross component" data transfer
Garbage collection mechanism
Application of TSDB in civil aircraft industry
全国首例,中国电信 5G 井下人员定位项目正式商用:可实时跟踪位置,保障作业安全
从0到1完全掌握 XSS
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
关于win10 版本kicad 卡死的问题, 版本6.x
NBD Network Block Device
Thymeleaf Usage Summary
移除区间(贪心)
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
What is the safest app for stock account opening? Tell me what you know
Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
一次性总结:64个数据分析常用术语!
Gif动图如何裁剪?收下这个图片在线裁剪工具