CMake | 将路径添加到 CMAKE_PREFIX_PATH

1. CMAKE_PREFIX_PATH

CMAKE_PREFIX_PATH是一个分号分隔的路径列表,用来指明软件/库安装路径前缀,以供find_package()find_program()find_library()find_file()find_path()命令搜索使用,这样就方便搜索可执行文件、头文件、库文件及相关配置文件等。

CMAKE_PREFIX_PATH文档

2. 演示

笔者将Eigen-3.4.0安装在了/opt/eigen-3.4.0目录下,在使用时可以这样写CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(program LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_PREFIX_PATH "/opt/eigen-3.4.0") # 安装路径前缀
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
message("** Found Eigen3 in: ${EIGEN3_ROOT_DIR}")

add_executable(${PROJECT_NAME} main.cpp)

命令cmake运行日志

-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
** Found Eigen3 in: /opt/eigen-3.4.0
-- Configuring done
-- Generating done
-- Build files have been written to: (这个路径内容太长,删掉了)

测试程序如下

#include <iostream>
#include <Eigen/Eigen>

int main(int argc, char* argv[])
{
    std::cout << "Eigen3 version is : " 
              << EIGEN_WORLD_VERSION << "." 
              << EIGEN_MAJOR_VERSION << "." 
              << EIGEN_MINOR_VERSION << std::endl;
    return 0;
}

运行结果
image

posted @ 2022-04-23 10:19  Fitanium  阅读(21052)  评论(0编辑  收藏  举报