Unable to link pre-built LAPACKE library in CMake C project

23 Views Asked by At

I would like to use CI for building my C project that I manage with cmake. The project will heavily employ Fortran's BLAS and LAPACK through CBLAS and LAPACKE. The CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.20)
project(collision-detection
    C
    Fortran
)

#include_directories(${CMAKE_SOURCE_DIR}/deps/lapack-3.12.0/build/include)
#link_directories(${CMAKE_SOURCE_DIR}/deps/lapack-3.12.0/build/lib)
message("${CMAKE_SOURCE_DIR}/deps/lapack-3.12.0/build/lib")

add_executable(helloworld_lapacke src/helloworld_lapacke.c)
target_include_directories(helloworld_lapacke PRIVATE ${CMAKE_SOURCE_DIR}/deps/lapack-3.12.0/build/include/)
target_link_directories(helloworld_lapacke PRIVATE ${CMAKE_SOURCE_DIR}/deps/lapack-3.12.0/build/lib/)
target_link_libraries(helloworld_lapacke PRIVATE
    blas cblas
    lapack lapacke
)

Currently I have tried using add_subdirectory() only for a pure Fortran executable linked against BLAS and LAPACK and it works but I am not familiar with CBLAS and LAPACKE.

The reason why I am adding lapacke (and all of its dependencies) to my project like this is that the package I am using (see here for direct download or here for documentation on it) is being built in a separate CI job. Another one takes care of the executable and links it against the artifacts from the job that builds lapacke.

That is why the binaries (static, .a on Linux) and header files are made available via paths relative to the project (here <PROJECT_ROOT>/deps/lapack-3.12.0/build/ with lib and include subdirectories).

If I run the CMakeLists.txt the building fails with an error from the linker:

myproject/deps/lapack-3.12.0/build/lib/liblapacke.a(lapacke_dgels_work.c.o): in function `LAPACKE_dgels_work':
lapacke_dgels_work.c:(.text+0x1c3): undefined reference to `dgels_'
/usr/bin/ld: lapacke_dgels_work.c:(.text+0x2ee): undefined reference to `dgels_'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/helloworld_lapacke.dir/build.make:97: helloworld_lapacke] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/helloworld_lapacke.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

For reproducing the error here is the C code that uses lapacke:

#include <stdio.h>
#include <lapacke.h>

int main(int argc, const char *argv[])
{
   double a[5][3] = {1, 1, 1, 2, 3, 4, 3, 5, 2, 4, 2, 5, 5, 4, 3};
   double b[5][2] = {-10, -3, 12, 14, 14, 12, 16, 16, 18, 16};
   lapack_int info, m, n, lda, ldb, nrhs;
   int i, j;

   m = 5;
   n = 3;
   nrhs = 2;
   lda = 3;
   ldb = 2;

   info = LAPACKE_dgels(LAPACK_ROW_MAJOR, 'N', m, n, nrhs, *a, lda, *b, ldb);

   for (i = 0; i < n; i++)
   {
      for (j = 0; j < nrhs; j++)
      {
         printf("%lf ", b[i][j]);
      }
      printf("\n");
   }
   return (info);
}

Note: I do believe the archive I am using is the same as the contents of this repository

0

There are 0 best solutions below