Search code examples
c++cmakeclionopenblas

Add/include a Cmake that finds a package into CMake


I found this CMake to find OpenBLAS but I can't find a way how to include that as an external file.

What I have in mind is like #include in C/C++. I tried googling bit i get the answer on how to include a project into CMake.

The main reason for this is that I want to have my CMake as clean and as small as possible since this is the fist time I am diving deeper in CMake world.


Solution

  • but I can't find a way how to include that as an external file.

    You need:

    1. Save the module (FindOpenBLAS.cmake) inside your project, for example:

      Project
      └── cmake
          └── Modules
              └── FindOpenBLAS.cmake
      
    2. Add the path into CMake variable inside CMakeLists.txt:

      set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/")
      
    3. Add find_package directive inside CMakeLists.txt:

      find_package (OpenBLAS REQUIRED)  
      
    4. Use populated variables, for example inside CMakeLists.txt:

      include_directories (${OpenBLAS_INCLUDE_DIR})
      ...
      target_link_libraries (${OpenBLAS_LIB})