You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20.04编译Armadillo示例代码遇undefined reference错误求助

解决Armadillo编译时undefined reference to arma::arma_rng_cxx11_instance链接错误

以下是针对Ubuntu 20.04.5 LTS + CLion环境的可行解决方案:

1. 修正CMake配置,使用官方推荐的find_package方式

不要手动指定-larmadillo,改用CMake的find_package自动处理依赖,确保CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.16)
project(armadillo_demo)

set(CMAKE_CXX_STANDARD 17)

# 查找Armadillo包并引入头文件路径
find_package(Armadillo REQUIRED)
include_directories(${ARMADILLO_INCLUDE_DIRS})

add_executable(armadillo_demo main.cpp)
# 链接Armadillo及所有依赖库
target_link_libraries(armadillo_demo ${ARMADILLO_LIBRARIES})

这种方式会自动处理Armadillo依赖的lapack、openBLAS等库,避免手动链接遗漏。

2. 确认C++11随机数功能已启用

检查系统中Armadillo的配置文件/usr/include/armadillo_bits/config.hpp,确认是否包含#define ARMA_USE_CXX11_RNG:

grep ARMA_USE_CXX11_RNG /usr/include/armadillo_bits/config.hpp

如果没有找到该定义,在你的示例代码开头添加(必须放在#include <armadillo>之前):

#define ARMA_USE_CXX11_RNG
#include <armadillo>

3. 调整g++编译命令的链接顺序

如果直接用g++编译,确保-larmadillo参数放在源文件之后,正确的命令格式:

g++ main.cpp -o armadillo_demo -larmadillo

链接器按顺序处理输入,库参数放在源文件后才能正确解析代码中引用的符号。

4. 清理CLion缓存并重建项目

CLion的缓存可能导致旧配置残留,执行以下操作:

  • 点击菜单栏File → Invalidate Caches...
  • 选择Invalidate and Restart
  • 重启后重新加载CMake项目并执行构建

5. 验证依赖库的系统注册状态

确认lapack、openBLAS等库已被系统链接器识别:

ldconfig -p | grep lapack
ldconfig -p | grep openblas

如果输出为空,运行sudo ldconfig更新系统库缓存。

内容的提问来源于stack exchange,提问作者Matthew Caius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 12:27:15