Ubuntu 22.04下mlpack安装后缺失mlpack.hpp无法编译的解决问询
问题
在Ubuntu 22.04上通过包管理器执行以下命令安装mlpack:
sudo apt install libmlpack-dev mlpack-bin libarmadillo-dev
编译mlpack官方示例代码时,发现系统中缺少mlpack.hpp文件——虽然存在/usr/include/mlpack目录,但里面没有所需的头文件。查阅mlpack快速入门文档和安装指南,均未提及该文件需要单独安装或下载,请问如何解决编译问题?该从何处获取此文件?
尝试编译的示例代码如下:
#include <iostream> #include <mlpack.hpp> using namespace mlpack; int main() { // Load the data from data.csv (hard-coded). Use CLI for simple command-line // parameter handling. arma::mat data("0.339406815,0.843176636,0.472701471; \ 0.212587646,0.351174901,0.81056695; \ 0.160147626,0.255047893,0.04072469; \ 0.564535197,0.943435462,0.597070812"); data = data.t(); // Use templates to specify that we want a NeighborSearch object which uses // the Manhattan distance. NeighborSearch<NearestNeighborSort, ManhattanDistance> nn(data); // Create the object we will store the nearest neighbors in. arma::Mat<size_t> neighbors; arma::mat distances; // We need to store the distance too. // Compute the neighbors. nn.Search(1, neighbors, distances); // Write each neighbor and distance using Log. for (size_t i = 0; i < neighbors.n_elem; ++i) { std::cout << "Nearest neighbor of point " << i << " is point " << neighbors[i] << " and the distance is " << distances[i] << "." << std::endl; } return 0; }
解决方法
- 版本适配问题:Ubuntu 22.04仓库中的mlpack版本为3.4.2,而
mlpack.hpp是mlpack 4.x版本才引入的统一入口头文件,旧版本没有这个文件,需直接引用具体模块的头文件。 - 修改代码头文件:将示例中的
<mlpack.hpp>替换为对应功能模块的头文件,本示例用到邻居搜索功能,替换为:#include <mlpack/methods/neighbor_search/neighbor_search.hpp> #include <armadillo> - 正确编译命令:编译时需要链接mlpack和armadillo库,执行:
g++ your_code.cpp -o your_code -lmlpack -larmadillo - 若需使用
mlpack.hpp:从源码编译安装mlpack 4.x版本,步骤如下:- 安装编译依赖:
sudo apt install build-essential cmake libarmadillo-dev libboost-all-dev - 克隆源码并编译安装:
git clone https://github.com/mlpack/mlpack.git cd mlpack mkdir build && cd build cmake .. make -j$(nproc) sudo make install
mlpack.hpp会被放置在/usr/local/include/mlpack/目录下,即可正常使用统一头文件。 - 安装编译依赖:
内容的提问来源于stack exchange,提问作者Wojciech Artichowicz
相关产品推荐
相关产品推荐

