noobquestionsonly
noobquestionsonly

Reputation: 317

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.

Upvotes: 1

Views: 686

Answers (2)

Andreas Rogge
Andreas Rogge

Reputation: 121

When you link against openblas_64 and CMake decides to do just -lopenblas_64 it probably didn't find a target by that name.

You can run cmake --build . --target help to get a list of all targets defined in your project, there should be at least one for openblas and that's what you need to pass to target_link_libraries().

Upvotes: 0

Milan Š.
Milan Š.

Reputation: 1638

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)

Upvotes: 2

Related Questions