cmake的学习笔记

cmake的学习笔记

1. CMAKE_PREFIX_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 设置安装目录

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 (更新于20220105)

在CMake使用pkg-config查找OpenCV的目录

今天在使用CMake编译opencv项目时候使用CMake的find_package命令寻找OpenCV的安装目录,代码如下:

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

上面命令可以成功找到OpenCV并且可以成功通过编译前的检测,但是,我们在编译时候死活找不到lib目录,进而导致静态库链接没有找到,在编译时候就报错了,报错如下:

====================[ 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.

在网上找了一轮,找到了利用pkg-config工具去添加静态库链接的方法,不使用cmake而使用g++编译的命令如下:

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

上面的命令很明显使用了,pkg-config命令找到了OpenCV的静态库,可以直接在shell中执行下面命令,并返回库链接结果:

pkg-config --cflags --libs opencv

如何在CMake中使用pkg-config库呢?

只需要添加下面几行,首先,利用pkg找到opencv

# 设置库目录
# 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})
# 添加三方opencv的头文件路径-- -/usr/local/include/opencv;/usr/local/include
INCLUDE_DIRECTORIES(${PKG_OPENCV_INCLUDE_DIRS})

因为pkg-config工具主要是利用*.pc后缀去搜索库文件的,所以上面代码第一行中的pkg-config路径可以使用find命令去搜索opencv.pc文件,下面是搜索的结果,可以知道我电脑的.pc文件是在/usr/local/lib/pkgconfig

root@ubuntu:~/CLionProjects/caffe_demo# find / -name "opencv.pc"
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/local/lib/pkgconfig/opencv.pc

上面代码找到了头文件并添加到导包目录中还不够,接下来需要我们把静态库文件链接到项目,我们使用TARGET_LINK_LIBRARIES命令:

add_executable(caffe_demo main.cpp)
# 为指定的bin文件添加三方链接库
TARGET_LINK_LIBRARIES(caffe_demo ${PKG_OPENCV_LDFLAGS})

注意:
TARGET_LINK_LIBRARIES命令一定要放在add_executable命令下面,否则编译时候也会报错的.
以上是本部分的所有内容了.

4. CMake + find_package+ opencv

研究完pkg-config库以后,我才发现CMake自带的find_package也是可以找到库文件的,并且更加方便,真的奔溃,下面直接上结果

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

上面代码第一二行不是必须的,如果系统找不到opencv的安装目录才通过一二行自己指定,找到OpenCV以后,同样需要链接库文件,并且和上面的方法也是类似的.只不过这次的变量名为OpenCV_LIBRARIES,同样需要放在add_executable的后面.

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

5. option命令

option命令用于设置一个类似于Boolean的变量

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

上面的命令,设置了一个USE_OPENCV的变量,并用add_definitions到全局当中去,注意add_definitions里面的内容,需要在变量名前面加上-D,即-D变{量名},此命令等于在命令行中传入参数:

-DUSE_OPENCV=ON

设置全局变量有什么用?

可以对文件里面的函数进行条件编译,下面是源码中的条件编译的使用,很明显,设置后,USE_OPENCV为ON时候才会执行if条件里面的代码.

#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值