0

Trying to compile using CLion on ubuntu 20.04.5 LTS the example code given in Armadillo documentation but getting errors such as: undefined reference to `arma::arma_rng_cxx11_instance'

here is the code

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

int main()
{
    mat A(4, 5, fill::randu);
    mat B(4, 5, fill::randu);

    cout << A*B.t() << endl;

    return 0;
}

I've installed armadillo using sudo apt-get install libarmadillo-dev, and have installed lapack, openBLAS, ARPACK and SuperLU

Here is my makefile:

cmake_minimum_required(VERSION 3.25)
project(ArmadilloSandbox)

set(CMAKE_CXX_STANDARD 17)

find_package(Armadillo REQUIRED)
include_directories(${ARMADILLO_INCLUDE_DIRS})
add_executable(ArmadilloSandbox main.cpp)
target_link_libraries(ArmadilloSandbox ${ARMADILLO_LIBRARIES})

and here is the full error message:

FAILED: ArmadilloSandbox 
: && /usr/bin/c++ -g  CMakeFiles/ArmadilloSandbox.dir/main.cpp.o -o ArmadilloSandbox  /usr/lib/x86_64-linux-gnu/libarmadillo.so && :
/usr/bin/ld: CMakeFiles/ArmadilloSandbox.dir/main.cpp.o: in function `TLS wrapper function for arma::arma_rng_cxx11_instance':
main.cpp:(.text._ZTWN4arma23arma_rng_cxx11_instanceE[_ZTWN4arma23arma_rng_cxx11_instanceE]+0x25): undefined reference to `arma::arma_rng_cxx11_instance'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

And if I use the command:

g++ main.cpp -o prog -O2 -larmadillo

I get a similar issue:

/usr/bin/ld: /tmp/cc3o77Vy.o: in function `arma::arma_rng::randu<double>::fill(double*, unsigned long long)':
main.cpp:(.text._ZN4arma8arma_rng5randuIdE4fillEPdy[_ZN4arma8arma_rng5randuIdE4fillEPdy]+0x2a): undefined reference to `arma::arma_rng_cxx11_instance'
/usr/bin/ld: main.cpp:(.text._ZN4arma8arma_rng5randuIdE4fillEPdy[_ZN4arma8arma_rng5randuIdE4fillEPdy]+0x136): undefined reference to `arma::arma_rng_cxx11_instance'
collect2: error: ld returned 1 exit status

despite checking with pkg-config --libs armadillo which outputs -larmadillo

  • Your CMakeLists.txt does not link to the library at all and in your manual invocation `-larmadillo` is probably either incorrect or incomplete. You can use `pkg-config --libs armadillo` or similar in a shell to obtain the correct list for manual invocation and add appropriate lines to your CMakeLists.txt to link the library, e.g. as in https://cmake.org/cmake/help/latest/module/FindArmadillo.html. – user17732522 May 29 '23 at 21:41
  • @user17732522 I've tried both, running pkg-config --libs armadillo gives -larmadillo and adding the lines to the makefile gives the same out come as the manual invocation – Matthew Caius May 29 '23 at 22:02
  • Please add that information to your question, i.e. complete the `CMakeLists.txt` and update the build output to match cmake's output. Also show your `pkg-config` call and its output in the question. Also make sure that what you are showing in the question is _exactly_ identical to what you tried, e.g. that the arguments to `g++` are in the exact same order. Also include the code that you are trying to compile (which should be in the form of a [mre]) – user17732522 May 29 '23 at 22:20
  • @user17732522 updated as requested – Matthew Caius May 29 '23 at 22:28
  • 1
    Instead of the useless phrase *Issue with*, use the error message as the title. It makes it searchable for future site users, as well as telling people what your question is actually about., I'd strongly suggest that you read [ask] again before your next post. – Ken White May 29 '23 at 22:35
  • @MatthewCaius Thanks, question looks ok to me now. It is still missing some information about your environment though, e.g. what Linux distribution are you using (Ubuntu? Which version?). Unfortunately I can't help you actually solve the issue, but I think it should then be enough information for someone to be able to attempt reproducing the issue. – user17732522 May 29 '23 at 22:36
  • @KenWhite I think the question is now specific enough that it doesn't need to be closed as a duplicate of that one. I don't know whether the problem has an obvious solution or is reproducible for someone more familiar with armadillo, but it looks to me like it could be. – user17732522 May 29 '23 at 22:37
  • @user17732522: I see no edit to the question since I posted my CV, so I don't see any reason it's not still a dupe. If the OP edits the question to add details that make it no longer a dupe, let me know and I'll consider retracting my CV. – Ken White May 29 '23 at 22:39
  • @KenWhite unfortunately that doesn't answer it since I can deduce that something is going wrong with either my installation of armadillo, or linking the library but that doesn't tell me what went wrong exactly and how to fix it, since I suspect its armadillo specific, whatever went wrong. – Matthew Caius May 29 '23 at 22:43
  • I've tried `sudo apt install libarmadillo-dev` and your code on my Ubuntu 20.04.6 and it compiled just fine with your command. It seems like something weird is happening with your specific installation. Maybe `g++` is not the Ubuntu's standard `g++`, for example. Or maybe the installation of the library actually failed. I'd check for `g++`'s version (mine is `Ubuntu 9.4.0-1ubuntu1~20.04.1`) and try reinstalling the library package. – yeputons May 29 '23 at 22:54
  • Also, try running all comments in a freshly started terminal (not CLion), in a separate directory with a short name without any spaces or non-Latin letters, create the file from scratch exactly like in your question. Just in case. – yeputons May 29 '23 at 22:54
  • Btw, I why does your last error message says `main.cpp` while you claim to be compiling with `g++ prog.cpp`? – yeputons May 29 '23 at 22:59
  • @yeputons I have the same version, and I tried purging and reinstalling libarmadillo-dev, also tried the second suggestion, no dice, and the last one is a mistake, as I used the command directly from the documentation, but forgot to change it to main.cpp like I did when I run it, edited to correct this. Do you think this could be an issue with the underlying blas or lapack? – Matthew Caius May 29 '23 at 23:22
  • @MatthewCaius I did not install anything except `libarmadillo-dev`. Did you use `apt install` for them as well? The error seems like some kind of incompatibility between headers and compiled library of Armadillo. E.g. if I omit `-larmadillo` locally, I get multiple undefined references, including exactly the one you see. No idea how it appeared if you have only installed from `sudo apt`. Would you mind double checking there are no `libarmadillo.o`/`libarmadillo.so` not from the package lying somewhere? E.g. purge, `find / -iname '*armadillo*'`, and then remove libraries found. – yeputons May 29 '23 at 23:41

0 Answers0