cmakeopenblas

CMAKE target link to a local openblas folder


I do not have Openblas installed. I do not want to install it. I want CMAKE to build my project using a local copy of OpenBLAS. I read this SO post: Using vendored OpenBLAS as a dependency in a CMake project Based on that, I made my CMAKELists.txt below:

My CMAKElists.txt:

cmake_minimum_required(VERSION 3.24)
project(untitled)

set(CMAKE_CXX_STANDARD 20)

add_executable(Eigentest main.cpp)

include_directories(${CMAKE_SOURCE_DIR}/Libraries/eigen-3.4.0)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -O3 -fopenmp -march=native")

add_subdirectory("${CMAKE_SOURCE_DIR}/Libraries/OpenBLAS-0.3.21")

target_link_libraries(Eigentest openblas_64)

I do get this output from CMAKE:

[...]
-- /home/user/Desktop/untitled/cmake-build-debug/Libraries/OpenBLAS-0.3.21/lapack/CMakeFiles/dtrti2_LU.c
-- /home/user/Desktop/untitled/cmake-build-debug/Libraries/OpenBLAS-0.3.21/lapack/CMakeFiles/ctrti2_LU.c
-- /home/user/Desktop/untitled/cmake-build-debug/Libraries/OpenBLAS-0.3.21/lapack/CMakeFiles/ztrti2_LU.c
-- c lapack
-- Building deprecated routines
-- Building Single Precision
-- Building Double Precision
-- Building Single Precision Complex
-- Building Double Precision Complex
-- Generating openblas_config.h in include/openblas
-- Generating cblas.h in include/openblas
-- Copying LAPACKE header files to include/openblas
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Desktop/untitled/cmake-build-debug

When I compile my project, I get this error:

FAILED: Eigentest 
: && /usr/bin/c++ -fsanitize=address -O3 -fopenmp -march=native -g  CMakeFiles/Eigentest.dir/main.cpp.o -o Eigentest  -lopenblas_64 && :
/usr/bin/ld: cannot find -lopenblas_64: No such file or directory
collect2: error: ld returned 1 exit status

How do I link to a local copy of openblas? I want to link to it without installing it.


Solution

  • I can provide you only with a wild guess because you haven't provided the entire make log:

    Normally when you add_library CMake configures the entire path for any targets that link against it. Judging by what's passed to /usr/bin/c++ this isn't happening. I assume it's mostly because the targets from add_subdirectory are only available for the local scope of that given subdirectory CMakeLists.txt.

    To quickly test this out try to add the target in your root CMakeLists.txt file with the IMPORTED tag (after the add_subdirectory, i.e.:

    add_library(openblas_64 SHARED IMPORTED)
    
    #OR
    
    add_library(openblas_64 STATIC IMPORTED)
    

    Then proceed with the target_link_libraries(...)

    Thus your CMakeLists.txt should look like this:

    cmake_minimum_required(VERSION 3.24)
    project(untitled)
    
    set(CMAKE_CXX_STANDARD 20)
    
    add_executable(Eigentest main.cpp)
    
    include_directories(${CMAKE_SOURCE_DIR}/Libraries/eigen-3.4.0)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -O3 -fopenmp -march=native")
    
    add_subdirectory("${CMAKE_SOURCE_DIR}/Libraries/OpenBLAS-0.3.21")
    
    add_library(openblas_64 SHARED IMPORTED)
    #OR: add_library(openblas_64 STATIC IMPORTED)
    
    target_link_libraries(Eigentest openblas_64)