A. Belyasova
A. Belyasova

Reputation: 32

How do I configure the way a project is built in CLion?

The problem is as follows.

I have installed libarmadillo on my Ubuntu distributive via apt utility having liblapack and libblas previously installed. And I'm trying to use it in my CLion project. What my intentions result in is that I'm receiving some build errors. One of them was solved by adding #define ARMA_DONT_USE_WRAPPER.

I have found the way to build my project in this topic - Armadillo + BLAS + LAPACK: Linking error?. Though, I can build it only with terminal. I assume that the issue with CLion is CMake configuration.

What is the way for me to alter CMake script so that I refine its built behavior and make it compile my project?

In simple words, how do I make it compile my program with g++ main.cpp -o lab2 -O1 -llapack -lblas.

Code sample:

#define ARMA_DONT_USE_WRAPPER
#define ARMA_USE_BLAS
#define ARMA_USE_LAPACK
#include <armadillo>
#include <iostream>
using namespace arma;
using namespace std;

int main() {
mat A(4, 5);
A.load("matrix.txt");
mat B = resize(A, 4, 4);
cout << norm(A, 2);
cout << B;
return 0;
}

The errors I'm issued:

CMakeFiles/lab2.dir/main.cpp.o: In function `double 
arma::blas::asum<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:241: undefined reference 
to `dasum_'
CMakeFiles/lab2.dir/main.cpp.o: In function `double 
arma::blas::nrm2<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:273: undefined reference 
to `dnrm2_'
collect2: error: ld returned 1 exit status
CMakeFiles/lab2.dir/build.make:94: recipe for target 'lab2' failed
make[3]: *** [lab2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/lab2.dir/all' 
failed
make[2]: *** [CMakeFiles/lab2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/lab2.dir/rule' 
failed
make[1]: *** [CMakeFiles/lab2.dir/rule] Error 2
Makefile:118: recipe for target 'lab2' failed
make: *** [lab2] Error 2

CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(lab2)

set(CMAKE_CXX_STANDARD 11)

add_executable(lab2 main.cpp)

Upvotes: 0

Views: 617

Answers (2)

arved
arved

Reputation: 4594

You first need CMake functions to find the libraries liblapack and libblas. For both there are already functions in the standard cmake distribution so

find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
find_package(Armadillo REQUIRED)

See FindBLAS.cmake and FindLAPACK.cmake in your cmake modules directory for documentation what variables they define.

then add them to your targets, e.g.:

target_link_libraries(lab2 ${LAPACK_LIBRARIES} ${BLAS_LIBARIES} ${ARMADILLO_LIBRARIES})

Upvotes: 1

piwi
piwi

Reputation: 5346

As @Richard Hodges said in a comment, in the Settings you can set the variables fed to CMake as well as define environment variables.

Something also I like to do is to include a custom CMake script to easily tweak a configuration; for example add in your CMakeLists.txt:

include(local.cmake OPTIONAL)

That way, you can put any CMake configuration you want in local.cmake in your source directory, and it will be parsed. Nothing happens if the file does not exist.

Upvotes: 0

Related Questions